3
Source file for initctl (init(8) control tool).
5
Copyright (C) 2000 Richard Gooch
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
Richard Gooch may be reached by email at rgooch@atnf.csiro.au
22
The postal address is:
23
Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
27
This tool will send control messages to init(8). For example, it may
28
request init(8) to start a service and will wait for that service to be
29
available. If the service is already available, init(8) will not start it
31
This tool may also be used to inspect the list of currently available
35
Written by Richard Gooch 28-FEB-2000
37
Updated by Richard Gooch 11-OCT-2000: Added provide support.
39
Last updated by Richard Gooch 6-NOV-2000: Renamed to initctl.c
46
#include <sys/types.h>
52
#include "simpleinit.h"
55
static void signal_handler (int sig);
58
static int caught_signal = 0;
61
int main (int argc, char **argv)
68
struct command_struct *command;
70
buffer = calloc(COMMAND_SIZE / sizeof (long) + 1, sizeof(long));
72
fprintf (stderr, "Unable allocate buffer for command\n");
75
command = (struct command_struct *) buffer;
78
sigaddset (&ss, SIG_PRESENT);
79
sigaddset (&ss, SIG_NOT_PRESENT);
80
sigaddset (&ss, SIG_FAILED);
81
sigprocmask (SIG_BLOCK, &ss, NULL);
82
sigemptyset (&sa.sa_mask);
84
sa.sa_handler = signal_handler;
85
sigaction (SIG_PRESENT, &sa, NULL);
86
sigaction (SIG_NOT_PRESENT, &sa, NULL);
87
sigaction (SIG_FAILED, &sa, NULL);
88
command->pid = getpid ();
89
command->ppid = getppid ();
90
if ( ( ptr = strrchr (argv[0], '/') ) == NULL ) ptr = argv[0];
92
/* First generate command number by looking at invocation name */
93
if (strcmp (ptr, "display-services") == 0)
94
command->command = COMMAND_DUMP_LIST;
95
else if (strcmp (ptr, "need") == 0) command->command = COMMAND_NEED;
96
else if (strcmp (ptr, "provide") == 0) command->command = COMMAND_PROVIDE;
97
else command->command = COMMAND_TEST;
98
/* Now check for switches */
99
if ( (argc > 1) && (argv[1][0] == '-') )
104
command->command = COMMAND_NEED;
107
command->command = COMMAND_ROLLBACK;
110
command->command = COMMAND_DUMP_LIST;
113
command->command = COMMAND_PROVIDE;
116
fprintf (stderr, "Illegal switch: \"%s\"\n", argv[1]);
123
switch (command->command)
126
case COMMAND_PROVIDE:
129
fprintf (stderr, "Usage:\tneed|provide programme\n");
133
case COMMAND_ROLLBACK:
134
if (argc > 1) strcpy (command->name, argv[1]);
135
else command->name[0] = '\0';
137
case COMMAND_DUMP_LIST:
138
if (tmpnam (command->name) == NULL)
140
fprintf (stderr, "Unable to create a unique filename\t%s\n",
144
if (mkfifo (command->name, S_IRUSR) != 0)
146
fprintf (stderr, "Unable to create FIFO: \"%s\"\t%s\n",
147
command->name, ERRSTRING);
152
if ( ( fd = open ("/dev/initctl", O_WRONLY, 0) ) < 0 )
154
fprintf (stderr, "Error opening\t%s\n", ERRSTRING);
157
if (write (fd, buffer, COMMAND_SIZE) < COMMAND_SIZE)
159
fprintf (stderr, "Error writing\t%s\n", ERRSTRING);
163
if (command->command != COMMAND_DUMP_LIST)
166
while (caught_signal == 0) sigsuspend (&ss);
167
switch (command->command)
169
case COMMAND_PROVIDE:
170
switch (caught_signal)
174
case SIG_NOT_PRESENT:
177
fprintf (stderr, "Error\n");
184
switch (caught_signal)
188
case SIG_NOT_PRESENT:
199
/* Read back the data and display it */
200
if ( ( fd = open (command->name, O_RDONLY, 0) ) < 0 )
202
fprintf (stderr, "Error opening:\"%s\"\t%s\n",
203
command->name, ERRSTRING);
206
unlink (command->name);
208
while ( ( nbytes = read (fd, buffer, COMMAND_SIZE) ) > 0 )
209
write (1, buffer, nbytes);
212
} /* End Function main */
214
static void signal_handler (int sig)
217
} /* End Function signal_handler */