/[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 2927 Revision 2928
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 int _match_daemon (const char *path, const char *file, 231static bool _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) != 0) { 241 if (! rc_exists (ffile)) {
242 free (ffile); 242 free (ffile);
243 return (-1); 243 return (false);
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 (-1); 249 return (false);
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 ? 0 : -1); 280 return (m == 111 ? true : false);
281} 281}
282 282
283int rc_set_service_daemon (const char *service, const char *exec, 283void 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;
299 298
300 free (svc); 299 free (svc);
301 if (! exec && ! name && ! pidfile) 300 if (! exec && ! name && ! pidfile)
302 return (-1); 301 return;
303 302
304 if (exec) { 303 if (exec) {
305 i = strlen (exec) + 6; 304 i = strlen (exec) + 6;
306 mexec = rc_xmalloc (sizeof (char *) * i); 305 mexec = rc_xmalloc (sizeof (char *) * i);
307 snprintf (mexec, i, "exec=%s", exec); 306 snprintf (mexec, i, "exec=%s", exec);
321 snprintf (mpidfile, i, "pidfile=%s", pidfile); 320 snprintf (mpidfile, i, "pidfile=%s", pidfile);
322 } else 321 } else
323 mpidfile = rc_xstrdup ("pidfile="); 322 mpidfile = rc_xstrdup ("pidfile=");
324 323
325 /* Regardless, erase any existing daemon info */ 324 /* Regardless, erase any existing daemon info */
326 if (rc_is_dir (dirpath) == 0) { 325 if (rc_is_dir (dirpath)) {
327 char *oldfile = NULL; 326 char *oldfile = NULL;
328 files = rc_ls_dir (dirpath, 0); 327 files = rc_ls_dir (dirpath, 0);
329 STRLIST_FOREACH (files, file, i) { 328 STRLIST_FOREACH (files, file, i) {
330 ffile = rc_strcatpaths (dirpath, file, (char *) NULL); 329 ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
331 nfiles++; 330 nfiles++;
332 331
333 if (! oldfile) { 332 if (! oldfile) {
334 if (_match_daemon (dirpath, file, mexec, mname, mpidfile) == 0) { 333 if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) {
335 unlink (ffile); 334 unlink (ffile);
336 oldfile = ffile; 335 oldfile = ffile;
337 nfiles--; 336 nfiles--;
338 } 337 }
339 } else { 338 } else {
349 /* Now store our daemon info */ 348 /* Now store our daemon info */
350 if (started) { 349 if (started) {
351 char buffer[10]; 350 char buffer[10];
352 FILE *fp; 351 FILE *fp;
353 352
354 if (rc_is_dir (dirpath) != 0) 353 if (! rc_is_dir (dirpath))
355 if (mkdir (dirpath, 0755) != 0) 354 if (mkdir (dirpath, 0755) != 0)
356 eerror ("mkdir `%s': %s", dirpath, strerror (errno)); 355 eerror ("mkdir `%s': %s", dirpath, strerror (errno));
357 356
358 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1); 357 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1);
359 file = rc_strcatpaths (dirpath, buffer, (char *) NULL); 358 file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
360 if ((fp = fopen (file, "w")) == NULL) 359 if ((fp = fopen (file, "w")) == NULL)
361 eerror ("fopen `%s': %s", file, strerror (errno)); 360 eerror ("fopen `%s': %s", file, strerror (errno));
362 else { 361 else {
363 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile); 362 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
364 retval = 0;
365 fclose (fp); 363 fclose (fp);
366 } 364 }
367 free (file); 365 free (file);
368 } 366 }
369 367
370 free (mexec); 368 free (mexec);
371 free (mname); 369 free (mname);
372 free (mpidfile); 370 free (mpidfile);
373 free (dirpath); 371 free (dirpath);
374
375 return (retval);
376} 372}
377librc_hidden_def(rc_set_service_daemon) 373librc_hidden_def(rc_set_service_daemon)
378 374
379int rc_service_started_daemon (const char *service, const char *exec, 375bool rc_service_started_daemon (const char *service, const char *exec,
380 int indx) 376 int indx)
381{ 377{
382 char *dirpath; 378 char *dirpath;
383 char *file; 379 char *file;
384 int i; 380 int i;
385 char *mexec; 381 char *mexec;
386 int retval = -1; 382 bool retval = false;
387 char *svc; 383 char *svc;
388 384
389 if (! service || ! exec) 385 if (! service || ! exec)
390 return (-1); 386 return (false);
391 387
392 svc = rc_xstrdup (service); 388 svc = rc_xstrdup (service);
393 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc), 389 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
394 (char *) NULL); 390 (char *) NULL);
395 free (svc); 391 free (svc);
396 392
397 if (rc_is_dir (dirpath) != 0) { 393 if (! rc_is_dir (dirpath)) {
398 free (dirpath); 394 free (dirpath);
399 return (-1); 395 return (false);
400 } 396 }
401 397
402 i = strlen (exec) + 6; 398 i = strlen (exec) + 6;
403 mexec = rc_xmalloc (sizeof (char *) * i); 399 mexec = rc_xmalloc (sizeof (char *) * i);
404 snprintf (mexec, i, "exec=%s", exec); 400 snprintf (mexec, i, "exec=%s", exec);
411 free (file); 407 free (file);
412 } else { 408 } else {
413 char **files = rc_ls_dir (dirpath, 0); 409 char **files = rc_ls_dir (dirpath, 0);
414 STRLIST_FOREACH (files, file, i) { 410 STRLIST_FOREACH (files, file, i) {
415 retval = _match_daemon (dirpath, file, mexec, NULL, NULL); 411 retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
416 if (retval == 0) 412 if (retval)
417 break; 413 break;
418 } 414 }
419 rc_strlist_free (files); 415 rc_strlist_free (files);
420 } 416 }
421 417
422 free (mexec); 418 free (mexec);
423 return (retval); 419 return (retval);
424} 420}
425librc_hidden_def(rc_service_started_daemon) 421librc_hidden_def(rc_service_started_daemon)
426 422
427int rc_service_daemons_crashed (const char *service) 423bool rc_service_daemons_crashed (const char *service)
428{ 424{
429 char *dirpath; 425 char *dirpath;
430 char **files; 426 char **files;
431 char *file; 427 char *file;
432 char *path; 428 char *path;
438 char *pidfile = NULL; 434 char *pidfile = NULL;
439 pid_t pid = 0; 435 pid_t pid = 0;
440 pid_t *pids = NULL; 436 pid_t *pids = NULL;
441 char *p; 437 char *p;
442 char *token; 438 char *token;
443 int retval = -1; 439 bool retval = false;
444 char *svc; 440 char *svc;
445 441
446 if (! service) 442 if (! service)
447 return (-1); 443 return (false);
448 444
449 svc = rc_xstrdup (service); 445 svc = rc_xstrdup (service);
450 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc), 446 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
451 (char *) NULL); 447 (char *) NULL);
452 free (svc); 448 free (svc);
453 449
454 if (rc_is_dir (dirpath) != 0) { 450 if (! rc_is_dir (dirpath)) {
455 free (dirpath); 451 free (dirpath);
456 return (-1); 452 return (false);
457 } 453 }
458 454
459 memset (buffer, 0, sizeof (buffer)); 455 memset (buffer, 0, sizeof (buffer));
460 files = rc_ls_dir (dirpath, 0); 456 files = rc_ls_dir (dirpath, 0);
461 STRLIST_FOREACH (files, file, i) { 457 STRLIST_FOREACH (files, file, i) {
495 } 491 }
496 fclose (fp); 492 fclose (fp);
497 493
498 pid = 0; 494 pid = 0;
499 if (pidfile) { 495 if (pidfile) {
500 if (rc_exists (pidfile) != 0) { 496 if (! rc_exists (pidfile)) {
501 retval = 0; 497 retval = true;
502 break; 498 break;
503 } 499 }
504 500
505 if ((fp = fopen (pidfile, "r")) == NULL) { 501 if ((fp = fopen (pidfile, "r")) == NULL) {
506 eerror ("fopen `%s': %s", pidfile, strerror (errno)); 502 eerror ("fopen `%s': %s", pidfile, strerror (errno));
507 retval = 0; 503 retval = true;
508 break; 504 break;
509 } 505 }
510 506
511 if (fscanf (fp, "%d", &pid) != 1) { 507 if (fscanf (fp, "%d", &pid) != 1) {
512 eerror ("no pid found in `%s'", pidfile); 508 eerror ("no pid found in `%s'", pidfile);
513 fclose (fp); 509 fclose (fp);
514 retval = 0; 510 retval = true;
515 break; 511 break;
516 } 512 }
517 513
518 fclose (fp); 514 fclose (fp);
519 free (pidfile); 515 free (pidfile);
525 free (name); 521 free (name);
526 name = NULL; 522 name = NULL;
527 } 523 }
528 524
529 if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) { 525 if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) {
530 retval = 0; 526 retval = true;
531 break; 527 break;
532 } 528 }
533 free (pids); 529 free (pids);
534 530
535 free (exec); 531 free (exec);

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

  ViewVC Help
Powered by ViewVC 1.1.20