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_SYS_PRCTL_H
22 #include <sys/prctl.h>
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_SYS_PRCTL_H) && defined(PR_GET_NAME) /* Since 2.6.11 */
51 int get_process_name(char *buf
, int len
)
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
60 return prctl(PR_GET_NAME
, buf
);
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
)
90 std::string
get_process_name_cpp()
93 if (get_process_name(buf
, sizeof(buf
))) {
96 return std::string(buf
);