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

Diff of /trunk/src/librc.c

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

Revision 2596 Revision 2597
3 core RC functions 3 core RC functions
4 Copyright 2007 Gentoo Foundation 4 Copyright 2007 Gentoo Foundation
5 Released under the GPLv2 5 Released under the GPLv2
6 */ 6 */
7 7
8#include <sys/types.h>
9#include <sys/select.h>
10#include <sys/time.h>
11#include <sys/stat.h>
12#include <sys/wait.h>
13#include <errno.h>
14#include <libgen.h>
15#include <limits.h>
16#include <stdarg.h>
17#include <stdbool.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22
23#include "einfo.h"
24#include "rc.h" 8#include "librc.h"
25#include "rc-misc.h"
26#include "strlist.h"
27 9
28/* usecs to wait while we poll the fifo */ 10/* usecs to wait while we poll the fifo */
29#define WAIT_INTERVAL 20000 11#define WAIT_INTERVAL 20000
30 12
31/* max secs to wait until a service comes up */ 13/* max secs to wait until a service comes up */
48 30
49bool rc_runlevel_starting (void) 31bool rc_runlevel_starting (void)
50{ 32{
51 return (rc_is_dir (RC_SVCDIR "softscripts.old")); 33 return (rc_is_dir (RC_SVCDIR "softscripts.old"));
52} 34}
35librc_hidden_def(rc_runlevel_starting)
53 36
54bool rc_runlevel_stopping (void) 37bool rc_runlevel_stopping (void)
55{ 38{
56 return (rc_is_dir (RC_SVCDIR "softscripts.new")); 39 return (rc_is_dir (RC_SVCDIR "softscripts.new"));
57} 40}
41librc_hidden_def(rc_runlevel_stopping)
58 42
59char **rc_get_runlevels (void) 43char **rc_get_runlevels (void)
60{ 44{
61 char **dirs = rc_ls_dir (NULL, RC_RUNLEVELDIR, 0); 45 char **dirs = rc_ls_dir (NULL, RC_RUNLEVELDIR, 0);
62 char **runlevels = NULL; 46 char **runlevels = NULL;
71 } 55 }
72 rc_strlist_free (dirs); 56 rc_strlist_free (dirs);
73 57
74 return (runlevels); 58 return (runlevels);
75} 59}
60librc_hidden_def(rc_get_runlevels)
76 61
77char *rc_get_runlevel (void) 62char *rc_get_runlevel (void)
78{ 63{
79 FILE *fp; 64 FILE *fp;
80 static char buffer [PATH_MAX]; 65 static char buffer [PATH_MAX];
94 79
95 fclose (fp); 80 fclose (fp);
96 snprintf (buffer, sizeof (buffer), "sysinit"); 81 snprintf (buffer, sizeof (buffer), "sysinit");
97 return (buffer); 82 return (buffer);
98} 83}
84librc_hidden_def(rc_get_runlevel)
99 85
100void rc_set_runlevel (const char *runlevel) 86void rc_set_runlevel (const char *runlevel)
101{ 87{
102 FILE *fp = fopen (SOFTLEVEL, "w"); 88 FILE *fp = fopen (SOFTLEVEL, "w");
103 if (! fp) 89 if (! fp)
104 eerrorx ("failed to open `" SOFTLEVEL "': %s", strerror (errno)); 90 eerrorx ("failed to open `" SOFTLEVEL "': %s", strerror (errno));
105 fprintf (fp, "%s", runlevel); 91 fprintf (fp, "%s", runlevel);
106 fclose (fp); 92 fclose (fp);
107} 93}
94librc_hidden_def(rc_set_runlevel)
108 95
109bool rc_runlevel_exists (const char *runlevel) 96bool rc_runlevel_exists (const char *runlevel)
110{ 97{
111 char *path; 98 char *path;
112 bool retval; 99 bool retval;
117 path = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, (char *) NULL); 104 path = rc_strcatpaths (RC_RUNLEVELDIR, runlevel, (char *) NULL);
118 retval = rc_is_dir (path); 105 retval = rc_is_dir (path);
119 free (path); 106 free (path);
120 return (retval); 107 return (retval);
121} 108}
109librc_hidden_def(rc_runlevel_exists)
122 110
123/* Resolve a service name to it's full path */ 111/* Resolve a service name to it's full path */
124char *rc_resolve_service (const char *service) 112char *rc_resolve_service (const char *service)
125{ 113{
126 char buffer[PATH_MAX]; 114 char buffer[PATH_MAX];
152 } 140 }
153 141
154 snprintf (buffer, sizeof (buffer), RC_INITDIR "%s", service); 142 snprintf (buffer, sizeof (buffer), RC_INITDIR "%s", service);
155 return (strdup (buffer)); 143 return (strdup (buffer));
156} 144}
145librc_hidden_def(rc_resolve_service)
157 146
158bool rc_service_exists (const char *service) 147bool rc_service_exists (const char *service)
159{ 148{
160 char *file; 149 char *file;
161 bool retval = false; 150 bool retval = false;
176 if (rc_exists (file)) 165 if (rc_exists (file))
177 retval = rc_is_exec (file); 166 retval = rc_is_exec (file);
178 free (file); 167 free (file);
179 return (retval); 168 return (retval);
180} 169}
170librc_hidden_def(rc_service_exists)
181 171
182bool rc_service_in_runlevel (const char *service, const char *runlevel) 172bool rc_service_in_runlevel (const char *service, const char *runlevel)
183{ 173{
184 char *file; 174 char *file;
185 bool retval; 175 bool retval;
198 retval = rc_exists (file); 188 retval = rc_exists (file);
199 free (file); 189 free (file);
200 190
201 return (retval); 191 return (retval);
202} 192}
193librc_hidden_def(rc_service_in_runlevel)
203 194
204bool rc_mark_service (const char *service, const rc_service_state_t state) 195bool rc_mark_service (const char *service, const rc_service_state_t state)
205{ 196{
206 char *file; 197 char *file;
207 int i = 0; 198 int i = 0;
342 333
343 free (svc); 334 free (svc);
344 free (init); 335 free (init);
345 return (true); 336 return (true);
346} 337}
338librc_hidden_def(rc_mark_service)
347 339
348bool rc_service_state (const char *service, const rc_service_state_t state) 340bool rc_service_state (const char *service, const rc_service_state_t state)
349{ 341{
350 char *file; 342 char *file;
351 bool retval; 343 bool retval;
381 free (svc); 373 free (svc);
382 retval = rc_exists (file); 374 retval = rc_exists (file);
383 free (file); 375 free (file);
384 return (retval); 376 return (retval);
385} 377}
378librc_hidden_def(rc_service_state)
386 379
387bool rc_get_service_option (const char *service, const char *option, 380bool rc_get_service_option (const char *service, const char *option,
388 char *value) 381 char *value)
389{ 382{
390 FILE *fp; 383 FILE *fp;
408 } 401 }
409 402
410 free (file); 403 free (file);
411 return (retval); 404 return (retval);
412} 405}
406librc_hidden_def(rc_get_service_option)
413 407
414bool rc_set_service_option (const char *service, const char *option, 408bool rc_set_service_option (const char *service, const char *option,
415 const char *value) 409 const char *value)
416{ 410{
417 FILE *fp; 411 FILE *fp;
439 433
440 free (path); 434 free (path);
441 free (file); 435 free (file);
442 return (retval); 436 return (retval);
443} 437}
438librc_hidden_def(rc_set_service_option)
444 439
445static pid_t _exec_service (const char *service, const char *arg) 440static pid_t _exec_service (const char *service, const char *arg)
446{ 441{
447 char *file; 442 char *file;
448 char *fifo; 443 char *fifo;
512 if (rc_service_state (service, rc_service_stopped)) 507 if (rc_service_state (service, rc_service_stopped))
513 return (0); 508 return (0);
514 509
515 return (_exec_service (service, "stop")); 510 return (_exec_service (service, "stop"));
516} 511}
517 512librc_hidden_def(rc_stop_service)
518 513
519pid_t rc_start_service (const char *service) 514pid_t rc_start_service (const char *service)
520{ 515{
521 if (! rc_service_state (service, rc_service_stopped)) 516 if (! rc_service_state (service, rc_service_stopped))
522 return (0); 517 return (0);
523 518
524 return (_exec_service (service, "start")); 519 return (_exec_service (service, "start"));
525} 520}
521librc_hidden_def(rc_start_service)
526 522
527void rc_schedule_start_service (const char *service, 523void rc_schedule_start_service (const char *service,
528 const char *service_to_start) 524 const char *service_to_start)
529{ 525{
530 char *dir; 526 char *dir;
556 552
557 free (init); 553 free (init);
558 free (file); 554 free (file);
559 free (dir); 555 free (dir);
560} 556}
557librc_hidden_def(rc_schedule_start_service)
561 558
562void rc_schedule_clear (const char *service) 559void rc_schedule_clear (const char *service)
563{ 560{
564 char *svc = rc_xstrdup (service); 561 char *svc = rc_xstrdup (service);
565 char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc), 562 char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc),
568 free (svc); 565 free (svc);
569 if (rc_is_dir (dir)) 566 if (rc_is_dir (dir))
570 rc_rm_dir (dir, true); 567 rc_rm_dir (dir, true);
571 free (dir); 568 free (dir);
572} 569}
570librc_hidden_def(rc_schedule_clear)
573 571
574bool rc_wait_service (const char *service) 572bool rc_wait_service (const char *service)
575{ 573{
576 char *svc = rc_xstrdup (service); 574 char *svc = rc_xstrdup (service);
577 char *fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (svc), 575 char *fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", basename (svc),
613 } 611 }
614 612
615 free (fifo); 613 free (fifo);
616 return (retval); 614 return (retval);
617} 615}
616librc_hidden_def(rc_wait_service)
618 617
619char **rc_services_in_runlevel (const char *runlevel) 618char **rc_services_in_runlevel (const char *runlevel)
620{ 619{
621 char *dir; 620 char *dir;
622 char **list = NULL; 621 char **list = NULL;
636 list = rc_ls_dir (list, dir, RC_LS_INITD); 635 list = rc_ls_dir (list, dir, RC_LS_INITD);
637 636
638 free (dir); 637 free (dir);
639 return (list); 638 return (list);
640} 639}
640librc_hidden_def(rc_services_in_runlevel)
641 641
642char **rc_services_in_state (rc_service_state_t state) 642char **rc_services_in_state (rc_service_state_t state)
643{ 643{
644 char *dir = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[state], 644 char *dir = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[state],
645 (char *) NULL); 645 (char *) NULL);
671 } 671 }
672 672
673 free (dir); 673 free (dir);
674 return (list); 674 return (list);
675} 675}
676librc_hidden_def(rc_services_in_state)
676 677
677bool rc_service_add (const char *runlevel, const char *service) 678bool rc_service_add (const char *runlevel, const char *service)
678{ 679{
679 bool retval; 680 bool retval;
680 char *init; 681 char *init;
699 retval = (symlink (init, file) == 0); 700 retval = (symlink (init, file) == 0);
700 free (init); 701 free (init);
701 free (file); 702 free (file);
702 return (retval); 703 return (retval);
703} 704}
705librc_hidden_def(rc_service_add)
704 706
705bool rc_service_delete (const char *runlevel, const char *service) 707bool rc_service_delete (const char *runlevel, const char *service)
706{ 708{
707 char *file; 709 char *file;
708 char *svc; 710 char *svc;
719 retval = true; 721 retval = true;
720 722
721 free (file); 723 free (file);
722 return (retval); 724 return (retval);
723} 725}
726librc_hidden_def(rc_service_delete)
724 727
725char **rc_services_scheduled_by (const char *service) 728char **rc_services_scheduled_by (const char *service)
726{ 729{
727 char **dirs = rc_ls_dir (NULL, RC_SVCDIR "scheduled", 0); 730 char **dirs = rc_ls_dir (NULL, RC_SVCDIR "scheduled", 0);
728 char **list = NULL; 731 char **list = NULL;
738 } 741 }
739 rc_strlist_free (dirs); 742 rc_strlist_free (dirs);
740 743
741 return (list); 744 return (list);
742} 745}
746librc_hidden_def(rc_services_scheduled_by)
743 747
744char **rc_services_scheduled (const char *service) 748char **rc_services_scheduled (const char *service)
745{ 749{
746 char *svc = rc_xstrdup (service); 750 char *svc = rc_xstrdup (service);
747 char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc), 751 char *dir = rc_strcatpaths (RC_SVCDIR, "scheduled", basename (svc),
753 757
754 free (svc); 758 free (svc);
755 free (dir); 759 free (dir);
756 return (list); 760 return (list);
757} 761}
762librc_hidden_def(rc_services_scheduled)
758 763
759bool rc_allow_plug (char *service) 764bool rc_allow_plug (char *service)
760{ 765{
761 char *list; 766 char *list;
762 char *p; 767 char *p;
791 } 796 }
792 797
793 free (list); 798 free (list);
794 return (allow); 799 return (allow);
795} 800}
801librc_hidden_def(rc_allow_plug)

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

  ViewVC Help
Powered by ViewVC 1.1.20