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

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

  ViewVC Help
Powered by ViewVC 1.1.20