/[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 2634 Revision 2927
116 116
117 if (pid != 0 && pid != p) 117 if (pid != 0 && pid != p)
118 continue; 118 continue;
119 119
120 if (uid) { 120 if (uid) {
121 snprintf (buffer, sizeof (buffer), "/proc/%d", pid); 121 snprintf (buffer, sizeof (buffer), "/proc/%d", p);
122 if (stat (buffer, &sb) != 0 || sb.st_uid != uid) 122 if (stat (buffer, &sb) != 0 || sb.st_uid != uid)
123 continue; 123 continue;
124 } 124 }
125 125
126 if (cmd && ! pid_is_cmd (p, cmd)) 126 if (cmd && ! pid_is_cmd (p, cmd))
145 return (pids); 145 return (pids);
146} 146}
147librc_hidden_def(rc_find_pids) 147librc_hidden_def(rc_find_pids)
148 148
149#elif defined(__DragonFly__) || defined(__FreeBSD__) || \ 149#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
150 defined(__NetBSD__) || defined(__OpenBSD__) 150 defined(__NetBSD__) || defined(__OpenBSD__)
151 151
152# if defined(__DragonFly__) || defined(__FreeBSD__) 152# if defined(__DragonFly__) || defined(__FreeBSD__)
153# ifndef KERN_PROC_PROC 153# ifndef KERN_PROC_PROC
154# define KERN_PROC_PROC KERN_PROC_ALL 154# define KERN_PROC_PROC KERN_PROC_ALL
155# endif 155# endif
226 226
227#else 227#else
228# error "Platform not supported!" 228# error "Platform not supported!"
229#endif 229#endif
230 230
231static bool _match_daemon (const char *path, const char *file, 231static int _match_daemon (const char *path, const char *file,
232 const char *mexec, const char *mname, 232 const char *mexec, const char *mname,
233 const char *mpidfile) 233 const char *mpidfile)
234{ 234{
235 char buffer[RC_LINEBUFFER]; 235 char buffer[RC_LINEBUFFER];
236 char *ffile = rc_strcatpaths (path, file, (char *) NULL); 236 char *ffile = rc_strcatpaths (path, file, (char *) NULL);
237 FILE *fp; 237 FILE *fp;
238 int lc = 0; 238 int lc = 0;
239 int m = 0; 239 int m = 0;
240 240
241 if (! rc_exists (ffile)) { 241 if (rc_exists (ffile) != 0) {
242 free (ffile); 242 free (ffile);
243 return (false); 243 return (-1);
244 } 244 }
245 245
246 if ((fp = fopen (ffile, "r")) == NULL) { 246 if ((fp = fopen (ffile, "r")) == NULL) {
247 eerror ("fopen `%s': %s", ffile, strerror (errno)); 247 eerror ("fopen `%s': %s", ffile, strerror (errno));
248 free (ffile); 248 free (ffile);
249 return (false); 249 return (-1);
250 } 250 }
251 251
252 if (! mname) 252 if (! mname)
253 m += 10; 253 m += 10;
254 if (! mpidfile) 254 if (! mpidfile)
275 break; 275 break;
276 } 276 }
277 fclose (fp); 277 fclose (fp);
278 free (ffile); 278 free (ffile);
279 279
280 return (m == 111 ? true : false); 280 return (m == 111 ? 0 : -1);
281} 281}
282 282
283void rc_set_service_daemon (const char *service, const char *exec, 283int rc_set_service_daemon (const char *service, const char *exec,
284 const char *name, const char *pidfile, 284 const char *name, const char *pidfile,
285 bool started) 285 bool started)
286{ 286{
287 char *svc = rc_xstrdup (service); 287 char *svc = rc_xstrdup (service);
288 char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc), 288 char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
289 (char *) NULL); 289 (char *) NULL);
290 char **files = NULL; 290 char **files = NULL;
293 int i; 293 int i;
294 char *mexec; 294 char *mexec;
295 char *mname; 295 char *mname;
296 char *mpidfile; 296 char *mpidfile;
297 int nfiles = 0; 297 int nfiles = 0;
298 int retval = -1;
298 299
299 free (svc); 300 free (svc);
300 if (! exec && ! name && ! pidfile) 301 if (! exec && ! name && ! pidfile)
301 return; 302 return (-1);
302 303
303 if (exec) { 304 if (exec) {
304 i = strlen (exec) + 6; 305 i = strlen (exec) + 6;
305 mexec = rc_xmalloc (sizeof (char *) * i); 306 mexec = rc_xmalloc (sizeof (char *) * i);
306 snprintf (mexec, i, "exec=%s", exec); 307 snprintf (mexec, i, "exec=%s", exec);
320 snprintf (mpidfile, i, "pidfile=%s", pidfile); 321 snprintf (mpidfile, i, "pidfile=%s", pidfile);
321 } else 322 } else
322 mpidfile = rc_xstrdup ("pidfile="); 323 mpidfile = rc_xstrdup ("pidfile=");
323 324
324 /* Regardless, erase any existing daemon info */ 325 /* Regardless, erase any existing daemon info */
325 if (rc_is_dir (dirpath)) { 326 if (rc_is_dir (dirpath) == 0) {
326 char *oldfile = NULL; 327 char *oldfile = NULL;
327 files = rc_ls_dir (NULL, dirpath, 0); 328 files = rc_ls_dir (dirpath, 0);
328 STRLIST_FOREACH (files, file, i) { 329 STRLIST_FOREACH (files, file, i) {
329 ffile = rc_strcatpaths (dirpath, file, (char *) NULL); 330 ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
330 nfiles++; 331 nfiles++;
331 332
332 if (! oldfile) { 333 if (! oldfile) {
333 if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) { 334 if (_match_daemon (dirpath, file, mexec, mname, mpidfile) == 0) {
334 unlink (ffile); 335 unlink (ffile);
335 oldfile = ffile; 336 oldfile = ffile;
336 nfiles--; 337 nfiles--;
337 } 338 }
338 } else { 339 } else {
339 rename (ffile, oldfile); 340 rename (ffile, oldfile);
340 free (oldfile); 341 free (oldfile);
341 oldfile = ffile; 342 oldfile = ffile;
342 } 343 }
343 } 344 }
344 if (ffile)
345 free (ffile); 345 free (ffile);
346 free (files); 346 rc_strlist_free (files);
347 } 347 }
348 348
349 /* Now store our daemon info */ 349 /* Now store our daemon info */
350 if (started) { 350 if (started) {
351 char buffer[10]; 351 char buffer[10];
352 FILE *fp; 352 FILE *fp;
353 353
354 if (! rc_is_dir (dirpath)) 354 if (rc_is_dir (dirpath) != 0)
355 if (mkdir (dirpath, 0755) != 0) 355 if (mkdir (dirpath, 0755) != 0)
356 eerror ("mkdir `%s': %s", dirpath, strerror (errno)); 356 eerror ("mkdir `%s': %s", dirpath, strerror (errno));
357 357
358 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1); 358 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1);
359 file = rc_strcatpaths (dirpath, buffer, (char *) NULL); 359 file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
360 if ((fp = fopen (file, "w")) == NULL) 360 if ((fp = fopen (file, "w")) == NULL)
361 eerror ("fopen `%s': %s", file, strerror (errno)); 361 eerror ("fopen `%s': %s", file, strerror (errno));
362 else { 362 else {
363 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile); 363 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
364 retval = 0;
364 fclose (fp); 365 fclose (fp);
365 } 366 }
366 free (file); 367 free (file);
367 } 368 }
368 369
369 free (mexec); 370 free (mexec);
370 free (mname); 371 free (mname);
371 free (mpidfile); 372 free (mpidfile);
372 free (dirpath); 373 free (dirpath);
374
375 return (retval);
373} 376}
374librc_hidden_def(rc_set_service_daemon) 377librc_hidden_def(rc_set_service_daemon)
375 378
376bool rc_service_started_daemon (const char *service, const char *exec, 379int rc_service_started_daemon (const char *service, const char *exec,
377 int indx) 380 int indx)
378{ 381{
379 char *dirpath; 382 char *dirpath;
380 char *file; 383 char *file;
381 int i; 384 int i;
382 char *mexec; 385 char *mexec;
383 bool retval = false; 386 int retval = -1;
384 char *svc; 387 char *svc;
385 388
386 if (! service || ! exec) 389 if (! service || ! exec)
387 return (false); 390 return (-1);
388 391
389 svc = rc_xstrdup (service); 392 svc = rc_xstrdup (service);
390 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc), 393 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
391 (char *) NULL); 394 (char *) NULL);
392 free (svc); 395 free (svc);
393 396
394 if (! rc_is_dir (dirpath)) { 397 if (rc_is_dir (dirpath) != 0) {
395 free (dirpath); 398 free (dirpath);
396 return (false); 399 return (-1);
397 } 400 }
398 401
399 i = strlen (exec) + 6; 402 i = strlen (exec) + 6;
400 mexec = rc_xmalloc (sizeof (char *) * i); 403 mexec = rc_xmalloc (sizeof (char *) * i);
401 snprintf (mexec, i, "exec=%s", exec); 404 snprintf (mexec, i, "exec=%s", exec);
405 file = rc_xmalloc (len); 408 file = rc_xmalloc (len);
406 snprintf (file, len, "%03d", indx); 409 snprintf (file, len, "%03d", indx);
407 retval = _match_daemon (dirpath, file, mexec, NULL, NULL); 410 retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
408 free (file); 411 free (file);
409 } else { 412 } else {
410 char **files = rc_ls_dir (NULL, dirpath, 0); 413 char **files = rc_ls_dir (dirpath, 0);
411 STRLIST_FOREACH (files, file, i) { 414 STRLIST_FOREACH (files, file, i) {
412 retval = _match_daemon (dirpath, file, mexec, NULL, NULL); 415 retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
413 if (retval) 416 if (retval == 0)
414 break; 417 break;
415 } 418 }
416 free (files); 419 rc_strlist_free (files);
417 } 420 }
418 421
419 free (mexec); 422 free (mexec);
420 return (retval); 423 return (retval);
421} 424}
422librc_hidden_def(rc_service_started_daemon) 425librc_hidden_def(rc_service_started_daemon)
423 426
424bool rc_service_daemons_crashed (const char *service) 427int rc_service_daemons_crashed (const char *service)
425{ 428{
426 char *dirpath; 429 char *dirpath;
427 char **files; 430 char **files;
428 char *file; 431 char *file;
429 char *path; 432 char *path;
435 char *pidfile = NULL; 438 char *pidfile = NULL;
436 pid_t pid = 0; 439 pid_t pid = 0;
437 pid_t *pids = NULL; 440 pid_t *pids = NULL;
438 char *p; 441 char *p;
439 char *token; 442 char *token;
440 bool retval = false; 443 int retval = -1;
441 char *svc; 444 char *svc;
442 445
443 if (! service) 446 if (! service)
444 return (false); 447 return (-1);
445 448
446 svc = rc_xstrdup (service); 449 svc = rc_xstrdup (service);
447 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc), 450 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
448 (char *) NULL); 451 (char *) NULL);
449 free (svc); 452 free (svc);
450 453
451 if (! rc_is_dir (dirpath)) { 454 if (rc_is_dir (dirpath) != 0) {
452 free (dirpath); 455 free (dirpath);
453 return (false); 456 return (-1);
454 } 457 }
455 458
456 memset (buffer, 0, sizeof (buffer)); 459 memset (buffer, 0, sizeof (buffer));
457 files = rc_ls_dir (NULL, dirpath, 0); 460 files = rc_ls_dir (dirpath, 0);
458 STRLIST_FOREACH (files, file, i) { 461 STRLIST_FOREACH (files, file, i) {
459 path = rc_strcatpaths (dirpath, file, (char *) NULL); 462 path = rc_strcatpaths (dirpath, file, (char *) NULL);
460 fp = fopen (path, "r"); 463 fp = fopen (path, "r");
461 free (path); 464 free (path);
462 if (! fp) { 465 if (! fp) {
492 } 495 }
493 fclose (fp); 496 fclose (fp);
494 497
495 pid = 0; 498 pid = 0;
496 if (pidfile) { 499 if (pidfile) {
497 if (! rc_exists (pidfile)) { 500 if (rc_exists (pidfile) != 0) {
498 retval = true; 501 retval = 0;
499 break; 502 break;
500 } 503 }
501 504
502 if ((fp = fopen (pidfile, "r")) == NULL) { 505 if ((fp = fopen (pidfile, "r")) == NULL) {
503 eerror ("fopen `%s': %s", pidfile, strerror (errno)); 506 eerror ("fopen `%s': %s", pidfile, strerror (errno));
504 retval = true; 507 retval = 0;
505 break; 508 break;
506 } 509 }
507 510
508 if (fscanf (fp, "%d", &pid) != 1) { 511 if (fscanf (fp, "%d", &pid) != 1) {
509 eerror ("no pid found in `%s'", pidfile); 512 eerror ("no pid found in `%s'", pidfile);
510 fclose (fp); 513 fclose (fp);
511 retval = true; 514 retval = 0;
512 break; 515 break;
513 } 516 }
514 517
515 fclose (fp); 518 fclose (fp);
516 free (pidfile); 519 free (pidfile);
517 pidfile = NULL; 520 pidfile = NULL;
518 }
519 521
520 if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) { 522 /* We have the pid, so no need to match on name */
521 retval = true;
522 break;
523 }
524 free (pids);
525
526 if (exec) {
527 free (exec); 523 free (exec);
528 exec = NULL; 524 exec = NULL;
529 }
530 if (name) {
531 free (name); 525 free (name);
532 name = NULL; 526 name = NULL;
533 } 527 }
528
529 if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) {
530 retval = 0;
531 break;
534 } 532 }
533 free (pids);
535 534
536 if (exec) {
537 free (exec); 535 free (exec);
538 exec = NULL; 536 exec = NULL;
539 }
540 if (name) {
541 free (name); 537 free (name);
542 name = NULL; 538 name = NULL;
543 } 539 }
544 540
541 free (exec);
542 free (name);
545 free (dirpath); 543 free (dirpath);
546 rc_strlist_free (files); 544 rc_strlist_free (files);
547 545
548 return (retval); 546 return (retval);
549} 547}

Legend:
Removed from v.2634  
changed lines
  Added in v.2927

  ViewVC Help
Powered by ViewVC 1.1.20