1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/* need.c
Source file for need (init(8) dependency tool).
Copyright (C) 2000 Richard Gooch
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Richard Gooch may be reached by email at rgooch@atnf.csiro.au
The postal address is:
Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
*/
/*
This tool will request init(8) to start a service and will wait for that
service to be available. If the service is already available, init(8) will
not start it again.
This tool may also be used to inspect the list of currently available
services.
Written by Richard Gooch 28-FEB-2000
Last updated by Richard Gooch 28-FEB-2000
*/
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include "simpleinit.h"
static void signal_handler (int sig);
static int caught_signal = 0;
int main (int argc, char **argv)
{
int fd, nbytes;
struct sigaction sa;
sigset_t ss;
char *ptr;
long buffer[COMMAND_SIZE / sizeof (long)];
struct command_struct *command = (struct command_struct *) buffer;
sigemptyset (&ss);
sigaddset (&ss, SIG_PRESENT);
sigaddset (&ss, SIG_NOT_PRESENT);
sigaddset (&ss, SIG_FAILED);
sigprocmask (SIG_BLOCK, &ss, NULL);
sigemptyset (&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = signal_handler;
sigaction (SIG_PRESENT, &sa, NULL);
sigaction (SIG_NOT_PRESENT, &sa, NULL);
sigaction (SIG_FAILED, &sa, NULL);
command->pid = getpid ();
if ( ( ptr = strrchr (argv[0], '/') ) == NULL ) ptr = argv[0];
else ++ptr;
if (strcmp (ptr, "display-services") == 0)
{
command->command = COMMAND_DUMP_LIST;
if (tmpnam (command->name) == NULL)
{
fprintf (stderr, "Unable to create a unique filename\t%s\n",
ERRSTRING);
exit (1);
}
if (mkfifo (command->name, S_IRUSR) != 0)
{
fprintf (stderr, "Unable to create FIFO: \"%s\"\t%s\n",
command->name, ERRSTRING);
exit (1);
}
}
else
{
if ( (argc > 1) && (strcmp (argv[1], "-r") == 0) )
{
command->command = COMMAND_ROLLBACK;
--argc;
++argv;
}
else command->command = COMMAND_NEED;
if (argc == 2) strcpy (command->name, argv[1]);
else
{
if (command->command == COMMAND_ROLLBACK) command->name[0] = '\0';
else
{
fprintf (stderr, "Usage:\tneed programme\n");
exit (1);
}
}
}
if ( ( fd = open ("/dev/initctl", O_WRONLY, 0) ) < 0 )
{
fprintf (stderr, "Error opening\t%s\n", ERRSTRING);
exit (1);
}
if (write (fd, buffer, COMMAND_SIZE) < COMMAND_SIZE)
{
fprintf (stderr, "Error writing\t%s\n", ERRSTRING);
exit (1);
}
close (fd);
if (command->command != COMMAND_DUMP_LIST)
{
sigemptyset (&ss);
while (caught_signal == 0) sigsuspend (&ss);
switch (caught_signal)
{
case SIG_PRESENT:
return 0;
case SIG_NOT_PRESENT:
return 2;
case SIG_FAILED:
return 1;
}
return 3;
}
/* Read back the data and display it */
if ( ( fd = open (command->name, O_RDONLY, 0) ) < 0 )
{
fprintf (stderr, "Error opening:\"%s\"\t%s\n",
command->name, ERRSTRING);
exit (1);
}
unlink (command->name);
fflush (stdout);
while ( ( nbytes = read (fd, buffer, COMMAND_SIZE) ) > 0 )
write (1, buffer, nbytes);
close (fd);
return (0);
} /* End Function main */
static void signal_handler (int sig)
{
caught_signal = sig;
} /* End Function signal_handler */
|