~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to simpleinit/initctl.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  initctl.c
2
 
 
3
 
    Source file for  initctl  (init(8) control tool).
4
 
 
5
 
    Copyright (C) 2000  Richard Gooch
6
 
 
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.
11
 
 
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.
16
 
 
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.
20
 
 
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.
24
 
*/
25
 
 
26
 
/*
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
30
 
    again.
31
 
    This tool may also be used to inspect the list of currently available
32
 
    services.
33
 
 
34
 
 
35
 
    Written by      Richard Gooch   28-FEB-2000
36
 
 
37
 
    Updated by      Richard Gooch   11-OCT-2000: Added provide support.
38
 
 
39
 
    Last updated by Richard Gooch   6-NOV-2000: Renamed to initctl.c
40
 
 
41
 
 
42
 
*/
43
 
#include <unistd.h>
44
 
#include <stdio.h>
45
 
#include <limits.h>
46
 
#include <sys/types.h>
47
 
#include <sys/stat.h>
48
 
#include <fcntl.h>
49
 
#include <signal.h>
50
 
#include <stdlib.h>
51
 
#include <string.h>
52
 
#include "simpleinit.h"
53
 
 
54
 
 
55
 
static void signal_handler (int sig);
56
 
 
57
 
 
58
 
static int caught_signal = 0;
59
 
 
60
 
 
61
 
int main (int argc, char **argv)
62
 
{
63
 
    int fd, nbytes;
64
 
    struct sigaction sa;
65
 
    sigset_t ss;
66
 
    char *ptr;
67
 
    long *buffer;
68
 
    struct command_struct *command;
69
 
 
70
 
    buffer = calloc(COMMAND_SIZE / sizeof (long) + 1, sizeof(long));
71
 
    if (!buffer) {
72
 
            fprintf (stderr, "Unable allocate buffer for command\n");
73
 
            exit(1);
74
 
    }
75
 
    command = (struct command_struct *) buffer;
76
 
 
77
 
    sigemptyset (&ss);
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);
83
 
    sa.sa_flags = 0;
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];
91
 
    else ++ptr;
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] == '-') )
100
 
    {
101
 
        switch (argv[1][1])
102
 
        {
103
 
          case 'n':
104
 
            command->command = COMMAND_NEED;
105
 
            break;
106
 
          case 'r':
107
 
            command->command = COMMAND_ROLLBACK;
108
 
            break;
109
 
          case 'd':
110
 
            command->command = COMMAND_DUMP_LIST;
111
 
            break;
112
 
          case 'p':
113
 
            command->command = COMMAND_PROVIDE;
114
 
            break;
115
 
          default:
116
 
            fprintf (stderr, "Illegal switch: \"%s\"\n", argv[1]);
117
 
            exit (1);
118
 
            /*break;*/
119
 
        }
120
 
        --argc;
121
 
        ++argv;
122
 
    }
123
 
    switch (command->command)
124
 
    {
125
 
      case COMMAND_NEED:
126
 
      case COMMAND_PROVIDE:
127
 
        if (argc < 2)
128
 
        {
129
 
            fprintf (stderr, "Usage:\tneed|provide programme\n");
130
 
            exit (1);
131
 
        }
132
 
        /*  Fall through  */
133
 
      case COMMAND_ROLLBACK:
134
 
        if (argc > 1) strcpy (command->name, argv[1]);
135
 
        else command->name[0] = '\0';
136
 
        break;
137
 
      case COMMAND_DUMP_LIST:
138
 
        if (tmpnam (command->name) == NULL)
139
 
        {
140
 
            fprintf (stderr, "Unable to create a unique filename\t%s\n",
141
 
                     ERRSTRING);
142
 
            exit (1);
143
 
        }
144
 
        if (mkfifo (command->name, S_IRUSR) != 0)
145
 
        {
146
 
            fprintf (stderr, "Unable to create FIFO: \"%s\"\t%s\n",
147
 
                     command->name, ERRSTRING);
148
 
            exit (1);
149
 
        }
150
 
        break;
151
 
    }
152
 
    if ( ( fd = open ("/dev/initctl", O_WRONLY, 0) ) < 0 )
153
 
    {
154
 
        fprintf (stderr, "Error opening\t%s\n", ERRSTRING);
155
 
        exit (1);
156
 
    }
157
 
    if (write (fd, buffer, COMMAND_SIZE) < COMMAND_SIZE)
158
 
    {
159
 
        fprintf (stderr, "Error writing\t%s\n", ERRSTRING);
160
 
        exit (1);
161
 
    }
162
 
    close (fd);
163
 
    if (command->command != COMMAND_DUMP_LIST)
164
 
    {
165
 
        sigemptyset (&ss);
166
 
        while (caught_signal == 0) sigsuspend (&ss);
167
 
        switch (command->command)
168
 
        {
169
 
          case COMMAND_PROVIDE:
170
 
            switch (caught_signal)
171
 
            {
172
 
              case SIG_PRESENT:
173
 
                return 1;
174
 
              case SIG_NOT_PRESENT:
175
 
                return 0;
176
 
              case SIG_NOT_CHILD:
177
 
                fprintf (stderr, "Error\n");
178
 
                return 2;
179
 
              default:
180
 
                return 3;
181
 
            }
182
 
            break;
183
 
          default:
184
 
            switch (caught_signal)
185
 
            {
186
 
              case SIG_PRESENT:
187
 
                return 0;
188
 
              case SIG_NOT_PRESENT:
189
 
                return 2;
190
 
              case SIG_FAILED:
191
 
                return 1;
192
 
              default:
193
 
                return 3;
194
 
            }
195
 
            break;
196
 
        }
197
 
        return 3;
198
 
    }
199
 
    /*  Read back the data and display it  */
200
 
    if ( ( fd = open (command->name, O_RDONLY, 0) ) < 0 )
201
 
    {
202
 
        fprintf (stderr, "Error opening:\"%s\"\t%s\n",
203
 
                 command->name, ERRSTRING);
204
 
        exit (1);
205
 
    }
206
 
    unlink (command->name);
207
 
    fflush (stdout);
208
 
    while ( ( nbytes = read (fd, buffer, COMMAND_SIZE) ) > 0 )
209
 
        write (1, buffer, nbytes);
210
 
    close (fd);
211
 
    return (0);
212
 
}   /*  End Function main  */
213
 
 
214
 
static void signal_handler (int sig)
215
 
{
216
 
    caught_signal = sig;
217
 
}   /*  End Function signal_handler  */