]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/code_environment.cc
update sources to 12.2.7
[ceph.git] / ceph / src / common / code_environment.cc
CommitLineData
7c673cae
FG
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 */
7c673cae
FG
14
15#include "common/code_environment.h"
16
7c673cae 17#include <iostream>
7c673cae 18
28e407b8
AA
19#include "acconfig.h"
20
7c673cae
FG
21#ifdef HAVE_SYS_PRCTL_H
22#include <sys/prctl.h>
23#endif
24
28e407b8
AA
25#include <string.h>
26
7c673cae
FG
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
31f18b77 43std::ostream &operator<<(std::ostream &oss, const enum code_environment_t e)
7c673cae
FG
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
51int 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 memset(buf, 0, len);
60 return prctl(PR_GET_NAME, buf);
61}
62
31f18b77
FG
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
7c673cae
FG
81#else
82
83int get_process_name(char *buf, int len)
84{
85 return -ENOSYS;
86}
87
88#endif
89
90std::string get_process_name_cpp()
91{
92 char buf[32];
93 if (get_process_name(buf, sizeof(buf))) {
94 return "(unknown)";
95 }
96 return std::string(buf);
97}