]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2011 New Dream Network | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
7c673cae FG |
14 | |
15 | #include "common/code_environment.h" | |
16 | ||
7c673cae | 17 | #include <iostream> |
7c673cae | 18 | |
28e407b8 AA |
19 | #include "acconfig.h" |
20 | ||
20effc67 TL |
21 | #ifdef HAVE_PTHREAD_GETNAME_NP |
22 | #include <pthread.h> | |
7c673cae FG |
23 | #endif |
24 | ||
28e407b8 AA |
25 | #include <string.h> |
26 | ||
7c673cae FG |
27 | code_environment_t g_code_env = CODE_ENVIRONMENT_UTILITY; |
28 | ||
29 | extern "C" const char *code_environment_to_str(enum code_environment_t e) | |
30 | { | |
31 | switch (e) { | |
32 | case CODE_ENVIRONMENT_UTILITY: | |
33 | return "CODE_ENVIRONMENT_UTILITY"; | |
34 | case CODE_ENVIRONMENT_DAEMON: | |
35 | return "CODE_ENVIRONMENT_DAEMON"; | |
36 | case CODE_ENVIRONMENT_LIBRARY: | |
37 | return "CODE_ENVIRONMENT_LIBRARY"; | |
38 | default: | |
39 | return NULL; | |
40 | } | |
41 | } | |
42 | ||
31f18b77 | 43 | std::ostream &operator<<(std::ostream &oss, const enum code_environment_t e) |
7c673cae FG |
44 | { |
45 | oss << code_environment_to_str(e); | |
46 | return oss; | |
47 | } | |
48 | ||
20effc67 | 49 | #if defined(HAVE_PTHREAD_GETNAME_NP) && !defined(_WIN32) |
7c673cae FG |
50 | |
51 | int get_process_name(char *buf, int len) | |
52 | { | |
53 | if (len <= 16) { | |
20effc67 TL |
54 | // The man page discourages using pthread_getname_np() with a buffer shorter |
55 | // than 16 bytes. With a 16-byte buffer, it might not be null-terminated. | |
7c673cae FG |
56 | return -ENAMETOOLONG; |
57 | } | |
92f5a8d4 | 58 | // FIPS zeroization audit 20191115: this memset is not security related. |
7c673cae | 59 | memset(buf, 0, len); |
20effc67 | 60 | return pthread_getname_np(pthread_self(), buf, len); |
7c673cae FG |
61 | } |
62 | ||
31f18b77 FG |
63 | #elif defined(HAVE_GETPROGNAME) |
64 | ||
65 | int get_process_name(char *buf, int len) | |
66 | { | |
67 | if (len <= 0) { | |
68 | return -EINVAL; | |
69 | } | |
70 | ||
71 | const char *progname = getprogname(); | |
72 | if (progname == nullptr || *progname == '\0') { | |
73 | return -ENOSYS; | |
74 | } | |
75 | ||
76 | strncpy(buf, progname, len - 1); | |
77 | buf[len - 1] = '\0'; | |
78 | return 0; | |
79 | } | |
80 | ||
f67539c2 TL |
81 | #elif defined(_WIN32) |
82 | ||
83 | int get_process_name(char *buf, int len) | |
84 | { | |
85 | if (len <= 0) { | |
86 | return -EINVAL; | |
87 | } | |
88 | ||
89 | char full_path[MAX_PATH]; | |
90 | int length = GetModuleFileNameA(nullptr, full_path, sizeof(full_path)); | |
91 | if (length <= 0) | |
92 | return -ENOSYS; | |
93 | ||
94 | char* start = strrchr(full_path, '\\'); | |
95 | if (!start) | |
96 | return -ENOSYS; | |
97 | start++; | |
98 | char* end = strstr(start, ".exe"); | |
99 | if (!end) | |
100 | return -ENOSYS; | |
101 | if (len <= end - start) { | |
102 | return -ENAMETOOLONG; | |
103 | } | |
104 | ||
105 | memcpy(buf, start, end - start); | |
106 | buf[end - start] = '\0'; | |
107 | return 0; | |
108 | } | |
109 | ||
7c673cae FG |
110 | #else |
111 | ||
112 | int get_process_name(char *buf, int len) | |
113 | { | |
114 | return -ENOSYS; | |
115 | } | |
116 | ||
117 | #endif | |
118 | ||
119 | std::string get_process_name_cpp() | |
120 | { | |
121 | char buf[32]; | |
122 | if (get_process_name(buf, sizeof(buf))) { | |
123 | return "(unknown)"; | |
124 | } | |
125 | return std::string(buf); | |
126 | } |