]>
Commit | Line | Data |
---|---|---|
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 | */ | |
14 | #include "acconfig.h" | |
15 | ||
16 | #include "common/code_environment.h" | |
17 | ||
18 | #include <errno.h> | |
19 | #include <iostream> | |
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
22 | #include <string> | |
23 | ||
24 | #ifdef HAVE_SYS_PRCTL_H | |
25 | #include <sys/prctl.h> | |
26 | #endif | |
27 | ||
28 | code_environment_t g_code_env = CODE_ENVIRONMENT_UTILITY; | |
29 | ||
30 | extern "C" const char *code_environment_to_str(enum code_environment_t e) | |
31 | { | |
32 | switch (e) { | |
33 | case CODE_ENVIRONMENT_UTILITY: | |
34 | return "CODE_ENVIRONMENT_UTILITY"; | |
35 | case CODE_ENVIRONMENT_DAEMON: | |
36 | return "CODE_ENVIRONMENT_DAEMON"; | |
37 | case CODE_ENVIRONMENT_LIBRARY: | |
38 | return "CODE_ENVIRONMENT_LIBRARY"; | |
39 | default: | |
40 | return NULL; | |
41 | } | |
42 | } | |
43 | ||
44 | std::ostream &operator<<(std::ostream &oss, enum code_environment_t e) | |
45 | { | |
46 | oss << code_environment_to_str(e); | |
47 | return oss; | |
48 | } | |
49 | ||
50 | #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME) /* Since 2.6.11 */ | |
51 | ||
52 | int get_process_name(char *buf, int len) | |
53 | { | |
54 | if (len <= 16) { | |
55 | /* The man page discourages using this prctl with a buffer shorter | |
56 | * than 16 bytes. With a 16-byte buffer, it might not be | |
57 | * null-terminated. */ | |
58 | return -ENAMETOOLONG; | |
59 | } | |
60 | memset(buf, 0, len); | |
61 | return prctl(PR_GET_NAME, buf); | |
62 | } | |
63 | ||
64 | #else | |
65 | ||
66 | int get_process_name(char *buf, int len) | |
67 | { | |
68 | return -ENOSYS; | |
69 | } | |
70 | ||
71 | #endif | |
72 | ||
73 | std::string get_process_name_cpp() | |
74 | { | |
75 | char buf[32]; | |
76 | if (get_process_name(buf, sizeof(buf))) { | |
77 | return "(unknown)"; | |
78 | } | |
79 | return std::string(buf); | |
80 | } |