~vorlon/ubuntu/raring/upstart/lp.1199778

« back to all changes in this revision

Viewing changes to init/control.c

  • Committer: Scott James Remnant
  • Date: 2007-02-03 23:41:33 UTC
  • Revision ID: scott@netsplit.com-20070203234133-voo55rjkojkzmh0p
* init/control.c (control_open_sock, control_reopen)
(control_close_handler): Drop these functions; unconnected datagram
sockets don't close -- so why try dealing with it?
(control_error_handler): Don't reopen the socket on error, just log
it -- the socket should be fine, there's no remote end to be lost,
after all.
* init/tests/test_control.c (test_close_handler): Drop.
(test_error_handler): Drop the reopen tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
 
45
45
 
46
46
/* Prototypes for static functions */
47
 
static int  control_open_sock      (void)
48
 
        __attribute__ ((warn_unused_result));
49
 
static void control_reopen         (void);
50
 
static void control_close_handler  (void *data, NihIo *io);
51
 
static void control_error_handler  (void *data, NihIo *io);
 
47
static void control_error_handler  (void  *data, NihIo *io);
52
48
static int  control_job_start      (void *data, pid_t pid,
53
49
                                    UpstartMessageType type, const char *name);
54
50
static int  control_job_stop       (void *data, pid_t pid,
111
107
 
112
108
 
113
109
/**
114
 
 * control_open_sock:
115
 
 *
116
 
 * Opens an upstart control socket for the current process (which should be
117
 
 * pid #1), and marks it to be closed when exec() is called so that it isn't
118
 
 * leaked to child processes.
119
 
 *
120
 
 * Returns: newly opened socket, or -1 on raised error;
121
 
 **/
122
 
static int
123
 
control_open_sock (void)
124
 
{
125
 
        int sock;
126
 
 
127
 
        sock = upstart_open ();
128
 
        if (sock < 0)
129
 
                return -1;
130
 
 
131
 
        if (nih_io_set_cloexec (sock) < 0) {
132
 
                close (sock);
133
 
                return -1;
134
 
        }
135
 
 
136
 
        return sock;
137
 
}
138
 
 
139
 
/**
140
110
 * control_open:
141
111
 *
142
112
 * Opens the control socket and associates it with an NihIo structure
150
120
{
151
121
        int sock;
152
122
 
153
 
        sock = control_open_sock ();
 
123
        sock = upstart_open ();
154
124
        if (sock < 0)
155
125
                return NULL;
156
126
 
 
127
        if (nih_io_set_cloexec (sock) < 0) {
 
128
                close (sock);
 
129
                return NULL;
 
130
        }
 
131
 
157
132
        while (! (control_io = nih_io_reopen (NULL, sock, NIH_IO_MESSAGE,
158
133
                                              (NihIoReader)upstart_message_reader,
159
 
                                              control_close_handler,
160
 
                                              control_error_handler,
 
134
                                              NULL, control_error_handler,
161
135
                                              message_handlers))) {
162
136
                NihError *err;
163
137
 
190
164
}
191
165
 
192
166
/**
193
 
 * control_reopen:
194
 
 *
195
 
 * This function is used to reopen a control socket that has been closed,
196
 
 * or on which an error has occurred.  The error is hopefully cleared, and
197
 
 * we can again continue receiving messages.
198
 
 *
199
 
 * This is all done without losing the state or queues in the NihIo
200
 
 * structure, so if there's no error, the send queue is not lost
201
 
 *
202
 
 * If we fail to reopen the socket, an error is logged and we're left with
203
 
 * no open socket.
204
 
 **/
205
 
static void
206
 
control_reopen (void)
207
 
{
208
 
        int sock;
209
 
 
210
 
        nih_assert (control_io != NULL);
211
 
 
212
 
        close (control_io->watch->fd);
213
 
 
214
 
        sock = control_open_sock ();
215
 
        if (sock < 0) {
216
 
                NihError *err;
217
 
 
218
 
                err = nih_error_get ();
219
 
                nih_error ("%s: %s: %s", _("Unable to re-open control socket"),
220
 
                           err->message, _("you may need to re-exec init"));
221
 
                nih_free (err);
222
 
 
223
 
                nih_free (control_io);
224
 
                control_io = NULL;
225
 
 
226
 
                return;
227
 
        }
228
 
 
229
 
        control_io->watch->fd = sock;
230
 
}
231
 
 
232
 
 
233
 
/**
234
 
 * control_close_handler:
235
 
 * @data: ignored,
236
 
 * @io: NihIo structure closed.
237
 
 *
238
 
 * This function is called should the control socket be unexpectedly closed;
239
 
 * we log the problem and attempt to re-open the control socket.
240
 
 **/
241
 
static void
242
 
control_close_handler (void  *data,
243
 
                       NihIo *io)
244
 
{
245
 
        nih_assert (io != NULL);
246
 
        nih_assert (io == control_io);
247
 
 
248
 
        nih_warn (_("Control socket closed unexpectedly"));
249
 
        control_reopen ();
250
 
}
251
 
 
252
 
/**
253
167
 * control_error_handler:
254
168
 * @data: ignored,
255
169
 * @io: NihIo structure on which an error occurred.
256
170
 *
257
171
 * This function is called should an error occur while reading from or
258
172
 * writing to a descriptor.  We handle errors that we recognise, otherwise
259
 
 * we log them and attempt to re-open the control socket.
 
173
 * we log them and carry on.
260
174
 **/
261
175
static void
262
176
control_error_handler (void  *data,
289
203
        }
290
204
        default:
291
205
                nih_error (_("Error on control socket: %s"), err->message);
292
 
                control_reopen ();
293
206
                break;
294
207
        }
295
208