Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | #include "includes.h" | - |
35 | | - |
36 | #ifndef HAVE_SETPROCTITLE | - |
37 | | - |
38 | #include <stdarg.h> | - |
39 | #include <stdlib.h> | - |
40 | #include <unistd.h> | - |
41 | #ifdef HAVE_SYS_PSTAT_H | - |
42 | #include <sys/pstat.h> | - |
43 | #endif | - |
44 | #include <string.h> | - |
45 | | - |
46 | #include <vis.h> | - |
47 | | - |
48 | #define SPT_NONE 0 /* don't use it at all */ | - |
49 | #define SPT_PSTAT 1 /* use pstat(PSTAT_SETCMD, ...) */ | - |
50 | #define SPT_REUSEARGV 2 /* cover argv with title information */ | - |
51 | | - |
52 | #ifndef SPT_TYPE | - |
53 | # define SPT_TYPE SPT_NONE | - |
54 | #endif | - |
55 | | - |
56 | #ifndef SPT_PADCHAR | - |
57 | # define SPT_PADCHAR '\0' | - |
58 | #endif | - |
59 | | - |
60 | #if SPT_TYPE == SPT_REUSEARGV | - |
61 | static char *argv_start = NULL; | - |
62 | static size_t argv_env_len = 0; | - |
63 | #endif | - |
64 | | - |
65 | #endif /* HAVE_SETPROCTITLE */ | - |
66 | | - |
67 | void | - |
68 | compat_init_setproctitle(int argc, char *argv[]) | - |
69 | { | - |
70 | #if !defined(HAVE_SETPROCTITLE) && \ | - |
71 | defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV | - |
72 | extern char **environ; | - |
73 | char *lastargv = NULL; | - |
74 | char **envp = environ; | - |
75 | int i; | - |
76 | | - |
77 | | - |
78 | | - |
79 | | - |
80 | | - |
81 | | - |
82 | | - |
83 | if (argc == 0 || argv[0] == NULL)TRUE | never evaluated | FALSE | evaluated 2 times by 1 test |
TRUE | never evaluated | FALSE | evaluated 2 times by 1 test |
| 0-2 |
84 | return; never executed: return; | 0 |
85 | | - |
86 | | - |
87 | for (i = 0; envp[i] != NULL; i++)TRUE | evaluated 110 times by 1 test | FALSE | evaluated 2 times by 1 test |
| 2-110 |
88 | ;executed 110 times by 1 test: ; | 110 |
89 | if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) {TRUE | never evaluated | FALSE | evaluated 2 times by 1 test |
| 0-2 |
90 | environ = envp; | - |
91 | return; never executed: return; | 0 |
92 | } | - |
93 | | - |
94 | | - |
95 | | - |
96 | | - |
97 | | - |
98 | for (i = 0; i < argc; i++) {TRUE | evaluated 8 times by 1 test | FALSE | evaluated 2 times by 1 test |
| 2-8 |
99 | if (lastargv == NULL || lastargv + 1 == argv[i])TRUE | evaluated 2 times by 1 test | FALSE | evaluated 6 times by 1 test |
TRUE | evaluated 6 times by 1 test | FALSE | never evaluated |
| 0-6 |
100 | lastargv = argv[i] + strlen(argv[i]);executed 8 times by 1 test: lastargv = argv[i] + strlen(argv[i]); | 8 |
101 | }executed 8 times by 1 test: end of block | 8 |
102 | for (i = 0; envp[i] != NULL; i++) {TRUE | evaluated 110 times by 1 test | FALSE | evaluated 2 times by 1 test |
| 2-110 |
103 | if (lastargv + 1 == envp[i])TRUE | evaluated 110 times by 1 test | FALSE | never evaluated |
| 0-110 |
104 | lastargv = envp[i] + strlen(envp[i]);executed 110 times by 1 test: lastargv = envp[i] + strlen(envp[i]); | 110 |
105 | }executed 110 times by 1 test: end of block | 110 |
106 | | - |
107 | argv[1] = NULL; | - |
108 | argv_start = argv[0]; | - |
109 | argv_env_len = lastargv - argv[0] - 1; | - |
110 | | - |
111 | | - |
112 | | - |
113 | | - |
114 | | - |
115 | for (i = 0; envp[i] != NULL; i++)TRUE | evaluated 110 times by 1 test | FALSE | evaluated 2 times by 1 test |
| 2-110 |
116 | environ[i] = strdup(envp[i]);executed 110 times by 1 test: environ[i] = (__extension__ (__builtin_constant_p ( envp[i] ) && ((size_t)(const void *)(( envp[i] ) + 1) - (size_t)(const void *)( envp[i] ) == 1) ? (((const char *) ( envp[i] ))[0] == '\0' ? (char *) calloc ((size_t) 1, (size_t) 1) : ({ size_t __len = strlen ( envp[i] ) + 1; char *__retval = (char *) malloc (__len); if (__retval != ((void *)0)) __retval = (char *) memcpy (__retval, envp[i] , __len); __retval; })) : __strdup ( envp[i] ))) ; never executed: __retval = (char *) memcpy (__retval, envp[i] , __len); TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | evaluated 110 times by 1 test |
TRUE | never evaluated | FALSE | never evaluated |
| 0-110 |
117 | environ[i] = NULL; | - |
118 | #endif /* SPT_REUSEARGV */ | - |
119 | }executed 2 times by 1 test: end of block | 2 |
120 | | - |
121 | #ifndef HAVE_SETPROCTITLE | - |
122 | void | - |
123 | setproctitle(const char *fmt, ...) | - |
124 | { | - |
125 | #if SPT_TYPE != SPT_NONE | - |
126 | va_list ap; | - |
127 | char buf[1024], ptitle[1024]; | - |
128 | size_t len = 0; | - |
129 | int r; | - |
130 | extern char *__progname; | - |
131 | #if SPT_TYPE == SPT_PSTAT | - |
132 | union pstun pst; | - |
133 | #endif | - |
134 | | - |
135 | #if SPT_TYPE == SPT_REUSEARGV | - |
136 | if (argv_env_len <= 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
137 | return; never executed: return; | 0 |
138 | #endif | - |
139 | | - |
140 | strlcpy(buf, __progname, sizeof(buf)); | - |
141 | | - |
142 | r = -1; | - |
143 | va_start(ap, fmt); | - |
144 | if (fmt != NULL) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
145 | len = strlcat(buf, ": ", sizeof(buf)); | - |
146 | if (len < sizeof(buf))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
147 | r = vsnprintf(buf + len, sizeof(buf) - len , fmt, ap); never executed: r = vsnprintf(buf + len, sizeof(buf) - len , fmt, ap); | 0 |
148 | } never executed: end of block | 0 |
149 | va_end(ap); | - |
150 | if (r == -1 || (size_t)r >= sizeof(buf) - len)TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
151 | return; never executed: return; | 0 |
152 | strnvis(ptitle, buf, sizeof(ptitle), | - |
153 | VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL); | - |
154 | | - |
155 | #if SPT_TYPE == SPT_PSTAT | - |
156 | pst.pst_command = ptitle; | - |
157 | pstat(PSTAT_SETCMD, pst, strlen(ptitle), 0, 0); | - |
158 | #elif SPT_TYPE == SPT_REUSEARGV | - |
159 | | - |
160 | | - |
161 | len = strlcpy(argv_start, ptitle, argv_env_len); | - |
162 | for(; len < argv_env_len; len++)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
163 | argv_start[len] = SPT_PADCHAR; never executed: argv_start[len] = '\0'; | 0 |
164 | #endif | - |
165 | | - |
166 | #endif /* SPT_NONE */ | - |
167 | } never executed: end of block | 0 |
168 | | - |
169 | #endif /* HAVE_SETPROCTITLE */ | - |
| | |