]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/code_environment.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / common / code_environment.cc
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 */
14
15 #include "common/code_environment.h"
16
17 #include <iostream>
18
19 #include "acconfig.h"
20
21 #ifdef HAVE_SYS_PRCTL_H
22 #include <sys/prctl.h>
23 #endif
24
25 #include <string.h>
26
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
43 std::ostream &operator<<(std::ostream &oss, const enum code_environment_t e)
44 {
45 oss << code_environment_to_str(e);
46 return oss;
47 }
48
49 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME) /* Since 2.6.11 */
50
51 int get_process_name(char *buf, int len)
52 {
53 if (len <= 16) {
54 /* The man page discourages using this prctl with a buffer shorter
55 * than 16 bytes. With a 16-byte buffer, it might not be
56 * null-terminated. */
57 return -ENAMETOOLONG;
58 }
59 // FIPS zeroization audit 20191115: this memset is not security related.
60 memset(buf, 0, len);
61 return prctl(PR_GET_NAME, buf);
62 }
63
64 #elif defined(HAVE_GETPROGNAME)
65
66 int get_process_name(char *buf, int len)
67 {
68 if (len <= 0) {
69 return -EINVAL;
70 }
71
72 const char *progname = getprogname();
73 if (progname == nullptr || *progname == '\0') {
74 return -ENOSYS;
75 }
76
77 strncpy(buf, progname, len - 1);
78 buf[len - 1] = '\0';
79 return 0;
80 }
81
82 #elif defined(_WIN32)
83
84 int get_process_name(char *buf, int len)
85 {
86 if (len <= 0) {
87 return -EINVAL;
88 }
89
90 char full_path[MAX_PATH];
91 int length = GetModuleFileNameA(nullptr, full_path, sizeof(full_path));
92 if (length <= 0)
93 return -ENOSYS;
94
95 char* start = strrchr(full_path, '\\');
96 if (!start)
97 return -ENOSYS;
98 start++;
99 char* end = strstr(start, ".exe");
100 if (!end)
101 return -ENOSYS;
102 if (len <= end - start) {
103 return -ENAMETOOLONG;
104 }
105
106 memcpy(buf, start, end - start);
107 buf[end - start] = '\0';
108 return 0;
109 }
110
111 #else
112
113 int get_process_name(char *buf, int len)
114 {
115 return -ENOSYS;
116 }
117
118 #endif
119
120 std::string get_process_name_cpp()
121 {
122 char buf[32];
123 if (get_process_name(buf, sizeof(buf))) {
124 return "(unknown)";
125 }
126 return std::string(buf);
127 }