root/procname.cpp
/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- lockPrgNameMapLock
- unlockPrgNameMapLock
- dmtcp_ProcName_EventHook
- prctlReset
- prctlGetProcessName
- prctlRestoreProcessName
1 #include <sys/prctl.h>
2 #include "dmtcp.h"
3 #include "dmtcpalloc.h"
4 #include "util.h"
5 #include "syscallwrappers.h"
6 #include "../jalib/jassert.h"
7
8 using namespace dmtcp;
9
10 static const char* DMTCP_PRGNAME_PREFIX = "DMTCP:";
11
12 static map<pid_t, string> prgNameMap;
13
14 static void prctlReset();
15 static void prctlGetProcessName();
16 static void prctlRestoreProcessName();
17
18 static pthread_mutex_t prgNameMapLock = PTHREAD_MUTEX_INITIALIZER;
19
20 // No need to reset the locks on fork because the locks are grabbed/released
21 // only during ckpt cycle and during that time fork is guaranteed to _not_ take
22 // place (courtesy of wrapper-execution locks).
23 static void lockPrgNameMapLock()
24 {
25 JASSERT(_real_pthread_mutex_lock(&prgNameMapLock) == 0) (JASSERT_ERRNO);
26 }
27
28 static void unlockPrgNameMapLock()
29 {
30 JASSERT(_real_pthread_mutex_unlock(&prgNameMapLock) == 0) (JASSERT_ERRNO);
31 }
32
33 void dmtcp_ProcName_EventHook(DmtcpEvent_t event, DmtcpEventData_t *data)
34 {
35 switch (event) {
36 case DMTCP_EVENT_WAIT_FOR_SUSPEND_MSG:
37 prctlReset();
38 break;
39
40 case DMTCP_EVENT_THREADS_SUSPEND:
41 case DMTCP_EVENT_PRE_SUSPEND_USER_THREAD:
42 prctlGetProcessName();
43 break;
44
45 case DMTCP_EVENT_RESUME_USER_THREAD:
46 if (data->resumeUserThreadInfo.isRestart) {
47 prctlRestoreProcessName();
48 }
49 break;
50
51 case DMTCP_EVENT_RESTART:
52 prctlRestoreProcessName();
53 break;
54
55 default:
56 break;
57 }
58 }
59
60 void prctlReset()
61 {
62 prgNameMap.clear();
63 }
64
65 void prctlGetProcessName()
66 {
67 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
68 char name[17] = {0};
69 int ret = prctl(PR_GET_NAME, name);
70 if (ret != -1) {
71 lockPrgNameMapLock();
72 prgNameMap[dmtcp_gettid()] = name;
73 unlockPrgNameMapLock();
74 JTRACE("prctl(PR_GET_NAME, ...) succeeded") (name);
75 } else {
76 JASSERT(errno == EINVAL) (JASSERT_ERRNO)
77 .Text ("prctl(PR_GET_NAME, ...) failed");
78 JTRACE("prctl(PR_GET_NAME, ...) failed. Not supported on this kernel?");
79 }
80 #endif
81 }
82
83 void prctlRestoreProcessName()
84 {
85 // Although PR_SET_NAME has been supported since 2.6.9, we wouldn't use it on
86 // kernel < 2.6.11 since we didn't get the process name using PR_GET_NAME
87 // which is supported on >= 2.6.11
88 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
89 // NOTE: We don't need to protect the access to prgNameMap with a lock
90 // because all accesses during restart are guaranteed to be read-only.
91 string prgName = prgNameMap[dmtcp_gettid()];
92 if (!Util::strStartsWith(prgName, DMTCP_PRGNAME_PREFIX)) {
93 // Add the "DMTCP:" prefix.
94 prgName = DMTCP_PRGNAME_PREFIX + prgName;
95 }
96
97 if (prctl(PR_SET_NAME, prgName.c_str()) != -1) {
98 JTRACE("prctl(PR_SET_NAME, ...) succeeded") (prgName);
99 } else {
100 JASSERT(errno == EINVAL) (prgName) (JASSERT_ERRNO)
101 .Text ("prctl(PR_SET_NAME, ...) failed");
102 JTRACE("prctl(PR_SET_NAME, ...) failed") (prgName);
103 }
104 #endif
105 }
106