jail: don't assume positive return value of creat
[project/procd.git] / sysupgrade.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 * Copyright (C) 2017 Matthias Schiffer <mschiffer@universe-factory.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 2.1
8 * as published by the Free Software Foundation
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16
17 #define _GNU_SOURCE
18 #include "watchdog.h"
19 #include "sysupgrade.h"
20
21 #include <ctype.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include <libubox/blobmsg.h>
29
30 void sysupgrade_exec_upgraded(const char *prefix, char *path,
31 const char *backup, char *command,
32 struct blob_attr *options)
33 {
34 char *wdt_fd = watchdog_fd();
35 char *argv[] = { "/sbin/upgraded", NULL, NULL, NULL};
36 struct blob_attr *option;
37 int rem;
38 int ret;
39
40 ret = chroot(prefix);
41 if (ret < 0) {
42 fprintf(stderr, "Failed to chroot for upgraded exec.\n");
43 return;
44 }
45
46 argv[1] = path;
47 argv[2] = command;
48
49 if (wdt_fd) {
50 watchdog_set_cloexec(false);
51 setenv("WDTFD", wdt_fd, 1);
52 }
53
54 if (backup)
55 setenv("UPGRADE_BACKUP", backup, 1);
56
57 blobmsg_for_each_attr(option, options, rem) {
58 const char *prefix = "UPGRADE_OPT_";
59 char value[11];
60 char *name;
61 char *c;
62 int tmp;
63
64 if (asprintf(&name, "%s%s", prefix, blobmsg_name(option)) <= 0)
65 continue;
66 for (c = name + strlen(prefix); *c; c++) {
67 if (isalnum(*c) || *c == '_') {
68 *c = toupper(*c);
69 } else {
70 c = NULL;
71 break;
72 }
73 }
74
75 if (!c) {
76 fprintf(stderr, "Option \"%s\" contains invalid characters\n",
77 blobmsg_name(option));
78 free(name);
79 continue;
80 }
81
82 switch (blobmsg_type(option)) {
83 case BLOBMSG_TYPE_INT32:
84 tmp = blobmsg_get_u32(option);
85 break;
86 case BLOBMSG_TYPE_INT16:
87 tmp = blobmsg_get_u16(option);
88 break;
89 case BLOBMSG_TYPE_INT8:
90 tmp = blobmsg_get_u8(option);
91 break;
92 default:
93 fprintf(stderr, "Option \"%s\" has unsupported type: %d\n",
94 blobmsg_name(option), blobmsg_type(option));
95 free(name);
96 continue;
97 }
98 snprintf(value, sizeof(value), "%u", tmp);
99
100 setenv(name, value, 1);
101
102 free(name);
103 }
104
105 execvp(argv[0], argv);
106
107 /* Cleanup on failure */
108 fprintf(stderr, "Failed to exec upgraded: %s\n", strerror(-errno));
109 unsetenv("WDTFD");
110 watchdog_set_cloexec(true);
111 ret = chroot(".");
112 if (ret < 0) {
113 fprintf(stderr, "Failed to reset chroot, exiting.\n");
114 exit(EXIT_FAILURE);
115 }
116 }