]> git.proxmox.com Git - ceph.git/blame_incremental - ceph/src/common/code_environment.cc
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / common / code_environment.cc
... / ...
CommitLineData
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_PTHREAD_GETNAME_NP
22#include <pthread.h>
23#endif
24
25#include <string.h>
26
27code_environment_t g_code_env = CODE_ENVIRONMENT_UTILITY;
28
29extern "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
43std::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_PTHREAD_GETNAME_NP) && !defined(_WIN32)
50
51int get_process_name(char *buf, int len)
52{
53 if (len <= 16) {
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.
56 return -ENAMETOOLONG;
57 }
58 // FIPS zeroization audit 20191115: this memset is not security related.
59 memset(buf, 0, len);
60 return pthread_getname_np(pthread_self(), buf, len);
61}
62
63#elif defined(HAVE_GETPROGNAME)
64
65int 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
81#elif defined(_WIN32)
82
83int 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
110#else
111
112int get_process_name(char *buf, int len)
113{
114 return -ENOSYS;
115}
116
117#endif
118
119std::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}