]> git.ipfire.org Git - people/ms/systemd.git/blame - util.c
add non-failing close() variant
[people/ms/systemd.git] / util.c
CommitLineData
60918275
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3#include <assert.h>
4#include <string.h>
5#include <unistd.h>
6#include <errno.h>
85261803 7#include <stdlib.h>
034c6ed7
LP
8#include <signal.h>
9#include <stdio.h>
60918275
LP
10
11#include "macro.h"
12#include "util.h"
13
14usec_t now(clockid_t clock) {
15 struct timespec ts;
16
17 assert_se(clock_gettime(clock, &ts) == 0);
18
19 return timespec_load(&ts);
20}
21
22usec_t timespec_load(const struct timespec *ts) {
23 assert(ts);
24
25 return
26 (usec_t) ts->tv_sec * USEC_PER_SEC +
27 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
28}
29
30struct timespec *timespec_store(struct timespec *ts, usec_t u) {
31 assert(ts);
32
33 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
34 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
35
36 return ts;
37}
38
39usec_t timeval_load(const struct timeval *tv) {
40 assert(tv);
41
42 return
43 (usec_t) tv->tv_sec * USEC_PER_SEC +
44 (usec_t) tv->tv_usec;
45}
46
47struct timeval *timeval_store(struct timeval *tv, usec_t u) {
48 assert(tv);
49
50 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
51 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
52
53 return tv;
54}
55
56bool endswith(const char *s, const char *postfix) {
57 size_t sl, pl;
58
59 assert(s);
60 assert(postfix);
61
62 sl = strlen(s);
63 pl = strlen(postfix);
64
65 if (sl < pl)
66 return false;
67
68 return memcmp(s + sl - pl, postfix, pl) == 0;
69}
70
71bool startswith(const char *s, const char *prefix) {
72 size_t sl, pl;
73
74 assert(s);
75 assert(prefix);
76
77 sl = strlen(s);
78 pl = strlen(prefix);
79
80 if (sl < pl)
81 return false;
82
83 return memcmp(s, prefix, pl) == 0;
84}
85
42f4e3c4 86int close_nointr(int fd) {
60918275
LP
87 assert(fd >= 0);
88
89 for (;;) {
90 int r;
91
92 if ((r = close(fd)) >= 0)
93 return r;
94
95 if (errno != EINTR)
96 return r;
97 }
98}
85261803 99
85f136b5
LP
100void close_nointr_nofail(int fd) {
101
102 /* like close_nointr() but cannot fail, and guarantees errno
103 * is unchanged */
104
105 assert_se(close_nointr(fd) == 0);
106}
107
85261803
LP
108int parse_boolean(const char *v) {
109 assert(v);
110
44d8db9e 111 if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
85261803 112 return 1;
44d8db9e 113 else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
85261803
LP
114 return 0;
115
116 return -EINVAL;
117}
118
119int safe_atou(const char *s, unsigned *ret_u) {
120 char *x = NULL;
034c6ed7 121 unsigned long l;
85261803
LP
122
123 assert(s);
124 assert(ret_u);
125
126 errno = 0;
127 l = strtoul(s, &x, 0);
128
129 if (!x || *x || errno)
130 return errno ? -errno : -EINVAL;
131
034c6ed7 132 if ((unsigned long) (unsigned) l != l)
85261803
LP
133 return -ERANGE;
134
135 *ret_u = (unsigned) l;
136 return 0;
137}
138
139int safe_atoi(const char *s, int *ret_i) {
140 char *x = NULL;
034c6ed7 141 long l;
85261803
LP
142
143 assert(s);
144 assert(ret_i);
145
146 errno = 0;
147 l = strtol(s, &x, 0);
148
149 if (!x || *x || errno)
150 return errno ? -errno : -EINVAL;
151
034c6ed7 152 if ((long) (int) l != l)
85261803
LP
153 return -ERANGE;
154
034c6ed7
LP
155 *ret_i = (int) l;
156 return 0;
157}
158
159int safe_atolu(const char *s, long unsigned *ret_lu) {
160 char *x = NULL;
161 unsigned long l;
162
163 assert(s);
164 assert(ret_lu);
165
166 errno = 0;
167 l = strtoul(s, &x, 0);
168
169 if (!x || *x || errno)
170 return errno ? -errno : -EINVAL;
171
172 *ret_lu = l;
173 return 0;
174}
175
176int safe_atoli(const char *s, long int *ret_li) {
177 char *x = NULL;
178 long l;
179
180 assert(s);
181 assert(ret_li);
182
183 errno = 0;
184 l = strtol(s, &x, 0);
185
186 if (!x || *x || errno)
187 return errno ? -errno : -EINVAL;
188
189 *ret_li = l;
190 return 0;
191}
192
193int safe_atollu(const char *s, long long unsigned *ret_llu) {
194 char *x = NULL;
195 unsigned long long l;
196
197 assert(s);
198 assert(ret_llu);
199
200 errno = 0;
201 l = strtoull(s, &x, 0);
202
203 if (!x || *x || errno)
204 return errno ? -errno : -EINVAL;
205
206 *ret_llu = l;
207 return 0;
208}
209
210int safe_atolli(const char *s, long long int *ret_lli) {
211 char *x = NULL;
212 long long l;
213
214 assert(s);
215 assert(ret_lli);
216
217 errno = 0;
218 l = strtoll(s, &x, 0);
219
220 if (!x || *x || errno)
221 return errno ? -errno : -EINVAL;
222
223 *ret_lli = l;
85261803
LP
224 return 0;
225}
a41e8209 226
a41e8209
LP
227/* Split a string into words. */
228char *split_spaces(const char *c, size_t *l, char **state) {
229 char *current;
230
231 current = *state ? *state : (char*) c;
232
233 if (!*current || *c == 0)
234 return NULL;
235
236 current += strspn(current, WHITESPACE);
237 *l = strcspn(current, WHITESPACE);
238 *state = current+*l;
239
240 return (char*) current;
241}
034c6ed7
LP
242
243/* Split a string into words, but consider strings enclosed in '' and
244 * "" as words even if they include spaces. */
245char *split_quoted(const char *c, size_t *l, char **state) {
246 char *current;
247
248 current = *state ? *state : (char*) c;
249
250 if (!*current || *c == 0)
251 return NULL;
252
253 current += strspn(current, WHITESPACE);
254
255 if (*current == '\'') {
256 current ++;
257 *l = strcspn(current, "'");
258 *state = current+*l;
259
260 if (**state == '\'')
261 (*state)++;
262 } else if (*current == '\"') {
263 current ++;
b858b600 264 *l = strcspn(current, "\"");
034c6ed7
LP
265 *state = current+*l;
266
267 if (**state == '\"')
268 (*state)++;
269 } else {
270 *l = strcspn(current, WHITESPACE);
271 *state = current+*l;
272 }
273
44d8db9e
LP
274 /* FIXME: Cannot deal with strings that have spaces AND ticks
275 * in them */
276
034c6ed7
LP
277 return (char*) current;
278}
279
280const char *sigchld_code(int code) {
281
282 if (code == CLD_EXITED)
283 return "exited";
284 else if (code == CLD_KILLED)
285 return "killed";
286 else if (code == CLD_DUMPED)
287 return "dumped";
288 else if (code == CLD_TRAPPED)
289 return "trapped";
290 else if (code == CLD_STOPPED)
291 return "stopped";
292 else if (code == CLD_CONTINUED)
293 return "continued";
294
295 return "unknown";
296}
297
298int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
299 int r;
300 FILE *f;
301 char fn[132], line[256], *p;
302 long long unsigned ppid;
303
304 assert(pid >= 0);
305 assert(_ppid);
306
307 assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%llu/stat", (unsigned long long) pid) < (int) (sizeof(fn)-1));
308 fn[sizeof(fn)-1] = 0;
309
310 if (!(f = fopen(fn, "r")))
311 return -errno;
312
313 if (!(fgets(line, sizeof(line), f))) {
314 r = -errno;
315 fclose(f);
316 return r;
317 }
318
319 fclose(f);
320
321 /* Let's skip the pid and comm fields. The latter is enclosed
322 * in () but does not escape any () in its value, so let's
323 * skip over it manually */
324
325 if (!(p = strrchr(line, ')')))
326 return -EIO;
327
328 p++;
329
330 if (sscanf(p, " "
331 "%*c " /* state */
332 "%llu ", /* ppid */
333 &ppid) != 1)
334 return -EIO;
335
336 if ((long long unsigned) (pid_t) ppid != ppid)
337 return -ERANGE;
338
339 *_ppid = (pid_t) ppid;
340
341 return 0;
342}
343
344int write_one_line_file(const char *fn, const char *line) {
345 FILE *f;
346 int r;
347
348 assert(fn);
349 assert(line);
350
351 if (!(f = fopen(fn, "we")))
352 return -errno;
353
354 if (fputs(line, f) < 0) {
355 r = -errno;
356 goto finish;
357 }
358
359 r = 0;
360finish:
361 fclose(f);
362 return r;
363}
364
365int read_one_line_file(const char *fn, char **line) {
366 FILE *f;
367 int r;
368 char t[64], *c;
369
370 assert(fn);
371 assert(line);
372
373 if (!(f = fopen(fn, "re")))
374 return -errno;
375
376 if (!(fgets(t, sizeof(t), f))) {
377 r = -errno;
378 goto finish;
379 }
380
381 if (!(c = strdup(t))) {
382 r = -ENOMEM;
383 goto finish;
384 }
385
386 *line = c;
387 r = 0;
388
389finish:
390 fclose(f);
391 return r;
392}
44d8db9e
LP
393
394char *strappend(const char *s, const char *suffix) {
395 size_t a, b;
396 char *r;
397
398 assert(s);
399 assert(suffix);
400
401 a = strlen(s);
402 b = strlen(suffix);
403
404 if (!(r = new(char, a+b+1)))
405 return NULL;
406
407 memcpy(r, s, a);
408 memcpy(r+a, suffix, b);
409 r[a+b] = 0;
410
411 return r;
412}
87f0e418
LP
413
414int readlink_malloc(const char *p, char **r) {
415 size_t l = 100;
416
417 assert(p);
418 assert(r);
419
420 for (;;) {
421 char *c;
422 ssize_t n;
423
424 if (!(c = new(char, l)))
425 return -ENOMEM;
426
427 if ((n = readlink(p, c, l-1)) < 0) {
428 int ret = -errno;
429 free(c);
430 return ret;
431 }
432
433 if ((size_t) n < l-1) {
434 c[n] = 0;
435 *r = c;
436 return 0;
437 }
438
439 free(c);
440 l *= 2;
441 }
442}
443
444char *file_name_from_path(const char *p) {
445 char *r;
446
447 assert(p);
448
449 if ((r = strrchr(p, '/')))
450 return r + 1;
451
452 return (char*) p;
453}
0301abf4
LP
454
455bool path_is_absolute(const char *p) {
456 assert(p);
457
458 return p[0] == '/';
459}
460
461bool is_path(const char *p) {
462
463 return !!strchr(p, '/');
464}
465
466char *path_make_absolute(const char *p, const char *prefix) {
467 char *r;
468
469 assert(p);
470
471 if (path_is_absolute(p) || !prefix)
472 return strdup(p);
473
474 if (asprintf(&r, "%s/%s", prefix, p) < 0)
475 return NULL;
476
477 return r;
478}
2a987ee8
LP
479
480int reset_all_signal_handlers(void) {
481 int sig;
482
483 for (sig = 1; sig < _NSIG; sig++) {
484 struct sigaction sa;
485
486 if (sig == SIGKILL || sig == SIGSTOP)
487 continue;
488
489 zero(sa);
490 sa.sa_handler = SIG_DFL;
491
492 /* On Linux the first two RT signals are reserved by
493 * glibc, and sigaction() will return EINVAL for them. */
494 if ((sigaction(sig, &sa, NULL) < 0))
495 if (errno != EINVAL)
496 return -errno;
497 }
498
499 return 0;
500}
4a72ff34
LP
501
502char *strstrip(char *s) {
503 char *e, *l = NULL;
504
505 /* Drops trailing whitespace. Modifies the string in
506 * place. Returns pointer to first non-space character */
507
508 s += strspn(s, WHITESPACE);
509
510 for (e = s; *e; e++)
511 if (!strchr(WHITESPACE, *e))
512 l = e;
513
514 if (l)
515 *(l+1) = 0;
516 else
517 *s = 0;
518
519 return s;
520
521}
522
523char *file_in_same_dir(const char *path, const char *filename) {
524 char *e, *r;
525 size_t k;
526
527 assert(path);
528 assert(filename);
529
530 /* This removes the last component of path and appends
531 * filename, unless the latter is absolute anyway or the
532 * former isn't */
533
534 if (path_is_absolute(filename))
535 return strdup(filename);
536
537 if (!(e = strrchr(path, '/')))
538 return strdup(filename);
539
540 k = strlen(filename);
541 if (!(r = new(char, e-path+1+k+1)))
542 return NULL;
543
544 memcpy(r, path, e-path+1);
545 memcpy(r+(e-path)+1, filename, k+1);
546
547 return r;
548}