]> git.ipfire.org Git - thirdparty/util-linux.git/blame - lib/path.c
setproctitle: fix out of boundary access
[thirdparty/util-linux.git] / lib / path.c
CommitLineData
8148217b 1/*
b295bdb1
BS
2 * Simple functions to access files. Paths can be globally prefixed to read
3 * data from an alternative source (e.g. a /proc dump for regression tests).
8148217b
HC
4 *
5 * Copyright (C) 2008 Cai Qian <qcai@redhat.com>
ca01695b 6 * Copyright (C) 2008-2012 Karel Zak <kzak@redhat.com>
8148217b
HC
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it would be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
7cebf0bb
SK
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
8148217b
HC
21 */
22
23#include <stdarg.h>
24#include <string.h>
25#include <unistd.h>
26#include <stdio.h>
37a5c7ee 27#include <inttypes.h>
8148217b
HC
28#include <errno.h>
29
e12c9866 30#include "all-io.h"
8148217b
HC
31#include "path.h"
32#include "nls.h"
33#include "c.h"
34
35static size_t prefixlen;
36static char pathbuf[PATH_MAX];
37
38static const char *
39path_vcreate(const char *path, va_list ap)
40{
f567220b
RM
41 int rc = vsnprintf(
42 pathbuf + prefixlen, sizeof(pathbuf) - prefixlen, path, ap);
43
44 if (rc < 0)
45 return NULL;
46 if ((size_t)rc >= sizeof(pathbuf)) {
47 errno = ENAMETOOLONG;
48 return NULL;
49 }
8148217b
HC
50 return pathbuf;
51}
52
81435af3
RM
53const char *
54path_get(const char *path, ...)
dd3bc51a
KZ
55{
56 const char *p;
57 va_list ap;
58
59 va_start(ap, path);
60 p = path_vcreate(path, ap);
61 va_end(ap);
62
81435af3 63 return p;
dd3bc51a
KZ
64}
65
8148217b
HC
66static FILE *
67path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap)
68{
69 FILE *f;
70 const char *p = path_vcreate(path, ap);
f567220b
RM
71 if (!p)
72 goto err;
8148217b
HC
73
74 f = fopen(p, mode);
f567220b
RM
75 if (!f)
76 goto err;
77
8148217b 78 return f;
f567220b
RM
79err:
80 if (exit_on_error)
81 err(EXIT_FAILURE, _("cannot open %s"), p ? p : "path");
82 return NULL;
8148217b
HC
83}
84
9bc2b4b1
HC
85static int
86path_vopen(int flags, const char *path, va_list ap)
87{
88 int fd;
89 const char *p = path_vcreate(path, ap);
f567220b
RM
90 if (!p)
91 goto err;
9bc2b4b1
HC
92
93 fd = open(p, flags);
94 if (fd == -1)
f567220b
RM
95 goto err;
96
9bc2b4b1 97 return fd;
f567220b
RM
98err:
99 err(EXIT_FAILURE, _("cannot open %s"), p ? p : "path");
9bc2b4b1
HC
100}
101
8148217b
HC
102FILE *
103path_fopen(const char *mode, int exit_on_error, const char *path, ...)
104{
105 FILE *fd;
106 va_list ap;
107
108 va_start(ap, path);
109 fd = path_vfopen(mode, exit_on_error, path, ap);
110 va_end(ap);
111
112 return fd;
113}
114
115void
ca01695b 116path_read_str(char *result, size_t len, const char *path, ...)
8148217b
HC
117{
118 FILE *fd;
119 va_list ap;
120
121 va_start(ap, path);
b1fa3e22 122 fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap);
8148217b
HC
123 va_end(ap);
124
125 if (!fgets(result, len, fd))
47481cbd 126 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
8148217b
HC
127 fclose(fd);
128
129 len = strlen(result);
130 if (result[len - 1] == '\n')
131 result[len - 1] = '\0';
132}
133
134int
ca01695b 135path_read_s32(const char *path, ...)
8148217b
HC
136{
137 FILE *fd;
138 va_list ap;
139 int result;
140
141 va_start(ap, path);
b1fa3e22 142 fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap);
8148217b
HC
143 va_end(ap);
144
145 if (fscanf(fd, "%d", &result) != 1) {
146 if (ferror(fd))
47481cbd 147 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
8148217b
HC
148 else
149 errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
150 }
151 fclose(fd);
152 return result;
153}
154
37a5c7ee
KZ
155uint64_t
156path_read_u64(const char *path, ...)
157{
158 FILE *fd;
159 va_list ap;
160 uint64_t result;
161
162 va_start(ap, path);
163 fd = path_vfopen("r", 1, path, ap);
164 va_end(ap);
165
166 if (fscanf(fd, "%"SCNu64, &result) != 1) {
167 if (ferror(fd))
47481cbd 168 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
37a5c7ee
KZ
169 else
170 errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
171 }
172 fclose(fd);
173 return result;
174}
175
9bc2b4b1 176int
ca01695b 177path_write_str(const char *str, const char *path, ...)
9bc2b4b1
HC
178{
179 int fd, result;
180 va_list ap;
181
182 va_start(ap, path);
b1fa3e22 183 fd = path_vopen(O_WRONLY|O_CLOEXEC, path, ap);
9bc2b4b1 184 va_end(ap);
25dea0b5 185 result = write_all(fd, str, strlen(str));
9bc2b4b1
HC
186 close(fd);
187 return result;
188}
189
8148217b
HC
190int
191path_exist(const char *path, ...)
192{
193 va_list ap;
194 const char *p;
195
196 va_start(ap, path);
197 p = path_vcreate(path, ap);
198 va_end(ap);
199
f567220b 200 return p && access(p, F_OK) == 0;
8148217b
HC
201}
202
e21e6d26
KZ
203#ifdef HAVE_CPU_SET_T
204
8148217b
HC
205static cpu_set_t *
206path_cpuparse(int maxcpus, int islist, const char *path, va_list ap)
207{
208 FILE *fd;
209 cpu_set_t *set;
210 size_t setsize, len = maxcpus * 7;
211 char buf[len];
212
b1fa3e22 213 fd = path_vfopen("r" UL_CLOEXECSTR, 1, path, ap);
8148217b
HC
214
215 if (!fgets(buf, len, fd))
47481cbd 216 err(EXIT_FAILURE, _("cannot read %s"), pathbuf);
8148217b
HC
217 fclose(fd);
218
219 len = strlen(buf);
220 if (buf[len - 1] == '\n')
221 buf[len - 1] = '\0';
222
223 set = cpuset_alloc(maxcpus, &setsize, NULL);
224 if (!set)
225 err(EXIT_FAILURE, _("failed to callocate cpu set"));
226
227 if (islist) {
228 if (cpulist_parse(buf, set, setsize, 0))
229 errx(EXIT_FAILURE, _("failed to parse CPU list %s"), buf);
230 } else {
231 if (cpumask_parse(buf, set, setsize))
232 errx(EXIT_FAILURE, _("failed to parse CPU mask %s"), buf);
233 }
234 return set;
235}
236
237cpu_set_t *
ca01695b 238path_read_cpuset(int maxcpus, const char *path, ...)
8148217b
HC
239{
240 va_list ap;
241 cpu_set_t *set;
242
243 va_start(ap, path);
244 set = path_cpuparse(maxcpus, 0, path, ap);
245 va_end(ap);
246
247 return set;
248}
249
250cpu_set_t *
ca01695b 251path_read_cpulist(int maxcpus, const char *path, ...)
8148217b
HC
252{
253 va_list ap;
254 cpu_set_t *set;
255
256 va_start(ap, path);
257 set = path_cpuparse(maxcpus, 1, path, ap);
258 va_end(ap);
259
260 return set;
261}
262
e230ae7b 263int
ca01695b 264path_set_prefix(const char *prefix)
8148217b 265{
e230ae7b
RM
266 size_t len = strlen(prefix);
267
268 if (len >= sizeof(pathbuf) - 1) {
269 errno = ENAMETOOLONG;
270 return -1;
271 }
272 prefixlen = len;
273 strcpy(pathbuf, prefix);
274 return 0;
8148217b 275}
4a04d7f3
RM
276
277#endif /* HAVE_CPU_SET_T */