Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <dirent.h>
9#include <errno.h>
10#include <fcntl.h>
11#include <signal.h>
12#include <string.h>
13#include <unistd.h>
14#include <sys/stat.h>
15#include <init.h>
16#include <os.h>
17
18#define UML_DIR "~/.uml/"
19
20#define UMID_LEN 64
21
22/* Changed by set_umid, which is run early in boot */
23static char umid[UMID_LEN] = { 0 };
24
25/* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
26static char *uml_dir = UML_DIR;
27
28static int __init make_uml_dir(void)
29{
30 char dir[512] = { '\0' };
31 int len, err;
32
33 if (*uml_dir == '~') {
34 char *home = getenv("HOME");
35
36 err = -ENOENT;
37 if (home == NULL) {
38 printk(UM_KERN_ERR
39 "%s: no value in environment for $HOME\n",
40 __func__);
41 goto err;
42 }
43 strscpy(dir, home);
44 uml_dir++;
45 }
46 strlcat(dir, uml_dir, sizeof(dir));
47 len = strlen(dir);
48 if (len > 0 && dir[len - 1] != '/')
49 strlcat(dir, "/", sizeof(dir));
50
51 err = -ENOMEM;
52 uml_dir = malloc(strlen(dir) + 1);
53 if (uml_dir == NULL) {
54 printk(UM_KERN_ERR "%s : malloc failed, errno = %d\n",
55 __func__, errno);
56 goto err;
57 }
58 strcpy(uml_dir, dir);
59
60 if ((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)) {
61 printk(UM_KERN_ERR "Failed to mkdir '%s': %s\n",
62 uml_dir, strerror(errno));
63 err = -errno;
64 goto err_free;
65 }
66 return 0;
67
68err_free:
69 free(uml_dir);
70err:
71 uml_dir = NULL;
72 return err;
73}
74
75/*
76 * Unlinks the files contained in @dir and then removes @dir.
77 * Doesn't handle directory trees, so it's not like rm -rf, but almost such. We
78 * ignore ENOENT errors for anything (they happen, strangely enough - possibly
79 * due to races between multiple dying UML threads).
80 */
81static int remove_files_and_dir(char *dir)
82{
83 DIR *directory;
84 struct dirent *ent;
85 int len;
86 char file[256];
87 int ret;
88
89 directory = opendir(dir);
90 if (directory == NULL) {
91 if (errno != ENOENT)
92 return -errno;
93 else
94 return 0;
95 }
96
97 while ((ent = readdir(directory)) != NULL) {
98 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
99 continue;
100 len = strlen(dir) + strlen("/") + strlen(ent->d_name) + 1;
101 if (len > sizeof(file)) {
102 ret = -E2BIG;
103 goto out;
104 }
105
106 sprintf(file, "%s/%s", dir, ent->d_name);
107 if (unlink(file) < 0 && errno != ENOENT) {
108 ret = -errno;
109 goto out;
110 }
111 }
112
113 if (rmdir(dir) < 0 && errno != ENOENT) {
114 ret = -errno;
115 goto out;
116 }
117
118 ret = 0;
119out:
120 closedir(directory);
121 return ret;
122}
123
124/*
125 * This says that there isn't already a user of the specified directory even if
126 * there are errors during the checking. This is because if these errors
127 * happen, the directory is unusable by the pre-existing UML, so we might as
128 * well take it over. This could happen either by
129 * the existing UML somehow corrupting its umid directory
130 * something other than UML sticking stuff in the directory
131 * this boot racing with a shutdown of the other UML
132 * In any of these cases, the directory isn't useful for anything else.
133 *
134 * Boolean return: 1 if in use, 0 otherwise.
135 */
136static inline int is_umdir_used(char *dir)
137{
138 char pid[sizeof("nnnnnnnnn")], *end, *file;
139 int fd, p, n, err;
140 size_t filelen = strlen(dir) + sizeof("/pid") + 1;
141
142 file = malloc(filelen);
143 if (!file)
144 return -ENOMEM;
145
146 snprintf(file, filelen, "%s/pid", dir);
147
148 fd = open(file, O_RDONLY);
149 if (fd < 0) {
150 fd = -errno;
151 if (fd != -ENOENT) {
152 printk(UM_KERN_ERR "is_umdir_used : couldn't open pid "
153 "file '%s', err = %d\n", file, -fd);
154 }
155 goto out;
156 }
157
158 err = 0;
159 n = read(fd, pid, sizeof(pid));
160 if (n < 0) {
161 printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
162 "'%s', err = %d\n", file, errno);
163 goto out_close;
164 } else if (n == 0) {
165 printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
166 "'%s', 0-byte read\n", file);
167 goto out_close;
168 }
169
170 p = strtoul(pid, &end, 0);
171 if (end == pid) {
172 printk(UM_KERN_ERR "is_umdir_used : couldn't parse pid file "
173 "'%s', errno = %d\n", file, errno);
174 goto out_close;
175 }
176
177 if ((kill(p, 0) == 0) || (errno != ESRCH)) {
178 printk(UM_KERN_ERR "umid \"%s\" is already in use by pid %d\n",
179 umid, p);
180 return 1;
181 }
182
183out_close:
184 close(fd);
185out:
186 free(file);
187 return 0;
188}
189
190/*
191 * Try to remove the directory @dir unless it's in use.
192 * Precondition: @dir exists.
193 * Returns 0 for success, < 0 for failure in removal or if the directory is in
194 * use.
195 */
196static int umdir_take_if_dead(char *dir)
197{
198 int ret;
199 if (is_umdir_used(dir))
200 return -EEXIST;
201
202 ret = remove_files_and_dir(dir);
203 if (ret) {
204 printk(UM_KERN_ERR "is_umdir_used - remove_files_and_dir "
205 "failed with err = %d\n", ret);
206 }
207 return ret;
208}
209
210static void __init create_pid_file(void)
211{
212 char pid[sizeof("nnnnnnnnn")], *file;
213 int fd, n;
214
215 n = strlen(uml_dir) + UMID_LEN + sizeof("/pid");
216 file = malloc(n);
217 if (!file)
218 return;
219
220 if (umid_file_name("pid", file, n))
221 goto out;
222
223 fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
224 if (fd < 0) {
225 printk(UM_KERN_ERR "Open of machine pid file \"%s\" failed: "
226 "%s\n", file, strerror(errno));
227 goto out;
228 }
229
230 snprintf(pid, sizeof(pid), "%d\n", getpid());
231 n = write(fd, pid, strlen(pid));
232 if (n != strlen(pid))
233 printk(UM_KERN_ERR "Write of pid file failed - err = %d\n",
234 errno);
235
236 close(fd);
237out:
238 free(file);
239}
240
241int __init set_umid(char *name)
242{
243 if (strlen(name) > UMID_LEN - 1)
244 return -E2BIG;
245
246 strscpy(umid, name);
247
248 return 0;
249}
250
251/* Changed in make_umid, which is called during early boot */
252static int umid_setup = 0;
253
254static int __init make_umid(void)
255{
256 int fd, err;
257 char tmp[256];
258
259 if (umid_setup)
260 return 0;
261
262 make_uml_dir();
263
264 if (*umid == '\0') {
265 strscpy(tmp, uml_dir);
266 strlcat(tmp, "XXXXXX", sizeof(tmp));
267 fd = mkstemp(tmp);
268 if (fd < 0) {
269 printk(UM_KERN_ERR "make_umid - mkstemp(%s) failed: "
270 "%s\n", tmp, strerror(errno));
271 err = -errno;
272 goto err;
273 }
274
275 close(fd);
276
277 set_umid(&tmp[strlen(uml_dir)]);
278
279 /*
280 * There's a nice tiny little race between this unlink and
281 * the mkdir below. It'd be nice if there were a mkstemp
282 * for directories.
283 */
284 if (unlink(tmp)) {
285 err = -errno;
286 goto err;
287 }
288 }
289
290 snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
291 err = mkdir(tmp, 0777);
292 if (err < 0) {
293 err = -errno;
294 if (err != -EEXIST)
295 goto err;
296
297 if (umdir_take_if_dead(tmp) < 0)
298 goto err;
299
300 err = mkdir(tmp, 0777);
301 }
302 if (err) {
303 err = -errno;
304 printk(UM_KERN_ERR "Failed to create '%s' - err = %d\n", umid,
305 errno);
306 goto err;
307 }
308
309 umid_setup = 1;
310
311 create_pid_file();
312
313 err = 0;
314 err:
315 return err;
316}
317
318static int __init make_umid_init(void)
319{
320 if (!make_umid())
321 return 0;
322
323 /*
324 * If initializing with the given umid failed, then try again with
325 * a random one.
326 */
327 printk(UM_KERN_ERR "Failed to initialize umid \"%s\", trying with a "
328 "random umid\n", umid);
329 *umid = '\0';
330 make_umid();
331
332 return 0;
333}
334
335__initcall(make_umid_init);
336
337int __init umid_file_name(char *name, char *buf, int len)
338{
339 int n, err;
340
341 err = make_umid();
342 if (err)
343 return err;
344
345 n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
346 if (n >= len) {
347 printk(UM_KERN_ERR "umid_file_name : buffer too short\n");
348 return -E2BIG;
349 }
350
351 return 0;
352}
353
354char *get_umid(void)
355{
356 return umid;
357}
358
359static int __init set_uml_dir(char *name, int *add)
360{
361 *add = 0;
362
363 if (*name == '\0') {
364 os_warn("uml_dir can't be an empty string\n");
365 return 0;
366 }
367
368 if (name[strlen(name) - 1] == '/') {
369 uml_dir = name;
370 return 0;
371 }
372
373 uml_dir = malloc(strlen(name) + 2);
374 if (uml_dir == NULL) {
375 os_warn("Failed to malloc uml_dir - error = %d\n", errno);
376
377 /*
378 * Return 0 here because do_initcalls doesn't look at
379 * the return value.
380 */
381 return 0;
382 }
383 sprintf(uml_dir, "%s/", name);
384
385 return 0;
386}
387
388__uml_setup("uml_dir=", set_uml_dir,
389"uml_dir=<directory>\n"
390" The location to place the pid and umid files.\n\n"
391);
392
393static void remove_umid_dir(void)
394{
395 char *dir, err;
396
397 dir = malloc(strlen(uml_dir) + UMID_LEN + 1);
398 if (!dir)
399 return;
400
401 sprintf(dir, "%s%s", uml_dir, umid);
402 err = remove_files_and_dir(dir);
403 if (err)
404 os_warn("%s - remove_files_and_dir failed with err = %d\n",
405 __func__, err);
406
407 free(dir);
408}
409
410__uml_exitcall(remove_umid_dir);
1/*
2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <dirent.h>
9#include <errno.h>
10#include <fcntl.h>
11#include <signal.h>
12#include <string.h>
13#include <unistd.h>
14#include <sys/stat.h>
15#include "init.h"
16#include "kern_constants.h"
17#include "os.h"
18#include "user.h"
19
20#define UML_DIR "~/.uml/"
21
22#define UMID_LEN 64
23
24/* Changed by set_umid, which is run early in boot */
25static char umid[UMID_LEN] = { 0 };
26
27/* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
28static char *uml_dir = UML_DIR;
29
30static int __init make_uml_dir(void)
31{
32 char dir[512] = { '\0' };
33 int len, err;
34
35 if (*uml_dir == '~') {
36 char *home = getenv("HOME");
37
38 err = -ENOENT;
39 if (home == NULL) {
40 printk(UM_KERN_ERR "make_uml_dir : no value in "
41 "environment for $HOME\n");
42 goto err;
43 }
44 strlcpy(dir, home, sizeof(dir));
45 uml_dir++;
46 }
47 strlcat(dir, uml_dir, sizeof(dir));
48 len = strlen(dir);
49 if (len > 0 && dir[len - 1] != '/')
50 strlcat(dir, "/", sizeof(dir));
51
52 err = -ENOMEM;
53 uml_dir = malloc(strlen(dir) + 1);
54 if (uml_dir == NULL) {
55 printf("make_uml_dir : malloc failed, errno = %d\n", errno);
56 goto err;
57 }
58 strcpy(uml_dir, dir);
59
60 if ((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)) {
61 printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno));
62 err = -errno;
63 goto err_free;
64 }
65 return 0;
66
67err_free:
68 free(uml_dir);
69err:
70 uml_dir = NULL;
71 return err;
72}
73
74/*
75 * Unlinks the files contained in @dir and then removes @dir.
76 * Doesn't handle directory trees, so it's not like rm -rf, but almost such. We
77 * ignore ENOENT errors for anything (they happen, strangely enough - possibly
78 * due to races between multiple dying UML threads).
79 */
80static int remove_files_and_dir(char *dir)
81{
82 DIR *directory;
83 struct dirent *ent;
84 int len;
85 char file[256];
86 int ret;
87
88 directory = opendir(dir);
89 if (directory == NULL) {
90 if (errno != ENOENT)
91 return -errno;
92 else
93 return 0;
94 }
95
96 while ((ent = readdir(directory)) != NULL) {
97 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
98 continue;
99 len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1;
100 if (len > sizeof(file)) {
101 ret = -E2BIG;
102 goto out;
103 }
104
105 sprintf(file, "%s/%s", dir, ent->d_name);
106 if (unlink(file) < 0 && errno != ENOENT) {
107 ret = -errno;
108 goto out;
109 }
110 }
111
112 if (rmdir(dir) < 0 && errno != ENOENT) {
113 ret = -errno;
114 goto out;
115 }
116
117 ret = 0;
118out:
119 closedir(directory);
120 return ret;
121}
122
123/*
124 * This says that there isn't already a user of the specified directory even if
125 * there are errors during the checking. This is because if these errors
126 * happen, the directory is unusable by the pre-existing UML, so we might as
127 * well take it over. This could happen either by
128 * the existing UML somehow corrupting its umid directory
129 * something other than UML sticking stuff in the directory
130 * this boot racing with a shutdown of the other UML
131 * In any of these cases, the directory isn't useful for anything else.
132 *
133 * Boolean return: 1 if in use, 0 otherwise.
134 */
135static inline int is_umdir_used(char *dir)
136{
137 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
138 char pid[sizeof("nnnnn\0")], *end;
139 int dead, fd, p, n, err;
140
141 n = snprintf(file, sizeof(file), "%s/pid", dir);
142 if (n >= sizeof(file)) {
143 printk(UM_KERN_ERR "is_umdir_used - pid filename too long\n");
144 err = -E2BIG;
145 goto out;
146 }
147
148 dead = 0;
149 fd = open(file, O_RDONLY);
150 if (fd < 0) {
151 fd = -errno;
152 if (fd != -ENOENT) {
153 printk(UM_KERN_ERR "is_umdir_used : couldn't open pid "
154 "file '%s', err = %d\n", file, -fd);
155 }
156 goto out;
157 }
158
159 err = 0;
160 n = read(fd, pid, sizeof(pid));
161 if (n < 0) {
162 printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
163 "'%s', err = %d\n", file, errno);
164 goto out_close;
165 } else if (n == 0) {
166 printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
167 "'%s', 0-byte read\n", file);
168 goto out_close;
169 }
170
171 p = strtoul(pid, &end, 0);
172 if (end == pid) {
173 printk(UM_KERN_ERR "is_umdir_used : couldn't parse pid file "
174 "'%s', errno = %d\n", file, errno);
175 goto out_close;
176 }
177
178 if ((kill(p, 0) == 0) || (errno != ESRCH)) {
179 printk(UM_KERN_ERR "umid \"%s\" is already in use by pid %d\n",
180 umid, p);
181 return 1;
182 }
183
184out_close:
185 close(fd);
186out:
187 return 0;
188}
189
190/*
191 * Try to remove the directory @dir unless it's in use.
192 * Precondition: @dir exists.
193 * Returns 0 for success, < 0 for failure in removal or if the directory is in
194 * use.
195 */
196static int umdir_take_if_dead(char *dir)
197{
198 int ret;
199 if (is_umdir_used(dir))
200 return -EEXIST;
201
202 ret = remove_files_and_dir(dir);
203 if (ret) {
204 printk(UM_KERN_ERR "is_umdir_used - remove_files_and_dir "
205 "failed with err = %d\n", ret);
206 }
207 return ret;
208}
209
210static void __init create_pid_file(void)
211{
212 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
213 char pid[sizeof("nnnnn\0")];
214 int fd, n;
215
216 if (umid_file_name("pid", file, sizeof(file)))
217 return;
218
219 fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
220 if (fd < 0) {
221 printk(UM_KERN_ERR "Open of machine pid file \"%s\" failed: "
222 "%s\n", file, strerror(errno));
223 return;
224 }
225
226 snprintf(pid, sizeof(pid), "%d\n", getpid());
227 n = write(fd, pid, strlen(pid));
228 if (n != strlen(pid))
229 printk(UM_KERN_ERR "Write of pid file failed - err = %d\n",
230 errno);
231
232 close(fd);
233}
234
235int __init set_umid(char *name)
236{
237 if (strlen(name) > UMID_LEN - 1)
238 return -E2BIG;
239
240 strlcpy(umid, name, sizeof(umid));
241
242 return 0;
243}
244
245/* Changed in make_umid, which is called during early boot */
246static int umid_setup = 0;
247
248static int __init make_umid(void)
249{
250 int fd, err;
251 char tmp[256];
252
253 if (umid_setup)
254 return 0;
255
256 make_uml_dir();
257
258 if (*umid == '\0') {
259 strlcpy(tmp, uml_dir, sizeof(tmp));
260 strlcat(tmp, "XXXXXX", sizeof(tmp));
261 fd = mkstemp(tmp);
262 if (fd < 0) {
263 printk(UM_KERN_ERR "make_umid - mkstemp(%s) failed: "
264 "%s\n", tmp, strerror(errno));
265 err = -errno;
266 goto err;
267 }
268
269 close(fd);
270
271 set_umid(&tmp[strlen(uml_dir)]);
272
273 /*
274 * There's a nice tiny little race between this unlink and
275 * the mkdir below. It'd be nice if there were a mkstemp
276 * for directories.
277 */
278 if (unlink(tmp)) {
279 err = -errno;
280 goto err;
281 }
282 }
283
284 snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
285 err = mkdir(tmp, 0777);
286 if (err < 0) {
287 err = -errno;
288 if (err != -EEXIST)
289 goto err;
290
291 if (umdir_take_if_dead(tmp) < 0)
292 goto err;
293
294 err = mkdir(tmp, 0777);
295 }
296 if (err) {
297 err = -errno;
298 printk(UM_KERN_ERR "Failed to create '%s' - err = %d\n", umid,
299 errno);
300 goto err;
301 }
302
303 umid_setup = 1;
304
305 create_pid_file();
306
307 err = 0;
308 err:
309 return err;
310}
311
312static int __init make_umid_init(void)
313{
314 if (!make_umid())
315 return 0;
316
317 /*
318 * If initializing with the given umid failed, then try again with
319 * a random one.
320 */
321 printk(UM_KERN_ERR "Failed to initialize umid \"%s\", trying with a "
322 "random umid\n", umid);
323 *umid = '\0';
324 make_umid();
325
326 return 0;
327}
328
329__initcall(make_umid_init);
330
331int __init umid_file_name(char *name, char *buf, int len)
332{
333 int n, err;
334
335 err = make_umid();
336 if (err)
337 return err;
338
339 n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
340 if (n >= len) {
341 printk(UM_KERN_ERR "umid_file_name : buffer too short\n");
342 return -E2BIG;
343 }
344
345 return 0;
346}
347
348char *get_umid(void)
349{
350 return umid;
351}
352
353static int __init set_uml_dir(char *name, int *add)
354{
355 if (*name == '\0') {
356 printf("uml_dir can't be an empty string\n");
357 return 0;
358 }
359
360 if (name[strlen(name) - 1] == '/') {
361 uml_dir = name;
362 return 0;
363 }
364
365 uml_dir = malloc(strlen(name) + 2);
366 if (uml_dir == NULL) {
367 printf("Failed to malloc uml_dir - error = %d\n", errno);
368
369 /*
370 * Return 0 here because do_initcalls doesn't look at
371 * the return value.
372 */
373 return 0;
374 }
375 sprintf(uml_dir, "%s/", name);
376
377 return 0;
378}
379
380__uml_setup("uml_dir=", set_uml_dir,
381"uml_dir=<directory>\n"
382" The location to place the pid and umid files.\n\n"
383);
384
385static void remove_umid_dir(void)
386{
387 char dir[strlen(uml_dir) + UMID_LEN + 1], err;
388
389 sprintf(dir, "%s%s", uml_dir, umid);
390 err = remove_files_and_dir(dir);
391 if (err)
392 printf("remove_umid_dir - remove_files_and_dir failed with "
393 "err = %d\n", err);
394}
395
396__uml_exitcall(remove_umid_dir);