chiark / gitweb /
base-filesystem: create /lib64 symlink to libdir /usr directory
[elogind.git] / src / shared / base-filesystem.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 Kay Sievers
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 <sys/stat.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <unistd.h>
28
29 #include "base-filesystem.h"
30 #include "log.h"
31 #include "macro.h"
32 #include "strv.h"
33 #include "util.h"
34 #include "label.h"
35 #include "mkdir.h"
36
37 typedef struct BaseFilesystem {
38         const char *dir;
39         mode_t mode;
40         const char *target;
41 } BaseFilesystem;
42
43 static const BaseFilesystem table[] = {
44         { "bin",      0, "usr/bin" },
45         { "lib",      0, "usr/lib" },
46 #if defined(__i386__) || defined(__x86_64__)
47         { "lib64",    0, "usr/lib/x86_64-linux-gnu\0usr/lib64" },
48 #endif
49         { "root",  0755, NULL },
50         { "sbin",     0, "usr/sbin" },
51 };
52
53 int base_filesystem_create(const char *root) {
54         _cleanup_close_ int fd = -1;
55         unsigned i;
56         int r;
57
58         fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
59         if (fd < 0)
60                 return -errno;
61
62         for (i = 0; i < ELEMENTSOF(table); i ++) {
63                 if (table[i].target) {
64                         const char *target = NULL;
65                         const char *s;
66
67                         /* check if one of the targets exists */
68                         NULSTR_FOREACH(s, table[i].target) {
69                                 if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
70                                         continue;
71
72                                 target = s;
73                                 break;
74                         }
75
76                         if (!target)
77                                 continue;
78
79                         r = symlinkat(target, fd, table[i].dir);
80                         if (r < 0 && errno != EEXIST) {
81                                 log_error("Failed to create symlink at %s/%s: %m", root, table[i].dir);
82                                 return -errno;
83                         }
84                         continue;
85                 }
86
87                 RUN_WITH_UMASK(0000)
88                         r = mkdirat(fd, table[i].dir, table[i].mode);
89                 if (r < 0 && errno != EEXIST) {
90                         log_error("Failed to create directory at %s/%s: %m", root, table[i].dir);
91                         return -errno;
92                 }
93         }
94
95         return 0;
96 }