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

Diff of /trunk/src/librc.c

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

Revision 2577 Revision 2596
9#include <sys/select.h> 9#include <sys/select.h>
10#include <sys/time.h> 10#include <sys/time.h>
11#include <sys/stat.h> 11#include <sys/stat.h>
12#include <sys/wait.h> 12#include <sys/wait.h>
13#include <errno.h> 13#include <errno.h>
14#ifndef __linux__
15/* Although linux should work fine, gcc likes to bitch with our default
16 CFLAGS so we just don't include the file and use the GNU one defined
17 in string.h */
18#include <libgen.h> 14#include <libgen.h>
19#endif
20#include <limits.h> 15#include <limits.h>
21#include <stdarg.h> 16#include <stdarg.h>
22#include <stdbool.h> 17#include <stdbool.h>
23#include <stdio.h> 18#include <stdio.h>
24#include <stdlib.h> 19#include <stdlib.h>
186 181
187bool rc_service_in_runlevel (const char *service, const char *runlevel) 182bool rc_service_in_runlevel (const char *service, const char *runlevel)
188{ 183{
189 char *file; 184 char *file;
190 bool retval; 185 bool retval;
186 char *svc;
191 187
192 if (! runlevel || ! service) 188 if (! runlevel || ! service)
193 return (false); 189 return (false);
194 190
195 if (! rc_service_exists (service)) 191 if (! rc_service_exists (service))
196 return (false); 192 return (false);
197 193
194 svc = rc_xstrdup (service);
198 file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (service), 195 file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc),
199 (char *) NULL); 196 (char *) NULL);
197 free (svc);
200 retval = rc_exists (file); 198 retval = rc_exists (file);
201 free (file); 199 free (file);
202 200
203 return (retval); 201 return (retval);
204} 202}
207{ 205{
208 char *file; 206 char *file;
209 int i = 0; 207 int i = 0;
210 int skip_state = -1; 208 int skip_state = -1;
211 char *base; 209 char *base;
210 char *svc;
212 char *init = rc_resolve_service (service); 211 char *init = rc_resolve_service (service);
213 bool skip_wasinactive = false; 212 bool skip_wasinactive = false;
214 213
215 if (! service) 214 if (! service)
216 return (false); 215 return (false);
217 216
217 svc = rc_xstrdup (service);
218 base = basename (service); 218 base = basename (svc);
219 219
220 if (state != rc_service_stopped) { 220 if (state != rc_service_stopped) {
221 if (! rc_is_file(init)) { 221 if (! rc_is_file(init)) {
222 free (init); 222 free (init);
223 free (svc);
223 return (false); 224 return (false);
224 } 225 }
225 226
226 file = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[state], base, 227 file = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[state], base,
227 (char *) NULL); 228 (char *) NULL);
229 unlink (file); 230 unlink (file);
230 i = symlink (init, file); 231 i = symlink (init, file);
231 if (i != 0) { 232 if (i != 0) {
232 free (file); 233 free (file);
233 free (init); 234 free (init);
235 free (svc);
234 einfo ("%d %s %s", state, rc_service_state_names[state], base); 236 einfo ("%d %s %s", state, rc_service_state_names[state], base);
235 eerror ("symlink `%s' to `%s': %s", init, file, strerror (errno)); 237 eerror ("symlink `%s' to `%s': %s", init, file, strerror (errno));
236 return (false); 238 return (false);
237 } 239 }
238 240
240 skip_state = state; 242 skip_state = state;
241 } 243 }
242 244
243 if (state == rc_service_coldplugged) { 245 if (state == rc_service_coldplugged) {
244 free (init); 246 free (init);
247 free (svc);
245 return (true); 248 return (true);
246 } 249 }
247 250
248 /* Remove any old states now */ 251 /* Remove any old states now */
249 i = 0; 252 i = 0;
335 } 338 }
336 rc_strlist_free (dirs); 339 rc_strlist_free (dirs);
337 free (sdir); 340 free (sdir);
338 } 341 }
339 342
343 free (svc);
340 free (init); 344 free (init);
341 return (true); 345 return (true);
342} 346}
343 347
344bool rc_service_state (const char *service, const rc_service_state_t state) 348bool rc_service_state (const char *service, const rc_service_state_t state)
345{ 349{
346 char *file; 350 char *file;
347 bool retval; 351 bool retval;
352 char *svc;
348 353
349 /* If the init script does not exist then we are stopped */ 354 /* If the init script does not exist then we are stopped */
350 if (! rc_service_exists (service)) 355 if (! rc_service_exists (service))
351 return (state == rc_service_stopped ? true : false); 356 return (state == rc_service_stopped ? true : false);
352 357
368 return (retval); 373 return (retval);
369 } 374 }
370 375
371 /* Now we just check if a file by the service name rc_exists 376 /* Now we just check if a file by the service name rc_exists
372 in the state dir */ 377 in the state dir */
378 svc = rc_xstrdup (service);
373 file = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[state], 379 file = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[state],
374 basename (service), (char*) NULL); 380 basename (svc), (char*) NULL);
381 free (svc);
375 retval = rc_exists (file); 382 retval = rc_exists (file);
376 free (file); 383 free (file);
377 return (retval); 384 return (retval);
378} 385}
379 386
440 char *file; 447 char *file;
441 char *fifo; 448 char *fifo;
442 pid_t pid = -1; 449 pid_t pid = -1;
443 pid_t savedpid; 450 pid_t savedpid;
444 int status; 451 int status;
452 char *svc;
445 453
446 file = rc_resolve_service (service); 454 file = rc_resolve_service (service);
447 if (! rc_is_file (file)) { 455 if (! rc_is_file (file)) {
448 rc_mark_service (service, rc_service_stopped); 456 rc_mark_service (service, rc_service_stopped);
449 free (file); 457 free (file);
450 return (0); 458 return (0);
451 } 459 }
452 460
453 /* We create a fifo so that other services can wait until we complete */ 461 /* We create a fifo so that other services can wait until we complete */
462 svc = rc_xstrdup (service);
454 fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (service), 463 fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (svc),
455 (char *) NULL); 464 (char *) NULL);
465 free (svc);
456 466
457 if (mkfifo (fifo, 0600) != 0 && errno != EEXIST) { 467 if (mkfifo (fifo, 0600) != 0 && errno != EEXIST) {
458 eerror ("unable to create fifo `%s': %s", fifo, strerror (errno)); 468 eerror ("unable to create fifo `%s': %s", fifo, strerror (errno));
459 free (fifo); 469 free (fifo);
460 free (file); 470 free (file);
518 const char *service_to_start) 528 const char *service_to_start)
519{ 529{
520 char *dir; 530 char *dir;
521 char *init; 531 char *init;
522 char *file; 532 char *file;
533 char *svc;
523 534
524 /* service may be a provided service, like net */ 535 /* service may be a provided service, like net */
525 if (! service || ! rc_service_exists (service_to_start)) 536 if (! service || ! rc_service_exists (service_to_start))
526 return; 537 return;
527 538
539 svc = rc_xstrdup (service);
528 dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (service), 540 dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc),
529 (char *) NULL); 541 (char *) NULL);
542 free (svc);
530 if (! rc_is_dir (dir)) 543 if (! rc_is_dir (dir))
531 if (mkdir (dir, 0755) != 0) { 544 if (mkdir (dir, 0755) != 0) {
532 eerror ("mkdir `%s': %s", dir, strerror (errno)); 545 eerror ("mkdir `%s': %s", dir, strerror (errno));
533 free (dir); 546 free (dir);
534 return; 547 return;
535 } 548 }
536 549
537 init = rc_resolve_service (service_to_start); 550 init = rc_resolve_service (service_to_start);
551 svc = rc_xstrdup (service_to_start);
538 file = rc_strcatpaths (dir, basename (service_to_start), (char *) NULL); 552 file = rc_strcatpaths (dir, basename (svc), (char *) NULL);
553 free (svc);
539 if (! rc_exists (file) && symlink (init, file) != 0) 554 if (! rc_exists (file) && symlink (init, file) != 0)
540 eerror ("symlink `%s' to `%s': %s", init, file, strerror (errno)); 555 eerror ("symlink `%s' to `%s': %s", init, file, strerror (errno));
541 556
542 free (init); 557 free (init);
543 free (file); 558 free (file);
544 free (dir); 559 free (dir);
545} 560}
546 561
547void rc_schedule_clear (const char *service) 562void rc_schedule_clear (const char *service)
548{ 563{
564 char *svc = rc_xstrdup (service);
549 char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (service), 565 char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc),
550 (char *) NULL); 566 (char *) NULL);
551 567
568 free (svc);
552 if (rc_is_dir (dir)) 569 if (rc_is_dir (dir))
553 rc_rm_dir (dir, true); 570 rc_rm_dir (dir, true);
554 free (dir); 571 free (dir);
555} 572}
556 573
557bool rc_wait_service (const char *service) 574bool rc_wait_service (const char *service)
558{ 575{
576 char *svc = rc_xstrdup (service);
559 char *fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (service), 577 char *fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (svc),
560 (char *) NULL); 578 (char *) NULL);
561 struct timeval tv; 579 struct timeval tv;
562 struct timeval stopat; 580 struct timeval stopat;
563 struct timeval now; 581 struct timeval now;
564 bool retval = false; 582 bool retval = false;
565 583
584 free (svc);
566 if (gettimeofday (&stopat, NULL) != 0) { 585 if (gettimeofday (&stopat, NULL) != 0) {
567 eerror ("gettimeofday: %s", strerror (errno)); 586 eerror ("gettimeofday: %s", strerror (errno));
568 return (false); 587 return (false);
569 } 588 }
570 stopat.tv_sec += WAIT_MAX; 589 stopat.tv_sec += WAIT_MAX;
658bool rc_service_add (const char *runlevel, const char *service) 677bool rc_service_add (const char *runlevel, const char *service)
659{ 678{
660 bool retval; 679 bool retval;
661 char *init; 680 char *init;
662 char *file; 681 char *file;
682 char *svc;
663 683
664 if (! rc_runlevel_exists (runlevel)) { 684 if (! rc_runlevel_exists (runlevel)) {
665 errno = ENOENT; 685 errno = ENOENT;
666 return (false); 686 return (false);
667 } 687 }
670 errno = EEXIST; 690 errno = EEXIST;
671 return (false); 691 return (false);
672 } 692 }
673 693
674 init = rc_resolve_service (service); 694 init = rc_resolve_service (service);
695 svc = rc_xstrdup (service);
675 file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (service), 696 file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc),
676 (char *) NULL); 697 (char *) NULL);
698 free (svc);
677 retval = (symlink (init, file) == 0); 699 retval = (symlink (init, file) == 0);
678 free (init); 700 free (init);
679 free (file); 701 free (file);
680 return (retval); 702 return (retval);
681} 703}
682 704
683bool rc_service_delete (const char *runlevel, const char *service) 705bool rc_service_delete (const char *runlevel, const char *service)
684{ 706{
685 char *file; 707 char *file;
708 char *svc;
686 bool retval = false; 709 bool retval = false;
687 710
688 if (! runlevel || ! service) 711 if (! runlevel || ! service)
689 return (false); 712 return (false);
690 713
714 svc = rc_xstrdup (service);
691 file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (service), 715 file = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, basename (svc),
692 (char *) NULL); 716 (char *) NULL);
717 free (svc);
693 if (unlink (file) == 0) 718 if (unlink (file) == 0)
694 retval = true; 719 retval = true;
695 720
696 free (file); 721 free (file);
697 return (retval); 722 return (retval);
716 return (list); 741 return (list);
717} 742}
718 743
719char **rc_services_scheduled (const char *service) 744char **rc_services_scheduled (const char *service)
720{ 745{
746 char *svc = rc_xstrdup (service);
721 char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (service), 747 char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc),
722 (char *) NULL); 748 (char *) NULL);
723 char **list = NULL; 749 char **list = NULL;
724 750
725 if (rc_is_dir (dir)) 751 if (rc_is_dir (dir))
726 list = rc_ls_dir (list, dir, RC_LS_INITD); 752 list = rc_ls_dir (list, dir, RC_LS_INITD);
727 753
754 free (svc);
728 free (dir); 755 free (dir);
729 return (list); 756 return (list);
730} 757}
731 758
732bool rc_allow_plug (char *service) 759bool rc_allow_plug (char *service)
738 bool allow = true; 765 bool allow = true;
739 char *match = getenv ("RC_PLUG_SERVICES"); 766 char *match = getenv ("RC_PLUG_SERVICES");
740 if (! match) 767 if (! match)
741 return true; 768 return true;
742 769
743 list = strdup (match); 770 list = rc_xstrdup (match);
744 p = list; 771 p = list;
745 while ((token = strsep (&p, " "))) { 772 while ((token = strsep (&p, " "))) {
746 bool truefalse = true; 773 bool truefalse = true;
747 if (token[0] == '!') { 774 if (token[0] == '!') {
748 truefalse = false; 775 truefalse = false;

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

  ViewVC Help
Powered by ViewVC 1.1.20