Gentoo Archives: gentoo-commits

From: "Robin H. Johnson" <robbat2@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/openrc:master commit in: man/, src/rc/
Date: Tue, 10 Jan 2012 03:22:52
Message-Id: 66f4305e1cb596c3243a67e83beed0cb6d973eb8.robbat2@gentoo
1 commit: 66f4305e1cb596c3243a67e83beed0cb6d973eb8
2 Author: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
3 AuthorDate: Tue Jan 10 03:20:47 2012 +0000
4 Commit: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org>
5 CommitDate: Tue Jan 10 03:20:47 2012 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/openrc.git;a=commit;h=66f4305e
7
8 rc/checkpath: tmpfiles.d backend creation code
9
10 This commit provides the checkpath applet with feature parity to
11 systemd's tmpfiles.c create_item function.
12
13 Very similarly to the systemd function, it does NOT do any of the
14 cleanup work in this function.
15
16 Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org>
17
18 ---
19 man/runscript.8 | 3 ++
20 src/rc/checkpath.c | 95 ++++++++++++++++++++++++++++++++++++---------------
21 2 files changed, 70 insertions(+), 28 deletions(-)
22
23 diff --git a/man/runscript.8 b/man/runscript.8
24 index 08f302e..6ad6031 100644
25 --- a/man/runscript.8
26 +++ b/man/runscript.8
27 @@ -296,8 +296,11 @@ Mark the service as coldplugged.
28 Mark the service as inactive.
29 .It Xo
30 .Ic checkpath
31 +.Op Fl D , -directory-truncate
32 .Op Fl d , -directory
33 +.Op Fl F , -file-truncate
34 .Op Fl f , -file
35 +.Op Fl p , -pipe
36 .Op Fl m , -mode Ar mode
37 .Op Fl o , owner Ar owner
38 .Ar path ...
39
40 diff --git a/src/rc/checkpath.c b/src/rc/checkpath.c
41 index c8bd8ad..4ad4ea7 100644
42 --- a/src/rc/checkpath.c
43 +++ b/src/rc/checkpath.c
44 @@ -32,6 +32,7 @@
45 #include <sys/types.h>
46 #include <sys/stat.h>
47
48 +#include <features.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <getopt.h>
52 @@ -46,44 +47,68 @@
53 #include "einfo.h"
54 #include "rc-misc.h"
55
56 +typedef enum {
57 + inode_unknown = 0,
58 + inode_file = 1,
59 + inode_dir = 2,
60 + inode_fifo = 3,
61 +} inode_t;
62 +
63 extern const char *applet;
64
65 static int
66 -do_check(char *path, uid_t uid, gid_t gid, mode_t mode, int file)
67 +do_check(char *path, uid_t uid, gid_t gid, mode_t mode, inode_t type, bool trunc)
68 {
69 struct stat st;
70 - int fd;
71 + int fd, flags;
72
73 if (stat(path, &st)) {
74 - if (file) {
75 + if (type == inode_file) {
76 einfo("%s: creating file", path);
77 - if (!mode)
78 - mode = S_IRUSR | S_IWUSR | S_IRGRP |
79 - S_IWGRP | S_IROTH;
80 - if ((fd = open(path, O_CREAT, mode)) == -1) {
81 + if (!mode) /* 664 */
82 + mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
83 + flags = O_CREAT|O_NDELAY|O_WRONLY|O_NOCTTY;
84 +#ifdef __USE_XOPEN2K8
85 + flags |= O_CLOEXEC|O_NOFOLLOW;
86 +#endif
87 + if (trunc)
88 + flags |= O_TRUNC;
89 + if ((fd = open(path, flags, mode)) == -1) {
90 eerror("%s: open: %s", applet, strerror(errno));
91 return -1;
92 }
93 close (fd);
94 - } else {
95 + } else if (type == inode_dir) {
96 einfo("%s: creating directory", path);
97 - if (!mode)
98 + if (!mode) /* 775 */
99 mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
100 - if (mkdir(path, mode)) {
101 + if (mkdir(path, mode) == -1) {
102 eerror("%s: mkdir: %s", applet,
103 strerror (errno));
104 return -1;
105 }
106 mode = 0;
107 + } else if (type == inode_fifo) {
108 + einfo("%s: creating fifo", path);
109 + if (!mode) /* 600 */
110 + mode = S_IRUSR | S_IWUSR;
111 + if (mkfifo(path, mode) == -1) {
112 + eerror("%s: mkfifo: %s", applet,
113 + strerror (errno));
114 + return -1;
115 + }
116 }
117 } else {
118 - if ((file && S_ISDIR(st.st_mode)) ||
119 - (!file && !S_ISDIR(st.st_mode)))
120 - {
121 - if (file)
122 - eerror("%s: is a directory", path);
123 - else
124 - eerror("%s: is a file", path);
125 + if (type != inode_dir && S_ISDIR(st.st_mode)) {
126 + eerror("%s: is a directory", path);
127 + return 1;
128 + }
129 + if (type != inode_file && S_ISREG(st.st_mode)) {
130 + eerror("%s: is a file", path);
131 + return 1;
132 + }
133 + if (type != inode_fifo && S_ISFIFO(st.st_mode)) {
134 + eerror("%s: is a fifo", path);
135 return -1;
136 }
137 }
138 @@ -143,17 +168,23 @@ parse_owner(struct passwd **user, struct group **group, const char *owner)
139
140 #include "_usage.h"
141 #define extraopts "path1 path2 ..."
142 -#define getoptstring "dfm:o:" getoptstring_COMMON
143 +#define getoptstring "dDfFpm:o:" getoptstring_COMMON
144 static const struct option longopts[] = {
145 - { "directory", 0, NULL, 'd'},
146 - { "file", 0, NULL, 'f'},
147 - { "mode", 1, NULL, 'm'},
148 - { "owner", 1, NULL, 'o'},
149 + { "directory", 0, NULL, 'd'},
150 + { "directory-truncate", 0, NULL, 'D'},
151 + { "file", 0, NULL, 'f'},
152 + { "file-truncate", 0, NULL, 'F'},
153 + { "pipe", 0, NULL, 'p'},
154 + { "mode", 1, NULL, 'm'},
155 + { "owner", 1, NULL, 'o'},
156 longopts_COMMON
157 };
158 static const char * const longopts_help[] = {
159 - "Check if a directory",
160 - "Check if a file",
161 + "Create a directory if not exists",
162 + "Create/empty directory",
163 + "Create a file if not exists",
164 + "Truncate file",
165 + "Create a named pipe (FIFO) if not exists",
166 "Mode to check",
167 "Owner to check (user:group)",
168 longopts_help_COMMON
169 @@ -169,18 +200,26 @@ checkpath(int argc, char **argv)
170 mode_t mode = 0;
171 struct passwd *pw = NULL;
172 struct group *gr = NULL;
173 - bool file = 0;
174 + inode_t type = inode_unknown;
175 int retval = EXIT_SUCCESS;
176 + bool trunc = 0;
177
178 while ((opt = getopt_long(argc, argv, getoptstring,
179 longopts, (int *) 0)) != -1)
180 {
181 switch (opt) {
182 + case 'D':
183 + trunc = 1;
184 case 'd':
185 - file = 0;
186 + type = inode_dir;
187 break;
188 + case 'F':
189 + trunc = 1;
190 case 'f':
191 - file = 1;
192 + type = inode_file;
193 + break;
194 + case 'p':
195 + type = inode_fifo;
196 break;
197 case 'm':
198 if (parse_mode(&mode, optarg) != 0)
199 @@ -208,7 +247,7 @@ checkpath(int argc, char **argv)
200 gid = gr->gr_gid;
201
202 while (optind < argc) {
203 - if (do_check(argv[optind], uid, gid, mode, file))
204 + if (do_check(argv[optind], uid, gid, mode, type, trunc))
205 retval = EXIT_FAILURE;
206 optind++;
207 }