chiark / gitweb /
test: allow deletion of temporary files from normal fs
[elogind.git] / src / system-update-generator / system-update-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 2012 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 <errno.h>
23 #include <unistd.h>
24
25 #include "log.h"
26 #include "util.h"
27 #include "unit-name.h"
28 #include "path-util.h"
29
30 /*
31  * Implements the logic described in
32  * http://freedesktop.org/wiki/Software/systemd/SystemUpdates
33  */
34
35 static const char *arg_dest = "/tmp";
36
37 static int generate_symlink(void) {
38         struct stat st;
39         char *p;
40
41         if (lstat("/system-update", &st) < 0) {
42                 if (errno == ENOENT)
43                         return 0;
44
45                 log_error("Failed to check for system update: %m");
46                 return -EINVAL;
47         }
48
49         p = strappend(arg_dest, "/default.target");
50         if (!p)
51                 return log_oom();
52
53         if (symlink(SYSTEM_DATA_UNIT_PATH "/system-update.target", p) < 0) {
54                 free(p);
55                 log_error("Failed to create symlink: %m");
56                 return -errno;
57         }
58
59         free(p);
60
61         return 0;
62 }
63
64 int main(int argc, char *argv[]) {
65         int r;
66
67         if (argc > 1 && argc != 4) {
68                 log_error("This program takes three or no arguments.");
69                 return EXIT_FAILURE;
70         }
71
72         if (argc > 1)
73                 arg_dest = argv[2];
74
75         log_set_target(LOG_TARGET_SAFE);
76         log_parse_environment();
77         log_open();
78
79         umask(0022);
80
81         r = generate_symlink();
82
83         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
84 }