summaryrefslogtreecommitdiffstats
path: root/src/common/code_environment.cc
blob: 14d55f60c3042d04fab52b9ec006115523d43b3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2011 New Dream Network
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "common/code_environment.h"

#include <iostream>

#include "acconfig.h"

#ifdef HAVE_PTHREAD_GETNAME_NP
#include <pthread.h>
#endif

#include <string.h>

code_environment_t g_code_env = CODE_ENVIRONMENT_UTILITY;

extern "C" const char *code_environment_to_str(enum code_environment_t e)
{
  switch (e) {
    case CODE_ENVIRONMENT_UTILITY:
      return "CODE_ENVIRONMENT_UTILITY";
    case CODE_ENVIRONMENT_DAEMON:
      return "CODE_ENVIRONMENT_DAEMON";
    case CODE_ENVIRONMENT_LIBRARY:
      return "CODE_ENVIRONMENT_LIBRARY";
    default:
      return NULL;
  }
}

std::ostream &operator<<(std::ostream &oss, const enum code_environment_t e)
{
  oss << code_environment_to_str(e);
  return oss;
}

#if defined(HAVE_PTHREAD_GETNAME_NP) && !defined(_WIN32)

int get_process_name(char *buf, int len)
{
  if (len <= 16) {
    // The man page discourages using pthread_getname_np() with a buffer shorter
    // than 16 bytes. With a 16-byte buffer, it might not be null-terminated.
    return -ENAMETOOLONG;
  }
  // FIPS zeroization audit 20191115: this memset is not security related.
  memset(buf, 0, len);
  return pthread_getname_np(pthread_self(), buf, len);
}

#elif defined(HAVE_GETPROGNAME)

int get_process_name(char *buf, int len)
{
  if (len <= 0) {
    return -EINVAL;
  }

  const char *progname = getprogname();
  if (progname == nullptr || *progname == '\0') {
    return -ENOSYS;
  }

  strncpy(buf, progname, len - 1);
  buf[len - 1] = '\0';
  return 0;
}

#elif defined(_WIN32)

int get_process_name(char *buf, int len)
{
  if (len <= 0) {
    return -EINVAL;
  }

  char full_path[MAX_PATH];
  int length = GetModuleFileNameA(nullptr, full_path, sizeof(full_path));
  if (length <= 0)
    return -ENOSYS;

  char* start = strrchr(full_path, '\\');
  if (!start)
    return -ENOSYS;
  start++;
  char* end = strstr(start, ".exe");
  if (!end)
    return -ENOSYS;
  if (len <= end - start) {
    return -ENAMETOOLONG;
  }

  memcpy(buf, start, end - start);
  buf[end - start] = '\0';
  return 0;
}

#else

int get_process_name(char *buf, int len)
{
  return -ENOSYS;
}

#endif

std::string get_process_name_cpp()
{
  char buf[32];
  if (get_process_name(buf, sizeof(buf))) {
    return "(unknown)";
  }
  return std::string(buf);
}