1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
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.
15 #include "common/code_environment.h"
21 #ifdef HAVE_PTHREAD_GETNAME_NP
27 code_environment_t g_code_env
= CODE_ENVIRONMENT_UTILITY
;
29 extern "C" const char *code_environment_to_str(enum code_environment_t 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";
43 std::ostream
&operator<<(std::ostream
&oss
, const enum code_environment_t e
)
45 oss
<< code_environment_to_str(e
);
49 #if defined(HAVE_PTHREAD_GETNAME_NP) && !defined(_WIN32)
51 int get_process_name(char *buf
, int len
)
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.
58 // FIPS zeroization audit 20191115: this memset is not security related.
60 return pthread_getname_np(pthread_self(), buf
, len
);
63 #elif defined(HAVE_GETPROGNAME)
65 int get_process_name(char *buf
, int len
)
71 const char *progname
= getprogname();
72 if (progname
== nullptr || *progname
== '\0') {
76 strncpy(buf
, progname
, len
- 1);
83 int get_process_name(char *buf
, int len
)
89 char full_path
[MAX_PATH
];
90 int length
= GetModuleFileNameA(nullptr, full_path
, sizeof(full_path
));
94 char* start
= strrchr(full_path
, '\\');
98 char* end
= strstr(start
, ".exe");
101 if (len
<= end
- start
) {
102 return -ENAMETOOLONG
;
105 memcpy(buf
, start
, end
- start
);
106 buf
[end
- start
] = '\0';
112 int get_process_name(char *buf
, int len
)
119 std::string
get_process_name_cpp()
122 if (get_process_name(buf
, sizeof(buf
))) {
125 return std::string(buf
);