summaryrefslogtreecommitdiffstats
path: root/src/shutdownd/shutdownd.c
blob: 4c9c149a2dcbeef314d35ae532fb3e9151aa7899 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2010 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <sys/socket.h>
#include <sys/poll.h>
#include <sys/types.h>
#include <sys/timerfd.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>

#include <systemd/sd-daemon.h>
#include <systemd/sd-shutdown.h>

#include "log.h"
#include "macro.h"
#include "util.h"
#include "utmp-wtmp.h"
#include "mkdir.h"
#include "fileio.h"

union shutdown_buffer {
        struct sd_shutdown_command command;
        char space[offsetof(struct sd_shutdown_command, wall_message) + LINE_MAX];
};

static int read_packet(int fd, union shutdown_buffer *_b) {
        struct ucred *ucred;
        ssize_t n;

        union shutdown_buffer b; /* We maintain our own copy here, in
                                  * order not to corrupt the last message */
        struct iovec iovec = {
                iovec.iov_base = &b,
                iovec.iov_len = sizeof(b) - 1,
        };
        union {
                struct cmsghdr cmsghdr;
                uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
        } control = {};
        struct msghdr msghdr = {
                .msg_iov = &iovec,
                msghdr.msg_iovlen = 1,
                msghdr.msg_control = &control,
                msghdr.msg_controllen = sizeof(control),
        };

        assert(fd >= 0);
        assert(_b);

        n = recvmsg(fd, &msghdr, MSG_DONTWAIT);
        if (n <= 0) {
                if (n == 0) {
                        log_error("Short read");
                        return -EIO;
                }

                if (errno == EAGAIN || errno == EINTR)
                        return 0;

                log_error("recvmsg(): %m");
                return -errno;
        }

        if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
            control.cmsghdr.cmsg_level != SOL_SOCKET ||
            control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
            control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
                log_warning("Received message without credentials. Ignoring.");
                return 0;
        }

        ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
        if (ucred->uid != 0) {
                log_warning("Got request from unprivileged user. Ignoring.");
                return 0;
        }

        if ((size_t) n < offsetof(struct sd_shutdown_command, wall_message)) {
                log_warning("Message has invalid size. Ignoring.");
                return 0;
        }

        if (b.command.mode != SD_SHUTDOWN_NONE &&
            b.command.mode != SD_SHUTDOWN_REBOOT &&
            b.command.mode != SD_SHUTDOWN_POWEROFF &&
            b.command.mode != SD_SHUTDOWN_HALT &&
            b.command.mode != SD_SHUTDOWN_KEXEC) {
                log_warning("Message has invalid mode. Ignoring.");
                return 0;
        }

        b.space[n] = 0;

        *_b = b;
        return 1;
}

static void warn_wall(usec_t n, struct sd_shutdown_command *c) {
        char date[FORMAT_TIMESTAMP_MAX];
        const char *prefix;
        char *l = NULL;

        assert(c);
        assert(c->warn_wall);

        if (n >= c->usec)
                return;

        if (c->mode == SD_SHUTDOWN_HALT)
                prefix = "The system is going down for system halt at ";
        else if (c->mode == SD_SHUTDOWN_POWEROFF)
                prefix = "The system is going down for power-off at ";
        else if (c->mode == SD_SHUTDOWN_REBOOT)
                prefix = "The system is going down for reboot at ";
        else if (c->mode == SD_SHUTDOWN_KEXEC)
                prefix = "The system is going down for kexec reboot at ";
        else if (c->mode == SD_SHUTDOWN_NONE)
                prefix = "The system shutdown has been cancelled at ";
        else
                assert_not_reached("Unknown mode!");

        if (asprintf(&l, "%s%s%s%s!", c->wall_message, c->wall_message[0] ? "\n" : "",
                     prefix, format_timestamp(date, sizeof(date), c->usec)) < 0)
                log_error("Failed to allocate wall message");
        else {
                utmp_wall(l, NULL);
                free(l);
        }
}

static usec_t when_wall(usec_t n, usec_t elapse) {

        static const struct {
                usec_t delay;
                usec_t interval;
        } table[] = {
                { 0,                    USEC_PER_MINUTE      },
                { 10 * USEC_PER_MINUTE, 15 * USEC_PER_MINUTE },
                { USEC_PER_HOUR,        30 * USEC_PER_MINUTE },
                { 3 * USEC_PER_HOUR,    USEC_PER_HOUR        },
        };

        usec_t left, sub;
        unsigned i = ELEMENTSOF(table) - 1;

        /* If the time is already passed, then don't announce */
        if (n >= elapse)
                return 0;

        left = elapse - n;
        while (left < table[i].delay)
                i--;
        sub = (left / table[i].interval) * table[i].interval;

        assert(sub < elapse);
        return elapse - sub;
}

static usec_t when_nologin(usec_t elapse) {
        return elapse > 5*USEC_PER_MINUTE ? elapse - 5*USEC_PER_MINUTE : 1;
}

static const char *mode_to_string(enum sd_shutdown_mode m) {
        switch (m) {
        case SD_SHUTDOWN_REBOOT:
                return "reboot";
        case SD_SHUTDOWN_POWEROFF:
                return "poweroff";
        case SD_SHUTDOWN_HALT:
                return "halt";
        case SD_SHUTDOWN_KEXEC:
                return "kexec";
        default:
                return NULL;
        }
}

static int update_schedule_file(struct sd_shutdown_command *c) {
        int r;
        FILE *f;
        char *temp_path, *t;

        assert(c);

        r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0);
        if (r < 0) {
                log_error("Failed to create shutdown subdirectory: %s", strerror(-r));
                return r;
        }

        t = cescape(c->wall_message);
        if (!t)
                return log_oom();

        r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
        if (r < 0) {
                log_error("Failed to save information about scheduled shutdowns: %s", strerror(-r));
                free(t);
                return r;
        }

        fchmod(fileno(f), 0644);

        fprintf(f,
                "USEC=%llu\n"
                "WARN_WALL=%i\n"
                "MODE=%s\n",
                (unsigned long long) c->usec,
                c->warn_wall,
                mode_to_string(c->mode));

        if (c->dry_run)
                fputs("DRY_RUN=1\n", f);

        if (!isempty(t))
                fprintf(f, "WALL_MESSAGE=%s\n", t);

        free(t);

        fflush(f);

        if (ferror(f) || rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
                log_error("Failed to write information about scheduled shutdowns: %m");
                r = -errno;

                unlink(temp_path);
                unlink("/run/systemd/shutdown/scheduled");
        }

        fclose(f);
        free(temp_path);

        return r;
}

static bool scheduled(struct sd_shutdown_command *c) {
        return c->usec > 0 && c->mode != SD_SHUTDOWN_NONE;
}

int main(int argc, char *argv[]) {
        enum {
                FD_SOCKET,
                FD_WALL_TIMER,
                FD_NOLOGIN_TIMER,
                FD_SHUTDOWN_TIMER,
                _FD_MAX
        };

        int r = EXIT_FAILURE, n_fds;
        union shutdown_buffer b = {};
        struct pollfd pollfd[_FD_MAX] = {};
        bool exec_shutdown = false, unlink_nologin = false;
        unsigned i;

        if (getppid() != 1) {
                log_error("This program should be invoked by init only.");
                return EXIT_FAILURE;
        }

        if (argc > 1) {
                log_error("This program does not take arguments.");
                return EXIT_FAILURE;
        }

        log_set_target(LOG_TARGET_AUTO);
        log_parse_environment();
        log_open();

        umask(0022);

        n_fds = sd_listen_fds(true);
        if (n_fds < 0) {
                log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
                return EXIT_FAILURE;
        }

        if (n_fds != 1) {
                log_error("Need exactly one file descriptor.");
                return EXIT_FAILURE;
        }

        pollfd[FD_SOCKET].fd = SD_LISTEN_FDS_START;
        pollfd[FD_SOCKET].events = POLLIN;

        for (i = FD_WALL_TIMER; i < _FD_MAX; i++) {
                pollfd[i].events = POLLIN;
                pollfd[i].fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
                if (pollfd[i].fd < 0) {
                        log_error("timerfd_create(): %m");
                        goto finish;
                }
        }

        log_debug("systemd-shutdownd running as pid %lu", (unsigned long) getpid());

        sd_notify(false,
                  "READY=1\n"
                  "STATUS=Processing requests...");

        for (;;) {
                int k;
                usec_t n;

                k = poll(pollfd, _FD_MAX, scheduled(&b.command) ? -1 : 0);
                if (k < 0) {

                        if (errno == EAGAIN || errno == EINTR)
                                continue;

                        log_error("poll(): %m");
                        goto finish;
                }

                /* Exit on idle */
                if (k == 0)
                        break;

                n = now(CLOCK_REALTIME);

                if (pollfd[FD_SOCKET].revents) {

                        k = read_packet(pollfd[FD_SOCKET].fd, &b);
                        if (k < 0)
                                goto finish;
                        else if (k > 0) {
                                struct itimerspec its;
                                char date[FORMAT_TIMESTAMP_MAX];

                                if (!scheduled(&b.command)) {
                                        log_info("Shutdown canceled.");
                                        if (b.command.warn_wall)
                                                warn_wall(0, &b.command);
                                        break;
                                }

                                zero(its);
                                if (b.command.warn_wall) {
                                        /* Send wall messages every so often */
                                        timespec_store(&its.it_value, when_wall(n, b.command.usec));

                                        /* Warn immediately if less than 15 minutes are left */
                                        if (n < b.command.usec &&
                                            n + 15*USEC_PER_MINUTE >= b.command.usec)
                                                warn_wall(n, &b.command);
                                }
                                if (timerfd_settime(pollfd[FD_WALL_TIMER].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) {
                                        log_error("timerfd_settime(): %m");
                                        goto finish;
                                }

                                /* Disallow logins 5 minutes prior to shutdown */
                                zero(its);
                                timespec_store(&its.it_value, when_nologin(b.command.usec));
                                if (timerfd_settime(pollfd[FD_NOLOGIN_TIMER].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) {
                                        log_error("timerfd_settime(): %m");
                                        goto finish;
                                }

                                /* Shutdown after the specified time is reached */
                                zero(its);
                                timespec_store(&its.it_value, b.command.usec);
                                if (timerfd_settime(pollfd[FD_SHUTDOWN_TIMER].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) {
                                        log_error("timerfd_settime(): %m");
                                        goto finish;
                                }

                                update_schedule_file(&b.command);

                                sd_notifyf(false,
                                           "STATUS=Shutting down at %s (%s)...",
                                           format_timestamp(date, sizeof(date), b.command.usec),
                                           mode_to_string(b.command.mode));

                                log_info("Shutting down at %s (%s)...", date, mode_to_string(b.command.mode));
                        }
                }

                if (pollfd[FD_WALL_TIMER].revents) {
                        struct itimerspec its = {};

                        warn_wall(n, &b.command);
                        flush_fd(pollfd[FD_WALL_TIMER].fd);

                        /* Restart timer */
                        timespec_store(&its.it_value, when_wall(n, b.command.usec));
                        if (timerfd_settime(pollfd[FD_WALL_TIMER].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) {
                                log_error("timerfd_settime(): %m");
                                goto finish;
                        }
                }

                if (pollfd[FD_NOLOGIN_TIMER].revents) {
                        int e;

                        log_info("Creating /run/nologin, blocking further logins...");

                        e = write_string_file_atomic("/run/nologin", "System is going down.");
                        if (e < 0)
                                log_error("Failed to create /run/nologin: %s", strerror(-e));
                        else
                                unlink_nologin = true;

                        flush_fd(pollfd[FD_NOLOGIN_TIMER].fd);
                }

                if (pollfd[FD_SHUTDOWN_TIMER].revents) {
                        exec_shutdown = true;
                        goto finish;
                }
        }

        r = EXIT_SUCCESS;

        log_debug("systemd-shutdownd stopped as pid %lu", (unsigned long) getpid());

finish:

        for (i = 0; i < _FD_MAX; i++)
                if (pollfd[i].fd >= 0)
                        close_nointr_nofail(pollfd[i].fd);

        if (unlink_nologin)
                unlink("/run/nologin");

        unlink("/run/systemd/shutdown/scheduled");

        if (exec_shutdown && !b.command.dry_run) {
                char sw[3];

                sw[0] = '-';
                sw[1] = b.command.mode;
                sw[2] = 0;

                execl(SYSTEMCTL_BINARY_PATH,
                      "shutdown",
                      sw,
                      "now",
                      (b.command.warn_wall && b.command.wall_message[0]) ? b.command.wall_message :
                      (b.command.warn_wall ? NULL : "--no-wall"),
                      NULL);

                log_error("Failed to execute /sbin/shutdown: %m");
        }

        sd_notify(false,
                  "STATUS=Exiting...");

        return r;
}