/[baselayout]/trunk/src/librc-daemon.c
Gentoo

Diff of /trunk/src/librc-daemon.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2569 Revision 2577
7 7
8#include <sys/types.h> 8#include <sys/types.h>
9#include <sys/stat.h> 9#include <sys/stat.h>
10 10
11#if defined(__DragonFly__) || defined(__FreeBSD__) || \ 11#if defined(__DragonFly__) || defined(__FreeBSD__) || \
12 defined(__NetBSD__) || defined (__OpenBSD__) 12 defined(__NetBSD__) || defined (__OpenBSD__)
13#include <sys/param.h> 13#include <sys/param.h>
14#include <sys/user.h> 14#include <sys/user.h>
15#include <sys/sysctl.h> 15#include <sys/sysctl.h>
16#include <kvm.h> 16#include <kvm.h>
17#include <limits.h> 17#include <limits.h>
35#include "strlist.h" 35#include "strlist.h"
36 36
37#if defined(__linux__) 37#if defined(__linux__)
38static bool pid_is_cmd (pid_t pid, const char *cmd) 38static bool pid_is_cmd (pid_t pid, const char *cmd)
39{ 39{
40 char buffer[32]; 40 char buffer[32];
41 FILE *fp; 41 FILE *fp;
42 int c; 42 int c;
43 43
44 snprintf(buffer, sizeof (buffer), "/proc/%d/stat", pid); 44 snprintf(buffer, sizeof (buffer), "/proc/%d/stat", pid);
45 if ((fp = fopen (buffer, "r")) == NULL) 45 if ((fp = fopen (buffer, "r")) == NULL)
46 return (false); 46 return (false);
47 47
48 while ((c = getc (fp)) != EOF && c != '(') 48 while ((c = getc (fp)) != EOF && c != '(')
49 ; 49 ;
50 50
51 if (c != '(') 51 if (c != '(') {
52 { 52 fclose(fp);
53 fclose(fp);
54 return (false); 53 return (false);
55 } 54 }
56 55
57 while ((c = getc (fp)) != EOF && c == *cmd) 56 while ((c = getc (fp)) != EOF && c == *cmd)
58 cmd++; 57 cmd++;
59 58
60 fclose (fp); 59 fclose (fp);
61 60
62 return ((c == ')' && *cmd == '\0') ? true : false); 61 return ((c == ')' && *cmd == '\0') ? true : false);
63} 62}
64 63
65static bool pid_is_exec (pid_t pid, const char *exec) 64static bool pid_is_exec (pid_t pid, const char *exec)
66{ 65{
67 char cmdline[32]; 66 char cmdline[32];
68 char buffer[PATH_MAX]; 67 char buffer[PATH_MAX];
69 char *p; 68 char *p;
70 int fd = -1; 69 int fd = -1;
71 int r; 70 int r;
72 71
73 snprintf (cmdline, sizeof (cmdline), "/proc/%u/exe", pid); 72 snprintf (cmdline, sizeof (cmdline), "/proc/%u/exe", pid);
74 memset (buffer, 0, sizeof (buffer)); 73 memset (buffer, 0, sizeof (buffer));
75 if (readlink (cmdline, buffer, sizeof (buffer)) != -1) 74 if (readlink (cmdline, buffer, sizeof (buffer)) != -1) {
76 {
77 if (strcmp (exec, buffer) == 0) 75 if (strcmp (exec, buffer) == 0)
78 return (true); 76 return (true);
79 77
80 /* We should cater for deleted binaries too */ 78 /* We should cater for deleted binaries too */
81 if (strlen (buffer) > 10) 79 if (strlen (buffer) > 10) {
82 {
83 p = buffer + (strlen (buffer) - 10); 80 p = buffer + (strlen (buffer) - 10);
84 if (strcmp (p, " (deleted)") == 0) 81 if (strcmp (p, " (deleted)") == 0) {
85 { 82 *p = 0;
86 *p = 0;
87 if (strcmp (buffer, exec) == 0) 83 if (strcmp (buffer, exec) == 0)
88 return (true); 84 return (true);
89 } 85 }
90 } 86 }
91 } 87 }
92 88
93 snprintf (cmdline, sizeof (cmdline), "/proc/%u/cmdline", pid); 89 snprintf (cmdline, sizeof (cmdline), "/proc/%u/cmdline", pid);
94 if ((fd = open (cmdline, O_RDONLY)) < 0) 90 if ((fd = open (cmdline, O_RDONLY)) < 0)
95 return (false); 91 return (false);
96 92
97 r = read(fd, buffer, sizeof (buffer)); 93 r = read(fd, buffer, sizeof (buffer));
98 close (fd); 94 close (fd);
99 95
100 if (r == -1) 96 if (r == -1)
101 return 0; 97 return 0;
102 98
103 buffer[r] = 0; 99 buffer[r] = 0;
104 return (strcmp (exec, buffer) == 0 ? true : false); 100 return (strcmp (exec, buffer) == 0 ? true : false);
105} 101}
106 102
107pid_t *rc_find_pids (const char *exec, const char *cmd, 103pid_t *rc_find_pids (const char *exec, const char *cmd,
108 uid_t uid, pid_t pid) 104 uid_t uid, pid_t pid)
109{ 105{
110 DIR *procdir; 106 DIR *procdir;
111 struct dirent *entry; 107 struct dirent *entry;
112 int npids = 0; 108 int npids = 0;
113 int foundany = false; 109 int foundany = false;
114 pid_t p; 110 pid_t p;
115 pid_t *pids = NULL; 111 pid_t *pids = NULL;
116 char buffer[PATH_MAX]; 112 char buffer[PATH_MAX];
117 struct stat sb; 113 struct stat sb;
118 pid_t runscript_pid = 0; 114 pid_t runscript_pid = 0;
119 char *pp; 115 char *pp;
120 116
121 if ((procdir = opendir ("/proc")) == NULL) 117 if ((procdir = opendir ("/proc")) == NULL)
122 eerrorx ("opendir `/proc': %s", strerror (errno)); 118 eerrorx ("opendir `/proc': %s", strerror (errno));
123 119
124 /* 120 /*
125 We never match RC_RUNSCRIPT_PID if present so we avoid the below 121 We never match RC_RUNSCRIPT_PID if present so we avoid the below
126 scenario 122 scenario
127 123
128 /etc/init.d/ntpd stop does 124 /etc/init.d/ntpd stop does
129 start-stop-daemon --stop --name ntpd 125 start-stop-daemon --stop --name ntpd
130 catching /etc/init.d/ntpd stop 126 catching /etc/init.d/ntpd stop
131 127
132 nasty 128 nasty
133 */ 129 */
134 130
135 if ((pp = getenv ("RC_RUNSCRIPT_PID"))) 131 if ((pp = getenv ("RC_RUNSCRIPT_PID"))) {
136 {
137 if (sscanf (pp, "%d", &runscript_pid) != 1) 132 if (sscanf (pp, "%d", &runscript_pid) != 1)
138 runscript_pid = 0; 133 runscript_pid = 0;
139 } 134 }
140 135
141 while ((entry = readdir (procdir)) != NULL) 136 while ((entry = readdir (procdir)) != NULL) {
142 {
143 if (sscanf (entry->d_name, "%d", &p) != 1) 137 if (sscanf (entry->d_name, "%d", &p) != 1)
144 continue; 138 continue;
145 foundany = true; 139 foundany = true;
146 140
147 if (runscript_pid != 0 && runscript_pid == p) 141 if (runscript_pid != 0 && runscript_pid == p)
148 continue; 142 continue;
149 143
150 if (pid != 0 && pid != p) 144 if (pid != 0 && pid != p)
151 continue; 145 continue;
152 146
153 if (uid) 147 if (uid) {
154 {
155 snprintf (buffer, sizeof (buffer), "/proc/%d", pid); 148 snprintf (buffer, sizeof (buffer), "/proc/%d", pid);
156 if (stat (buffer, &sb) != 0 || sb.st_uid != uid) 149 if (stat (buffer, &sb) != 0 || sb.st_uid != uid)
157 continue; 150 continue;
158 } 151 }
159 152
160 if (cmd && ! pid_is_cmd (p, cmd)) 153 if (cmd && ! pid_is_cmd (p, cmd))
161 continue; 154 continue;
162 155
163 if (exec && ! cmd && ! pid_is_exec (p, exec)) 156 if (exec && ! cmd && ! pid_is_exec (p, exec))
164 continue; 157 continue;
165 158
166 pids = realloc (pids, sizeof (pid_t) * (npids + 2)); 159 pids = realloc (pids, sizeof (pid_t) * (npids + 2));
167 if (! pids) 160 if (! pids)
168 eerrorx ("memory exhausted"); 161 eerrorx ("memory exhausted");
169 162
170 pids[npids] = p; 163 pids[npids] = p;
171 pids[npids + 1] = 0; 164 pids[npids + 1] = 0;
172 npids++; 165 npids++;
173 } 166 }
174 closedir (procdir); 167 closedir (procdir);
175 168
176 if (! foundany) 169 if (! foundany)
177 eerrorx ("nothing in /proc"); 170 eerrorx ("nothing in /proc");
178 171
179 return (pids); 172 return (pids);
180} 173}
181 174
182#elif defined(__DragonFly__) || defined(__FreeBSD__) || \ 175#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
183 defined(__NetBSD__) || defined(__OpenBSD__) 176 defined(__NetBSD__) || defined(__OpenBSD__)
184 177
185# if defined(__DragonFly__) || defined(__FreeBSD__) 178# if defined(__DragonFly__) || defined(__FreeBSD__)
186# ifndef KERN_PROC_PROC 179# ifndef KERN_PROC_PROC
187# define KERN_PROC_PROC KERN_PROC_ALL 180# define KERN_PROC_PROC KERN_PROC_ALL
188# endif 181# endif
198# define _GET_KINFO_COMM(kp) (kp.p_comm) 191# define _GET_KINFO_COMM(kp) (kp.p_comm)
199# define _GET_KINFO_PID(kp) (kp.p_pid) 192# define _GET_KINFO_PID(kp) (kp.p_pid)
200# endif 193# endif
201 194
202pid_t *rc_find_pids (const char *exec, const char *cmd, 195pid_t *rc_find_pids (const char *exec, const char *cmd,
203 uid_t uid, pid_t pid) 196 uid_t uid, pid_t pid)
204{ 197{
205 static kvm_t *kd = NULL; 198 static kvm_t *kd = NULL;
206 char errbuf[_POSIX2_LINE_MAX]; 199 char errbuf[_POSIX2_LINE_MAX];
207 struct _KINFO_PROC *kp; 200 struct _KINFO_PROC *kp;
208 int i; 201 int i;
209 int processes = 0; 202 int processes = 0;
210 int argc = 0; 203 int argc = 0;
211 char **argv; 204 char **argv;
212 pid_t *pids = NULL; 205 pid_t *pids = NULL;
213 int npids = 0; 206 int npids = 0;
214 207
215 if ((kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL) 208 if ((kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL)
216 eerrorx ("kvm_open: %s", errbuf); 209 eerrorx ("kvm_open: %s", errbuf);
217 210
218#if defined(__DragonFly__) || defined( __FreeBSD__) 211#if defined(__DragonFly__) || defined( __FreeBSD__)
219 kp = kvm_getprocs (kd, KERN_PROC_PROC, 0, &processes); 212 kp = kvm_getprocs (kd, KERN_PROC_PROC, 0, &processes);
220#else 213#else
221 kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), 214 kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
222 &processes); 215 &processes);
223#endif 216#endif
224 for (i = 0; i < processes; i++) 217 for (i = 0; i < processes; i++) {
225 {
226 pid_t p = _GET_KINFO_PID (kp[i]); 218 pid_t p = _GET_KINFO_PID (kp[i]);
227 if (pid != 0 && pid != p) 219 if (pid != 0 && pid != p)
228 continue; 220 continue;
229 221
230 if (uid != 0 && uid != _GET_KINFO_UID (kp[i])) 222 if (uid != 0 && uid != _GET_KINFO_UID (kp[i]))
231 continue; 223 continue;
232 224
233 if (cmd) 225 if (cmd) {
234 {
235 if (! _GET_KINFO_COMM (kp[i]) || 226 if (! _GET_KINFO_COMM (kp[i]) ||
236 strcmp (cmd, _GET_KINFO_COMM (kp[i])) != 0) 227 strcmp (cmd, _GET_KINFO_COMM (kp[i])) != 0)
237 continue; 228 continue;
238 } 229 }
239 230
240 if (exec && ! cmd) 231 if (exec && ! cmd) {
241 {
242 if ((argv = _KVM_GETARGV (kd, &kp[i], argc)) == NULL || ! *argv) 232 if ((argv = _KVM_GETARGV (kd, &kp[i], argc)) == NULL || ! *argv)
243 continue; 233 continue;
244 234
245 if (strcmp (*argv, exec) != 0) 235 if (strcmp (*argv, exec) != 0)
246 continue; 236 continue;
247 } 237 }
248 238
249 pids = realloc (pids, sizeof (pid_t) * (npids + 2)); 239 pids = realloc (pids, sizeof (pid_t) * (npids + 2));
250 if (! pids) 240 if (! pids)
251 eerrorx ("memory exhausted"); 241 eerrorx ("memory exhausted");
252 242
253 pids[npids] = p; 243 pids[npids] = p;
254 pids[npids + 1] = 0; 244 pids[npids + 1] = 0;
255 npids++; 245 npids++;
256 } 246 }
257 kvm_close(kd); 247 kvm_close(kd);
258 248
259 return (pids); 249 return (pids);
260} 250}
261 251
262#else 252#else
263# error "Platform not supported!" 253# error "Platform not supported!"
264#endif 254#endif
265 255
266static bool _match_daemon (const char *path, const char *file, 256static bool _match_daemon (const char *path, const char *file,
267 const char *mexec, const char *mname, 257 const char *mexec, const char *mname,
268 const char *mpidfile) 258 const char *mpidfile)
269{ 259{
270 char buffer[RC_LINEBUFFER]; 260 char buffer[RC_LINEBUFFER];
271 char *ffile = rc_strcatpaths (path, file, (char *) NULL); 261 char *ffile = rc_strcatpaths (path, file, (char *) NULL);
272 FILE *fp; 262 FILE *fp;
273 int lc = 0; 263 int lc = 0;
274 int m = 0; 264 int m = 0;
275 265
276 if (! rc_exists (ffile)) 266 if (! rc_exists (ffile)) {
277 {
278 free (ffile); 267 free (ffile);
279 return (false); 268 return (false);
280 } 269 }
281 270
282 if ((fp = fopen (ffile, "r")) == NULL) 271 if ((fp = fopen (ffile, "r")) == NULL) {
283 {
284 eerror ("fopen `%s': %s", ffile, strerror (errno)); 272 eerror ("fopen `%s': %s", ffile, strerror (errno));
285 free (ffile); 273 free (ffile);
286 return (false); 274 return (false);
287 } 275 }
288 276
289 if (! mname) 277 if (! mname)
290 m += 10; 278 m += 10;
291 if (! mpidfile) 279 if (! mpidfile)
292 m += 100; 280 m += 100;
293 281
294 memset (buffer, 0, sizeof (buffer)); 282 memset (buffer, 0, sizeof (buffer));
295 while ((fgets (buffer, RC_LINEBUFFER, fp))) 283 while ((fgets (buffer, RC_LINEBUFFER, fp))) {
296 {
297 int lb = strlen (buffer) - 1; 284 int lb = strlen (buffer) - 1;
298 if (buffer[lb] == '\n') 285 if (buffer[lb] == '\n')
299 buffer[lb] = 0; 286 buffer[lb] = 0;
300 287
301 if (strcmp (buffer, mexec) == 0) 288 if (strcmp (buffer, mexec) == 0)
302 m += 1; 289 m += 1;
303 else if (mname && strcmp (buffer, mname) == 0) 290 else if (mname && strcmp (buffer, mname) == 0)
304 m += 10; 291 m += 10;
305 else if (mpidfile && strcmp (buffer, mpidfile) == 0) 292 else if (mpidfile && strcmp (buffer, mpidfile) == 0)
306 m += 100; 293 m += 100;
307 294
308 if (m == 111) 295 if (m == 111)
309 break; 296 break;
310 297
311 lc++; 298 lc++;
312 if (lc > 5) 299 if (lc > 5)
313 break; 300 break;
314 } 301 }
315 fclose (fp); 302 fclose (fp);
316 free (ffile); 303 free (ffile);
317 304
318 return (m == 111 ? true : false); 305 return (m == 111 ? true : false);
319} 306}
320 307
321void rc_set_service_daemon (const char *service, const char *exec, 308void rc_set_service_daemon (const char *service, const char *exec,
322 const char *name, const char *pidfile, 309 const char *name, const char *pidfile,
323 bool started) 310 bool started)
324{ 311{
325 char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service), 312 char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
326 (char *) NULL); 313 (char *) NULL);
327 char **files = NULL; 314 char **files = NULL;
328 char *file; 315 char *file;
329 char *ffile = NULL; 316 char *ffile = NULL;
330 int i; 317 int i;
331 char *mexec; 318 char *mexec;
332 char *mname; 319 char *mname;
333 char *mpidfile; 320 char *mpidfile;
334 int nfiles = 0; 321 int nfiles = 0;
335 322
336 if (! exec && ! name && ! pidfile) 323 if (! exec && ! name && ! pidfile)
337 return; 324 return;
338 325
339 if (exec) 326 if (exec) {
340 {
341 i = strlen (exec) + 6; 327 i = strlen (exec) + 6;
342 mexec = rc_xmalloc (sizeof (char *) * i); 328 mexec = rc_xmalloc (sizeof (char *) * i);
343 snprintf (mexec, i, "exec=%s", exec); 329 snprintf (mexec, i, "exec=%s", exec);
344 }
345 else 330 } else
346 mexec = strdup ("exec="); 331 mexec = strdup ("exec=");
347 332
348 if (name) 333 if (name) {
349 {
350 i = strlen (name) + 6; 334 i = strlen (name) + 6;
351 mname = rc_xmalloc (sizeof (char *) * i); 335 mname = rc_xmalloc (sizeof (char *) * i);
352 snprintf (mname, i, "name=%s", name); 336 snprintf (mname, i, "name=%s", name);
353 }
354 else 337 } else
355 mname = strdup ("name="); 338 mname = strdup ("name=");
356 339
357 if (pidfile) 340 if (pidfile) {
358 {
359 i = strlen (pidfile) + 9; 341 i = strlen (pidfile) + 9;
360 mpidfile = rc_xmalloc (sizeof (char *) * i); 342 mpidfile = rc_xmalloc (sizeof (char *) * i);
361 snprintf (mpidfile, i, "pidfile=%s", pidfile); 343 snprintf (mpidfile, i, "pidfile=%s", pidfile);
362 }
363 else 344 } else
364 mpidfile = strdup ("pidfile="); 345 mpidfile = strdup ("pidfile=");
365 346
366 /* Regardless, erase any existing daemon info */ 347 /* Regardless, erase any existing daemon info */
367 if (rc_is_dir (dirpath)) 348 if (rc_is_dir (dirpath)) {
368 {
369 char *oldfile = NULL; 349 char *oldfile = NULL;
370 files = rc_ls_dir (NULL, dirpath, 0); 350 files = rc_ls_dir (NULL, dirpath, 0);
371 STRLIST_FOREACH (files, file, i) 351 STRLIST_FOREACH (files, file, i) {
372 {
373 ffile = rc_strcatpaths (dirpath, file, (char *) NULL); 352 ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
374 nfiles++; 353 nfiles++;
375 354
376 if (! oldfile) 355 if (! oldfile) {
377 {
378 if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) 356 if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) {
379 { 357 unlink (ffile);
380 unlink (ffile); 358 oldfile = ffile;
381 oldfile = ffile; 359 nfiles--;
382 nfiles--; 360 }
383 } 361 } else {
384 } 362 rename (ffile, oldfile);
385 else 363 free (oldfile);
386 { 364 oldfile = ffile;
387 rename (ffile, oldfile); 365 }
388 free (oldfile); 366 }
389 oldfile = ffile; 367 if (ffile)
390 } 368 free (ffile);
391 }
392 if (ffile)
393 free (ffile);
394 free (files); 369 free (files);
395 } 370 }
396 371
397 /* Now store our daemon info */ 372 /* Now store our daemon info */
398 if (started) 373 if (started) {
399 {
400 char buffer[10]; 374 char buffer[10];
401 FILE *fp; 375 FILE *fp;
402 376
403 if (! rc_is_dir (dirpath)) 377 if (! rc_is_dir (dirpath))
404 if (mkdir (dirpath, 0755) != 0) 378 if (mkdir (dirpath, 0755) != 0)
405 eerror ("mkdir `%s': %s", dirpath, strerror (errno)); 379 eerror ("mkdir `%s': %s", dirpath, strerror (errno));
406 380
407 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1); 381 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1);
408 file = rc_strcatpaths (dirpath, buffer, (char *) NULL); 382 file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
409 if ((fp = fopen (file, "w")) == NULL) 383 if ((fp = fopen (file, "w")) == NULL)
410 eerror ("fopen `%s': %s", file, strerror (errno)); 384 eerror ("fopen `%s': %s", file, strerror (errno));
411 else 385 else {
412 {
413 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile); 386 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
414 fclose (fp); 387 fclose (fp);
415 } 388 }
416 free (file); 389 free (file);
417 } 390 }
418 391
419 free (mexec); 392 free (mexec);
420 free (mname); 393 free (mname);
421 free (mpidfile); 394 free (mpidfile);
422 free (dirpath); 395 free (dirpath);
423} 396}
424 397
425bool rc_service_started_daemon (const char *service, const char *exec, 398bool rc_service_started_daemon (const char *service, const char *exec,
426 int indx) 399 int indx)
427{ 400{
428 char *dirpath; 401 char *dirpath;
429 char *file; 402 char *file;
430 int i; 403 int i;
431 char *mexec; 404 char *mexec;
432 bool retval = false; 405 bool retval = false;
433 406
434 if (! service || ! exec) 407 if (! service || ! exec)
435 return (false); 408 return (false);
436 409
437 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service), 410 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
438 (char *) NULL); 411 (char *) NULL);
439 if (! rc_is_dir (dirpath)) 412 if (! rc_is_dir (dirpath)) {
440 {
441 free (dirpath); 413 free (dirpath);
442 return (false); 414 return (false);
443 } 415 }
444 416
445 i = strlen (exec) + 6; 417 i = strlen (exec) + 6;
446 mexec = rc_xmalloc (sizeof (char *) * i); 418 mexec = rc_xmalloc (sizeof (char *) * i);
447 snprintf (mexec, i, "exec=%s", exec); 419 snprintf (mexec, i, "exec=%s", exec);
448 420
449 if (indx > 0) 421 if (indx > 0) {
450 {
451 int len = sizeof (char *) * 10; 422 int len = sizeof (char *) * 10;
452 file = rc_xmalloc (len); 423 file = rc_xmalloc (len);
453 snprintf (file, len, "%03d", indx); 424 snprintf (file, len, "%03d", indx);
454 retval = _match_daemon (dirpath, file, mexec, NULL, NULL); 425 retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
455 free (file); 426 free (file);
456 } 427 } else {
457 else
458 {
459 char **files = rc_ls_dir (NULL, dirpath, 0); 428 char **files = rc_ls_dir (NULL, dirpath, 0);
460 STRLIST_FOREACH (files, file, i) 429 STRLIST_FOREACH (files, file, i) {
461 {
462 retval = _match_daemon (dirpath, file, mexec, NULL, NULL); 430 retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
463 if (retval) 431 if (retval)
464 break; 432 break;
465 } 433 }
466 free (files); 434 free (files);
467 } 435 }
468 436
469 free (mexec); 437 free (mexec);
470 return (retval); 438 return (retval);
471} 439}
472 440
473bool rc_service_daemons_crashed (const char *service) 441bool rc_service_daemons_crashed (const char *service)
474{ 442{
475 char *dirpath; 443 char *dirpath;
476 char **files; 444 char **files;
477 char *file; 445 char *file;
478 char *path; 446 char *path;
479 int i; 447 int i;
480 FILE *fp; 448 FILE *fp;
481 char buffer[RC_LINEBUFFER]; 449 char buffer[RC_LINEBUFFER];
482 char *exec = NULL; 450 char *exec = NULL;
483 char *name = NULL; 451 char *name = NULL;
484 char *pidfile = NULL; 452 char *pidfile = NULL;
485 pid_t pid = 0; 453 pid_t pid = 0;
486 pid_t *pids = NULL; 454 pid_t *pids = NULL;
487 char *p; 455 char *p;
488 char *token; 456 char *token;
489 bool retval = false; 457 bool retval = false;
490 458
491 if (! service) 459 if (! service)
492 return (false); 460 return (false);
493 461
494 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service), 462 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
495 (char *) NULL); 463 (char *) NULL);
496 if (! rc_is_dir (dirpath)) 464 if (! rc_is_dir (dirpath)) {
497 {
498 free (dirpath); 465 free (dirpath);
499 return (false); 466 return (false);
500 } 467 }
501 468
502 memset (buffer, 0, sizeof (buffer)); 469 memset (buffer, 0, sizeof (buffer));
503 files = rc_ls_dir (NULL, dirpath, 0); 470 files = rc_ls_dir (NULL, dirpath, 0);
504 STRLIST_FOREACH (files, file, i) 471 STRLIST_FOREACH (files, file, i) {
505 {
506 path = rc_strcatpaths (dirpath, file, (char *) NULL); 472 path = rc_strcatpaths (dirpath, file, (char *) NULL);
507 fp = fopen (path, "r"); 473 fp = fopen (path, "r");
508 free (path); 474 free (path);
509 if (! fp) 475 if (! fp) {
510 {
511 eerror ("fopen `%s': %s", file, strerror (errno)); 476 eerror ("fopen `%s': %s", file, strerror (errno));
512 continue; 477 continue;
513 } 478 }
514 479
515 while ((fgets (buffer, RC_LINEBUFFER, fp))) 480 while ((fgets (buffer, RC_LINEBUFFER, fp))) {
516 {
517 int lb = strlen (buffer) - 1; 481 int lb = strlen (buffer) - 1;
518 if (buffer[lb] == '\n') 482 if (buffer[lb] == '\n')
519 buffer[lb] = 0; 483 buffer[lb] = 0;
520 484
521 p = buffer; 485 p = buffer;
522 if ((token = strsep (&p, "=")) == NULL || ! p) 486 if ((token = strsep (&p, "=")) == NULL || ! p)
523 continue; 487 continue;
524 488
525 if (strlen (p) == 0) 489 if (strlen (p) == 0)
526 continue; 490 continue;
527 491
528 if (strcmp (token, "exec") == 0) 492 if (strcmp (token, "exec") == 0) {
529 { 493 if (exec)
530 if (exec) 494 free (exec);
531 free (exec); 495 exec = strdup (p);
532 exec = strdup (p);
533 }
534 else if (strcmp (token, "name") == 0) 496 } else if (strcmp (token, "name") == 0) {
535 { 497 if (name)
536 if (name) 498 free (name);
537 free (name); 499 name = strdup (p);
538 name = strdup (p);
539 }
540 else if (strcmp (token, "pidfile") == 0) 500 } else if (strcmp (token, "pidfile") == 0) {
541 { 501 if (pidfile)
542 if (pidfile) 502 free (pidfile);
543 free (pidfile); 503 pidfile = strdup (p);
544 pidfile = strdup (p); 504 }
545 } 505 }
546 }
547 fclose (fp); 506 fclose (fp);
548 507
549 pid = 0; 508 pid = 0;
550 if (pidfile) 509 if (pidfile) {
551 {
552 if (! rc_exists (pidfile)) 510 if (! rc_exists (pidfile)) {
553 { 511 retval = true;
554 retval = true; 512 break;
555 break; 513 }
556 }
557 514
558 if ((fp = fopen (pidfile, "r")) == NULL) 515 if ((fp = fopen (pidfile, "r")) == NULL) {
559 {
560 eerror ("fopen `%s': %s", pidfile, strerror (errno)); 516 eerror ("fopen `%s': %s", pidfile, strerror (errno));
561 retval = true; 517 retval = true;
562 break; 518 break;
563 } 519 }
564 520
565 if (fscanf (fp, "%d", &pid) != 1) 521 if (fscanf (fp, "%d", &pid) != 1) {
566 {
567 eerror ("no pid found in `%s'", pidfile); 522 eerror ("no pid found in `%s'", pidfile);
568 fclose (fp); 523 fclose (fp);
569 retval = true; 524 retval = true;
570 break; 525 break;
571 } 526 }
572 527
573 fclose (fp); 528 fclose (fp);
574 free (pidfile); 529 free (pidfile);
575 pidfile = NULL; 530 pidfile = NULL;
576 } 531 }
577 532
578 if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) 533 if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) {
579 { 534 retval = true;
580 retval = true; 535 break;
581 break; 536 }
582 }
583 free (pids); 537 free (pids);
584 538
585 if (exec) 539 if (exec) {
586 { 540 free (exec);
587 free (exec); 541 exec = NULL;
588 exec = NULL; 542 }
589 } 543 if (name) {
590 if (name) 544 free (name);
591 { 545 name = NULL;
592 free (name); 546 }
593 name = NULL; 547 }
594 }
595 }
596 548
597 if (exec) 549 if (exec) {
598 {
599 free (exec); 550 free (exec);
600 exec = NULL; 551 exec = NULL;
601 } 552 }
602 if (name) 553 if (name) {
603 {
604 free (name); 554 free (name);
605 name = NULL; 555 name = NULL;
606 } 556 }
607 557
608 free (dirpath); 558 free (dirpath);
609 rc_strlist_free (files); 559 rc_strlist_free (files);
610 560
611 return (retval); 561 return (retval);
612} 562}

Legend:
Removed from v.2569  
changed lines
  Added in v.2577

  ViewVC Help
Powered by ViewVC 1.1.20