1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
14 usec_t now(clockid_t clock) {
17 assert_se(clock_gettime(clock, &ts) == 0);
19 return timespec_load(&ts);
22 usec_t timespec_load(const struct timespec *ts) {
26 (usec_t) ts->tv_sec * USEC_PER_SEC +
27 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
30 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
33 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
34 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
39 usec_t timeval_load(const struct timeval *tv) {
43 (usec_t) tv->tv_sec * USEC_PER_SEC +
47 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
50 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
51 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
56 bool endswith(const char *s, const char *postfix) {
68 return memcmp(s + sl - pl, postfix, pl) == 0;
71 bool startswith(const char *s, const char *prefix) {
83 return memcmp(s, prefix, pl) == 0;
86 int close_nointr(int fd) {
92 if ((r = close(fd)) >= 0)
100 int parse_boolean(const char *v) {
103 if (!strcmp(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
105 else if (!strcmp(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
111 int safe_atou(const char *s, unsigned *ret_u) {
119 l = strtoul(s, &x, 0);
121 if (!x || *x || errno)
122 return errno ? -errno : -EINVAL;
124 if ((unsigned long) (unsigned) l != l)
127 *ret_u = (unsigned) l;
131 int safe_atoi(const char *s, int *ret_i) {
139 l = strtol(s, &x, 0);
141 if (!x || *x || errno)
142 return errno ? -errno : -EINVAL;
144 if ((long) (int) l != l)
151 int safe_atolu(const char *s, long unsigned *ret_lu) {
159 l = strtoul(s, &x, 0);
161 if (!x || *x || errno)
162 return errno ? -errno : -EINVAL;
168 int safe_atoli(const char *s, long int *ret_li) {
176 l = strtol(s, &x, 0);
178 if (!x || *x || errno)
179 return errno ? -errno : -EINVAL;
185 int safe_atollu(const char *s, long long unsigned *ret_llu) {
187 unsigned long long l;
193 l = strtoull(s, &x, 0);
195 if (!x || *x || errno)
196 return errno ? -errno : -EINVAL;
202 int safe_atolli(const char *s, long long int *ret_lli) {
210 l = strtoll(s, &x, 0);
212 if (!x || *x || errno)
213 return errno ? -errno : -EINVAL;
219 /* What is interpreted as whitespace? */
220 #define WHITESPACE " \t\n"
222 /* Split a string into words. */
223 char *split_spaces(const char *c, size_t *l, char **state) {
226 current = *state ? *state : (char*) c;
228 if (!*current || *c == 0)
231 current += strspn(current, WHITESPACE);
232 *l = strcspn(current, WHITESPACE);
235 return (char*) current;
238 /* Split a string into words, but consider strings enclosed in '' and
239 * "" as words even if they include spaces. */
240 char *split_quoted(const char *c, size_t *l, char **state) {
243 current = *state ? *state : (char*) c;
245 if (!*current || *c == 0)
248 current += strspn(current, WHITESPACE);
250 if (*current == '\'') {
252 *l = strcspn(current, "'");
257 } else if (*current == '\"') {
259 *l = strcspn(current+1, "\"");
265 *l = strcspn(current, WHITESPACE);
269 return (char*) current;
272 const char *sigchld_code(int code) {
274 if (code == CLD_EXITED)
276 else if (code == CLD_KILLED)
278 else if (code == CLD_DUMPED)
280 else if (code == CLD_TRAPPED)
282 else if (code == CLD_STOPPED)
284 else if (code == CLD_CONTINUED)
290 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
293 char fn[132], line[256], *p;
294 long long unsigned ppid;
299 assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%llu/stat", (unsigned long long) pid) < (int) (sizeof(fn)-1));
300 fn[sizeof(fn)-1] = 0;
302 if (!(f = fopen(fn, "r")))
305 if (!(fgets(line, sizeof(line), f))) {
313 /* Let's skip the pid and comm fields. The latter is enclosed
314 * in () but does not escape any () in its value, so let's
315 * skip over it manually */
317 if (!(p = strrchr(line, ')')))
328 if ((long long unsigned) (pid_t) ppid != ppid)
331 *_ppid = (pid_t) ppid;
336 int write_one_line_file(const char *fn, const char *line) {
343 if (!(f = fopen(fn, "we")))
346 if (fputs(line, f) < 0) {
357 int read_one_line_file(const char *fn, char **line) {
365 if (!(f = fopen(fn, "re")))
368 if (!(fgets(t, sizeof(t), f))) {
373 if (!(c = strdup(t))) {