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"
19 #ifdef HAVE_SYS_PRCTL_H
20 #include <sys/prctl.h>
23 code_environment_t g_code_env = CODE_ENVIRONMENT_UTILITY;
25 extern "C" const char *code_environment_to_str(enum code_environment_t e)
28 case CODE_ENVIRONMENT_UTILITY:
29 return "CODE_ENVIRONMENT_UTILITY";
30 case CODE_ENVIRONMENT_DAEMON:
31 return "CODE_ENVIRONMENT_DAEMON";
32 case CODE_ENVIRONMENT_LIBRARY:
33 return "CODE_ENVIRONMENT_LIBRARY";
39 std::ostream &operator<<(std::ostream &oss, const enum code_environment_t e)
41 oss << code_environment_to_str(e);
45 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME) /* Since 2.6.11 */
47 int get_process_name(char *buf, int len)
50 /* The man page discourages using this prctl with a buffer shorter
51 * than 16 bytes. With a 16-byte buffer, it might not be
56 return prctl(PR_GET_NAME, buf);
59 #elif defined(HAVE_GETPROGNAME)
61 int get_process_name(char *buf, int len)
67 const char *progname = getprogname();
68 if (progname == nullptr || *progname == '\0') {
72 strncpy(buf, progname, len - 1);
79 int get_process_name(char *buf, int len)
86 std::string get_process_name_cpp()
89 if (get_process_name(buf, sizeof(buf))) {
92 return std::string(buf);