/[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 2550 Revision 2569
73 snprintf (cmdline, sizeof (cmdline), "/proc/%u/exe", pid); 73 snprintf (cmdline, sizeof (cmdline), "/proc/%u/exe", pid);
74 memset (buffer, 0, sizeof (buffer)); 74 memset (buffer, 0, sizeof (buffer));
75 if (readlink (cmdline, buffer, sizeof (buffer)) != -1) 75 if (readlink (cmdline, buffer, sizeof (buffer)) != -1)
76 { 76 {
77 if (strcmp (exec, buffer) == 0) 77 if (strcmp (exec, buffer) == 0)
78 return (true); 78 return (true);
79 79
80 /* We should cater for deleted binaries too */ 80 /* We should cater for deleted binaries too */
81 if (strlen (buffer) > 10) 81 if (strlen (buffer) > 10)
82 { 82 {
83 p = buffer + (strlen (buffer) - 10); 83 p = buffer + (strlen (buffer) - 10);
84 if (strcmp (p, " (deleted)") == 0) 84 if (strcmp (p, " (deleted)") == 0)
85 { 85 {
86 *p = 0; 86 *p = 0;
87 if (strcmp (buffer, exec) == 0) 87 if (strcmp (buffer, exec) == 0)
88 return (true); 88 return (true);
89 } 89 }
90 } 90 }
91 } 91 }
92 92
93 snprintf (cmdline, sizeof (cmdline), "/proc/%u/cmdline", pid); 93 snprintf (cmdline, sizeof (cmdline), "/proc/%u/cmdline", pid);
94 if ((fd = open (cmdline, O_RDONLY)) < 0) 94 if ((fd = open (cmdline, O_RDONLY)) < 0)
95 return (false); 95 return (false);
103 buffer[r] = 0; 103 buffer[r] = 0;
104 return (strcmp (exec, buffer) == 0 ? true : false); 104 return (strcmp (exec, buffer) == 0 ? true : false);
105} 105}
106 106
107pid_t *rc_find_pids (const char *exec, const char *cmd, 107pid_t *rc_find_pids (const char *exec, const char *cmd,
108 uid_t uid, pid_t pid) 108 uid_t uid, pid_t pid)
109{ 109{
110 DIR *procdir; 110 DIR *procdir;
111 struct dirent *entry; 111 struct dirent *entry;
112 int npids = 0; 112 int npids = 0;
113 int foundany = false; 113 int foundany = false;
128 /etc/init.d/ntpd stop does 128 /etc/init.d/ntpd stop does
129 start-stop-daemon --stop --name ntpd 129 start-stop-daemon --stop --name ntpd
130 catching /etc/init.d/ntpd stop 130 catching /etc/init.d/ntpd stop
131 131
132 nasty 132 nasty
133 */ 133 */
134 134
135 if ((pp = getenv ("RC_RUNSCRIPT_PID"))) 135 if ((pp = getenv ("RC_RUNSCRIPT_PID")))
136 { 136 {
137 if (sscanf (pp, "%d", &runscript_pid) != 1) 137 if (sscanf (pp, "%d", &runscript_pid) != 1)
138 runscript_pid = 0; 138 runscript_pid = 0;
139 } 139 }
140 140
141 while ((entry = readdir (procdir)) != NULL) 141 while ((entry = readdir (procdir)) != NULL)
142 { 142 {
143 if (sscanf (entry->d_name, "%d", &p) != 1) 143 if (sscanf (entry->d_name, "%d", &p) != 1)
144 continue; 144 continue;
145 foundany = true; 145 foundany = true;
146 146
147 if (runscript_pid != 0 && runscript_pid == p) 147 if (runscript_pid != 0 && runscript_pid == p)
148 continue; 148 continue;
149 149
150 if (pid != 0 && pid != p) 150 if (pid != 0 && pid != p)
151 continue; 151 continue;
152 152
153 if (uid) 153 if (uid)
154 { 154 {
155 snprintf (buffer, sizeof (buffer), "/proc/%d", pid); 155 snprintf (buffer, sizeof (buffer), "/proc/%d", pid);
156 if (stat (buffer, &sb) != 0 || sb.st_uid != uid) 156 if (stat (buffer, &sb) != 0 || sb.st_uid != uid)
157 continue; 157 continue;
158 } 158 }
159 159
160 if (cmd && ! pid_is_cmd (p, cmd)) 160 if (cmd && ! pid_is_cmd (p, cmd))
161 continue; 161 continue;
162 162
163 if (exec && ! cmd && ! pid_is_exec (p, exec)) 163 if (exec && ! cmd && ! pid_is_exec (p, exec))
164 continue; 164 continue;
165 165
166 pids = realloc (pids, sizeof (pid_t) * (npids + 2)); 166 pids = realloc (pids, sizeof (pid_t) * (npids + 2));
167 if (! pids) 167 if (! pids)
168 eerrorx ("memory exhausted"); 168 eerrorx ("memory exhausted");
169 169
170 pids[npids] = p; 170 pids[npids] = p;
171 pids[npids + 1] = 0; 171 pids[npids + 1] = 0;
172 npids++; 172 npids++;
173 } 173 }
198# define _GET_KINFO_COMM(kp) (kp.p_comm) 198# define _GET_KINFO_COMM(kp) (kp.p_comm)
199# define _GET_KINFO_PID(kp) (kp.p_pid) 199# define _GET_KINFO_PID(kp) (kp.p_pid)
200# endif 200# endif
201 201
202pid_t *rc_find_pids (const char *exec, const char *cmd, 202pid_t *rc_find_pids (const char *exec, const char *cmd,
203 uid_t uid, pid_t pid) 203 uid_t uid, pid_t pid)
204{ 204{
205 static kvm_t *kd = NULL; 205 static kvm_t *kd = NULL;
206 char errbuf[_POSIX2_LINE_MAX]; 206 char errbuf[_POSIX2_LINE_MAX];
207 struct _KINFO_PROC *kp; 207 struct _KINFO_PROC *kp;
208 int i; 208 int i;
217 217
218#if defined(__DragonFly__) || defined( __FreeBSD__) 218#if defined(__DragonFly__) || defined( __FreeBSD__)
219 kp = kvm_getprocs (kd, KERN_PROC_PROC, 0, &processes); 219 kp = kvm_getprocs (kd, KERN_PROC_PROC, 0, &processes);
220#else 220#else
221 kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), 221 kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
222 &processes); 222 &processes);
223#endif 223#endif
224 for (i = 0; i < processes; i++) 224 for (i = 0; i < processes; i++)
225 { 225 {
226 pid_t p = _GET_KINFO_PID (kp[i]); 226 pid_t p = _GET_KINFO_PID (kp[i]);
227 if (pid != 0 && pid != p) 227 if (pid != 0 && pid != p)
228 continue; 228 continue;
229 229
230 if (uid != 0 && uid != _GET_KINFO_UID (kp[i])) 230 if (uid != 0 && uid != _GET_KINFO_UID (kp[i]))
231 continue; 231 continue;
232 232
233 if (cmd) 233 if (cmd)
234 { 234 {
235 if (! _GET_KINFO_COMM (kp[i]) || 235 if (! _GET_KINFO_COMM (kp[i]) ||
236 strcmp (cmd, _GET_KINFO_COMM (kp[i])) != 0) 236 strcmp (cmd, _GET_KINFO_COMM (kp[i])) != 0)
237 continue; 237 continue;
238 } 238 }
239 239
240 if (exec && ! cmd) 240 if (exec && ! cmd)
241 { 241 {
242 if ((argv = _KVM_GETARGV (kd, &kp[i], argc)) == NULL || ! *argv) 242 if ((argv = _KVM_GETARGV (kd, &kp[i], argc)) == NULL || ! *argv)
243 continue; 243 continue;
244 244
245 if (strcmp (*argv, exec) != 0) 245 if (strcmp (*argv, exec) != 0)
246 continue; 246 continue;
247 } 247 }
248 248
249 pids = realloc (pids, sizeof (pid_t) * (npids + 2)); 249 pids = realloc (pids, sizeof (pid_t) * (npids + 2));
250 if (! pids) 250 if (! pids)
251 eerrorx ("memory exhausted"); 251 eerrorx ("memory exhausted");
252 252
253 pids[npids] = p; 253 pids[npids] = p;
254 pids[npids + 1] = 0; 254 pids[npids + 1] = 0;
255 npids++; 255 npids++;
256 } 256 }
262#else 262#else
263# error "Platform not supported!" 263# error "Platform not supported!"
264#endif 264#endif
265 265
266static bool _match_daemon (const char *path, const char *file, 266static bool _match_daemon (const char *path, const char *file,
267 const char *mexec, const char *mname, 267 const char *mexec, const char *mname,
268 const char *mpidfile) 268 const char *mpidfile)
269{ 269{
270 char buffer[RC_LINEBUFFER]; 270 char buffer[RC_LINEBUFFER];
271 char *ffile = rc_strcatpaths (path, file, (char *) NULL); 271 char *ffile = rc_strcatpaths (path, file, (char *) NULL);
272 FILE *fp; 272 FILE *fp;
273 int lc = 0; 273 int lc = 0;
294 memset (buffer, 0, sizeof (buffer)); 294 memset (buffer, 0, sizeof (buffer));
295 while ((fgets (buffer, RC_LINEBUFFER, fp))) 295 while ((fgets (buffer, RC_LINEBUFFER, fp)))
296 { 296 {
297 int lb = strlen (buffer) - 1; 297 int lb = strlen (buffer) - 1;
298 if (buffer[lb] == '\n') 298 if (buffer[lb] == '\n')
299 buffer[lb] = 0; 299 buffer[lb] = 0;
300 300
301 if (strcmp (buffer, mexec) == 0) 301 if (strcmp (buffer, mexec) == 0)
302 m += 1; 302 m += 1;
303 else if (mname && strcmp (buffer, mname) == 0) 303 else if (mname && strcmp (buffer, mname) == 0)
304 m += 10; 304 m += 10;
305 else if (mpidfile && strcmp (buffer, mpidfile) == 0) 305 else if (mpidfile && strcmp (buffer, mpidfile) == 0)
306 m += 100; 306 m += 100;
307 307
308 if (m == 111) 308 if (m == 111)
309 break; 309 break;
310 310
311 lc++; 311 lc++;
312 if (lc > 5) 312 if (lc > 5)
313 break; 313 break;
314 } 314 }
315 fclose (fp); 315 fclose (fp);
316 free (ffile); 316 free (ffile);
317 317
318 return (m == 111 ? true : false); 318 return (m == 111 ? true : false);
319} 319}
320 320
321void rc_set_service_daemon (const char *service, const char *exec, 321void rc_set_service_daemon (const char *service, const char *exec,
322 const char *name, const char *pidfile, 322 const char *name, const char *pidfile,
323 bool started) 323 bool started)
324{ 324{
325 char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service), 325 char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
326 (char *) NULL); 326 (char *) NULL);
327 char **files = NULL; 327 char **files = NULL;
328 char *file; 328 char *file;
329 char *ffile = NULL; 329 char *ffile = NULL;
330 int i; 330 int i;
331 char *mexec; 331 char *mexec;
367 if (rc_is_dir (dirpath)) 367 if (rc_is_dir (dirpath))
368 { 368 {
369 char *oldfile = NULL; 369 char *oldfile = NULL;
370 files = rc_ls_dir (NULL, dirpath, 0); 370 files = rc_ls_dir (NULL, dirpath, 0);
371 STRLIST_FOREACH (files, file, i) 371 STRLIST_FOREACH (files, file, i)
372 { 372 {
373 ffile = rc_strcatpaths (dirpath, file, (char *) NULL); 373 ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
374 nfiles++; 374 nfiles++;
375 375
376 if (! oldfile) 376 if (! oldfile)
377 { 377 {
378 if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) 378 if (_match_daemon (dirpath, file, mexec, mname, mpidfile))
379 { 379 {
380 unlink (ffile); 380 unlink (ffile);
381 oldfile = ffile; 381 oldfile = ffile;
382 nfiles--; 382 nfiles--;
383 } 383 }
384 } 384 }
385 else 385 else
386 { 386 {
387 rename (ffile, oldfile); 387 rename (ffile, oldfile);
388 free (oldfile); 388 free (oldfile);
389 oldfile = ffile; 389 oldfile = ffile;
390 } 390 }
391 } 391 }
392 if (ffile) 392 if (ffile)
393 free (ffile); 393 free (ffile);
394 free (files); 394 free (files);
395 } 395 }
396 396
397 /* Now store our daemon info */ 397 /* Now store our daemon info */
398 if (started) 398 if (started)
399 { 399 {
400 char buffer[10]; 400 char buffer[10];
401 FILE *fp; 401 FILE *fp;
402 402
403 if (! rc_is_dir (dirpath)) 403 if (! rc_is_dir (dirpath))
404 if (mkdir (dirpath, 0755) != 0) 404 if (mkdir (dirpath, 0755) != 0)
405 eerror ("mkdir `%s': %s", dirpath, strerror (errno)); 405 eerror ("mkdir `%s': %s", dirpath, strerror (errno));
406 406
407 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1); 407 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1);
408 file = rc_strcatpaths (dirpath, buffer, (char *) NULL); 408 file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
409 if ((fp = fopen (file, "w")) == NULL) 409 if ((fp = fopen (file, "w")) == NULL)
410 eerror ("fopen `%s': %s", file, strerror (errno)); 410 eerror ("fopen `%s': %s", file, strerror (errno));
411 else 411 else
412 { 412 {
413 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile); 413 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
414 fclose (fp); 414 fclose (fp);
415 } 415 }
416 free (file); 416 free (file);
417 } 417 }
418 418
419 free (mexec); 419 free (mexec);
420 free (mname); 420 free (mname);
421 free (mpidfile); 421 free (mpidfile);
422 free (dirpath); 422 free (dirpath);
423} 423}
424 424
425bool rc_service_started_daemon (const char *service, const char *exec, 425bool rc_service_started_daemon (const char *service, const char *exec,
426 int indx) 426 int indx)
427{ 427{
428 char *dirpath; 428 char *dirpath;
429 char *file; 429 char *file;
430 int i; 430 int i;
431 char *mexec; 431 char *mexec;
433 433
434 if (! service || ! exec) 434 if (! service || ! exec)
435 return (false); 435 return (false);
436 436
437 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service), 437 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
438 (char *) NULL); 438 (char *) NULL);
439 if (! rc_is_dir (dirpath)) 439 if (! rc_is_dir (dirpath))
440 { 440 {
441 free (dirpath); 441 free (dirpath);
442 return (false); 442 return (false);
443 } 443 }
456 } 456 }
457 else 457 else
458 { 458 {
459 char **files = rc_ls_dir (NULL, dirpath, 0); 459 char **files = rc_ls_dir (NULL, dirpath, 0);
460 STRLIST_FOREACH (files, file, i) 460 STRLIST_FOREACH (files, file, i)
461 { 461 {
462 retval = _match_daemon (dirpath, file, mexec, NULL, NULL); 462 retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
463 if (retval) 463 if (retval)
464 break; 464 break;
465 } 465 }
466 free (files); 466 free (files);
467 } 467 }
468 468
469 free (mexec); 469 free (mexec);
470 return (retval); 470 return (retval);
490 490
491 if (! service) 491 if (! service)
492 return (false); 492 return (false);
493 493
494 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service), 494 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
495 (char *) NULL); 495 (char *) NULL);
496 if (! rc_is_dir (dirpath)) 496 if (! rc_is_dir (dirpath))
497 { 497 {
498 free (dirpath); 498 free (dirpath);
499 return (false); 499 return (false);
500 } 500 }
505 { 505 {
506 path = rc_strcatpaths (dirpath, file, (char *) NULL); 506 path = rc_strcatpaths (dirpath, file, (char *) NULL);
507 fp = fopen (path, "r"); 507 fp = fopen (path, "r");
508 free (path); 508 free (path);
509 if (! fp) 509 if (! fp)
510 { 510 {
511 eerror ("fopen `%s': %s", file, strerror (errno)); 511 eerror ("fopen `%s': %s", file, strerror (errno));
512 continue; 512 continue;
513 } 513 }
514 514
515 while ((fgets (buffer, RC_LINEBUFFER, fp))) 515 while ((fgets (buffer, RC_LINEBUFFER, fp)))
516 { 516 {
517 int lb = strlen (buffer) - 1; 517 int lb = strlen (buffer) - 1;
518 if (buffer[lb] == '\n') 518 if (buffer[lb] == '\n')
519 buffer[lb] = 0; 519 buffer[lb] = 0;
520 520
521 p = buffer; 521 p = buffer;
522 if ((token = strsep (&p, "=")) == NULL || ! p) 522 if ((token = strsep (&p, "=")) == NULL || ! p)
523 continue; 523 continue;
524 524
525 if (strlen (p) == 0) 525 if (strlen (p) == 0)
526 continue; 526 continue;
527 527
528 if (strcmp (token, "exec") == 0) 528 if (strcmp (token, "exec") == 0)
529 { 529 {
530 if (exec) 530 if (exec)
531 free (exec); 531 free (exec);
532 exec = strdup (p); 532 exec = strdup (p);
533 } 533 }
534 else if (strcmp (token, "name") == 0) 534 else if (strcmp (token, "name") == 0)
535 { 535 {
536 if (name) 536 if (name)
537 free (name); 537 free (name);
538 name = strdup (p); 538 name = strdup (p);
539 } 539 }
540 else if (strcmp (token, "pidfile") == 0) 540 else if (strcmp (token, "pidfile") == 0)
541 { 541 {
542 if (pidfile) 542 if (pidfile)
543 free (pidfile); 543 free (pidfile);
544 pidfile = strdup (p); 544 pidfile = strdup (p);
545 } 545 }
546 } 546 }
547 fclose (fp); 547 fclose (fp);
548 548
549 pid = 0; 549 pid = 0;
550 if (pidfile) 550 if (pidfile)
551 { 551 {
552 if (! rc_exists (pidfile)) 552 if (! rc_exists (pidfile))
553 { 553 {
554 retval = true; 554 retval = true;
555 break; 555 break;
556 } 556 }
557 557
558 if ((fp = fopen (pidfile, "r")) == NULL) 558 if ((fp = fopen (pidfile, "r")) == NULL)
559 { 559 {
560 eerror ("fopen `%s': %s", pidfile, strerror (errno)); 560 eerror ("fopen `%s': %s", pidfile, strerror (errno));
561 retval = true; 561 retval = true;
562 break; 562 break;
563 } 563 }
564 564
565 if (fscanf (fp, "%d", &pid) != 1) 565 if (fscanf (fp, "%d", &pid) != 1)
566 { 566 {
567 eerror ("no pid found in `%s'", pidfile); 567 eerror ("no pid found in `%s'", pidfile);
568 fclose (fp);
569 retval = true;
570 break;
571 }
572
568 fclose (fp); 573 fclose (fp);
569 retval = true;
570 break;
571 }
572
573 fclose (fp);
574 free (pidfile); 574 free (pidfile);
575 pidfile = NULL; 575 pidfile = NULL;
576 } 576 }
577 577
578 if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) 578 if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL)
579 { 579 {
580 retval = true; 580 retval = true;
581 break; 581 break;
582 } 582 }
583 free (pids); 583 free (pids);
584 584
585 if (exec) 585 if (exec)
586 { 586 {
587 free (exec); 587 free (exec);
588 exec = NULL; 588 exec = NULL;
589 } 589 }
590 if (name) 590 if (name)
591 { 591 {
592 free (name); 592 free (name);
593 name = NULL; 593 name = NULL;
594 } 594 }
595 } 595 }
596 596
597 if (exec) 597 if (exec)
598 { 598 {
599 free (exec); 599 free (exec);

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

  ViewVC Help
Powered by ViewVC 1.1.20