chiark / gitweb /
service: don't accept negative ERRNO= notification messages
[elogind.git] / src / firstboot / firstboot-generator.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "util.h"
23 #include "mkdir.h"
24
25 static const char *arg_dest = "/tmp";
26
27 static bool is_first_boot(void) {
28         const char *e;
29
30         e = getenv("SYSTEMD_FIRST_BOOT");
31         if (!e)
32                 return false;
33
34         return parse_boolean(e) > 0;
35 }
36
37 int main(int argc, char *argv[]) {
38         int r;
39
40         if (argc > 1 && argc != 4) {
41                 log_error("This program takes three or no arguments.");
42                 return EXIT_FAILURE;
43         }
44
45         if (argc > 1)
46                 arg_dest = argv[2];
47
48         log_set_target(LOG_TARGET_SAFE);
49         log_parse_environment();
50         log_open();
51
52         umask(0022);
53
54         if (is_first_boot()) {
55                 const char *t;
56
57                 t = strappenda(arg_dest, "/default.target.wants/systemd-firstboot.service");
58
59                 mkdir_parents(t, 0755);
60                 if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-firstboot.service", t) < 0 && errno != EEXIST) {
61                         log_error("Failed to create firstboot service symlinks %s: %m", t);
62                         r = -errno;
63                         goto finish;
64                 }
65         }
66
67         r = 0;
68
69 finish:
70         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
71 }