1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
12 #include <sys/resource.h>
19 usec_t now(clockid_t clock) {
22 assert_se(clock_gettime(clock, &ts) == 0);
24 return timespec_load(&ts);
27 usec_t timespec_load(const struct timespec *ts) {
31 (usec_t) ts->tv_sec * USEC_PER_SEC +
32 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
35 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
38 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
39 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
44 usec_t timeval_load(const struct timeval *tv) {
48 (usec_t) tv->tv_sec * USEC_PER_SEC +
52 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
55 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
56 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
61 bool endswith(const char *s, const char *postfix) {
73 return memcmp(s + sl - pl, postfix, pl) == 0;
76 bool startswith(const char *s, const char *prefix) {
88 return memcmp(s, prefix, pl) == 0;
91 int close_nointr(int fd) {
97 if ((r = close(fd)) >= 0)
105 void close_nointr_nofail(int fd) {
107 /* like close_nointr() but cannot fail, and guarantees errno
110 assert_se(close_nointr(fd) == 0);
113 int parse_boolean(const char *v) {
116 if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
118 else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
124 int safe_atou(const char *s, unsigned *ret_u) {
132 l = strtoul(s, &x, 0);
134 if (!x || *x || errno)
135 return errno ? -errno : -EINVAL;
137 if ((unsigned long) (unsigned) l != l)
140 *ret_u = (unsigned) l;
144 int safe_atoi(const char *s, int *ret_i) {
152 l = strtol(s, &x, 0);
154 if (!x || *x || errno)
155 return errno ? -errno : -EINVAL;
157 if ((long) (int) l != l)
164 int safe_atolu(const char *s, long unsigned *ret_lu) {
172 l = strtoul(s, &x, 0);
174 if (!x || *x || errno)
175 return errno ? -errno : -EINVAL;
181 int safe_atoli(const char *s, long int *ret_li) {
189 l = strtol(s, &x, 0);
191 if (!x || *x || errno)
192 return errno ? -errno : -EINVAL;
198 int safe_atollu(const char *s, long long unsigned *ret_llu) {
200 unsigned long long l;
206 l = strtoull(s, &x, 0);
208 if (!x || *x || errno)
209 return errno ? -errno : -EINVAL;
215 int safe_atolli(const char *s, long long int *ret_lli) {
223 l = strtoll(s, &x, 0);
225 if (!x || *x || errno)
226 return errno ? -errno : -EINVAL;
232 /* Split a string into words. */
233 char *split_spaces(const char *c, size_t *l, char **state) {
236 current = *state ? *state : (char*) c;
238 if (!*current || *c == 0)
241 current += strspn(current, WHITESPACE);
242 *l = strcspn(current, WHITESPACE);
245 return (char*) current;
248 /* Split a string into words, but consider strings enclosed in '' and
249 * "" as words even if they include spaces. */
250 char *split_quoted(const char *c, size_t *l, char **state) {
253 current = *state ? *state : (char*) c;
255 if (!*current || *c == 0)
258 current += strspn(current, WHITESPACE);
260 if (*current == '\'') {
262 *l = strcspn(current, "'");
267 } else if (*current == '\"') {
269 *l = strcspn(current, "\"");
275 *l = strcspn(current, WHITESPACE);
279 /* FIXME: Cannot deal with strings that have spaces AND ticks
282 return (char*) current;
285 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
288 char fn[132], line[256], *p;
289 long long unsigned ppid;
294 assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%llu/stat", (unsigned long long) pid) < (int) (sizeof(fn)-1));
295 fn[sizeof(fn)-1] = 0;
297 if (!(f = fopen(fn, "r")))
300 if (!(fgets(line, sizeof(line), f))) {
308 /* Let's skip the pid and comm fields. The latter is enclosed
309 * in () but does not escape any () in its value, so let's
310 * skip over it manually */
312 if (!(p = strrchr(line, ')')))
323 if ((long long unsigned) (pid_t) ppid != ppid)
326 *_ppid = (pid_t) ppid;
331 int write_one_line_file(const char *fn, const char *line) {
338 if (!(f = fopen(fn, "we")))
341 if (fputs(line, f) < 0) {
352 int read_one_line_file(const char *fn, char **line) {
360 if (!(f = fopen(fn, "re")))
363 if (!(fgets(t, sizeof(t), f))) {
368 if (!(c = strdup(t))) {
381 char *strappend(const char *s, const char *suffix) {
391 if (!(r = new(char, a+b+1)))
395 memcpy(r+a, suffix, b);
401 int readlink_malloc(const char *p, char **r) {
411 if (!(c = new(char, l)))
414 if ((n = readlink(p, c, l-1)) < 0) {
420 if ((size_t) n < l-1) {
431 char *file_name_from_path(const char *p) {
436 if ((r = strrchr(p, '/')))
442 bool path_is_absolute(const char *p) {
448 bool is_path(const char *p) {
450 return !!strchr(p, '/');
453 char *path_make_absolute(const char *p, const char *prefix) {
458 if (path_is_absolute(p) || !prefix)
461 if (asprintf(&r, "%s/%s", prefix, p) < 0)
467 int reset_all_signal_handlers(void) {
470 for (sig = 1; sig < _NSIG; sig++) {
473 if (sig == SIGKILL || sig == SIGSTOP)
477 sa.sa_handler = SIG_DFL;
478 sa.sa_flags = SA_RESTART;
480 /* On Linux the first two RT signals are reserved by
481 * glibc, and sigaction() will return EINVAL for them. */
482 if ((sigaction(sig, &sa, NULL) < 0))
490 char *strstrip(char *s) {
493 /* Drops trailing whitespace. Modifies the string in
494 * place. Returns pointer to first non-space character */
496 s += strspn(s, WHITESPACE);
499 if (!strchr(WHITESPACE, *e))
511 char *file_in_same_dir(const char *path, const char *filename) {
518 /* This removes the last component of path and appends
519 * filename, unless the latter is absolute anyway or the
522 if (path_is_absolute(filename))
523 return strdup(filename);
525 if (!(e = strrchr(path, '/')))
526 return strdup(filename);
528 k = strlen(filename);
529 if (!(r = new(char, e-path+1+k+1)))
532 memcpy(r, path, e-path+1);
533 memcpy(r+(e-path)+1, filename, k+1);
538 char hexchar(int x) {
539 static const char table[16] = "0123456789abcdef";
541 return table[x & 15];
544 int unhexchar(char c) {
546 if (c >= '0' && c <= '9')
549 if (c >= 'a' && c <= 'f')
552 if (c >= 'A' && c <= 'F')
558 char octchar(int x) {
559 return '0' + (x & 7);
562 int unoctchar(char c) {
564 if (c >= '0' && c <= '7')
570 char *cescape(const char *s) {
576 /* Does C style string escaping. */
578 if (!(r = new(char, strlen(s)*4 + 1)))
581 for (f = s, t = r; *f; f++)
627 /* For special chars we prefer octal over
628 * hexadecimal encoding, simply because glib's
629 * g_strescape() does the same */
630 if ((*f < ' ') || (*f >= 127)) {
632 *(t++) = octchar((unsigned char) *f >> 6);
633 *(t++) = octchar((unsigned char) *f >> 3);
634 *(t++) = octchar((unsigned char) *f);
645 char *cunescape(const char *s) {
651 /* Undoes C style string escaping */
653 if (!(r = new(char, strlen(s)+1)))
656 for (f = s, t = r; *f; f++) {
699 /* hexadecimal encoding */
702 if ((a = unhexchar(f[1])) < 0 ||
703 (b = unhexchar(f[2])) < 0) {
704 /* Invalid escape code, let's take it literal then */
708 *(t++) = (char) ((a << 4) | b);
726 if ((a = unoctchar(f[0])) < 0 ||
727 (b = unoctchar(f[1])) < 0 ||
728 (c = unoctchar(f[2])) < 0) {
729 /* Invalid escape code, let's take it literal then */
733 *(t++) = (char) ((a << 6) | (b << 3) | c);
741 /* premature end of string.*/
746 /* Invalid escape code, let's take it literal then */
759 char *xescape(const char *s, const char *bad) {
763 /* Escapes all chars in bad, in addition to \ and all special
764 * chars, in \xFF style escaping. May be reversed with
767 if (!(r = new(char, strlen(s)*4+1)))
770 for (f = s, t = r; *f; f++) {
772 if (*f < ' ' || *f >= 127 ||
773 *f == '\\' || strchr(bad, *f)) {
776 *(t++) = hexchar(*f >> 4);
777 *(t++) = hexchar(*f);
787 char *path_kill_slashes(char *path) {
791 /* Removes redundant inner and trailing slashes. Modifies the
792 * passed string in-place.
794 * ///foo///bar/ becomes /foo/bar
797 for (f = path, t = path; *f; f++) {
812 /* Special rule, if we are talking of the root directory, a
813 trailing slash is good */
815 if (t == path && slash)
822 bool path_startswith(const char *path, const char *prefix) {
826 if ((path[0] == '/') != (prefix[0] == '/'))
832 path += strspn(path, "/");
833 prefix += strspn(prefix, "/");
841 a = strcspn(path, "/");
842 b = strcspn(prefix, "/");
847 if (memcmp(path, prefix, a) != 0)
855 char *ascii_strlower(char *path) {
860 for (p = path; *p; p++)
861 if (*p >= 'A' && *p <= 'Z')
867 static const char *const ioprio_class_table[] = {
868 [IOPRIO_CLASS_NONE] = "none",
869 [IOPRIO_CLASS_RT] = "realtime",
870 [IOPRIO_CLASS_BE] = "best-effort",
871 [IOPRIO_CLASS_IDLE] = "idle"
874 DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
876 static const char *const sigchld_code_table[] = {
877 [CLD_EXITED] = "exited",
878 [CLD_KILLED] = "killed",
879 [CLD_DUMPED] = "dumped",
880 [CLD_TRAPPED] = "trapped",
881 [CLD_STOPPED] = "stopped",
882 [CLD_CONTINUED] = "continued",
885 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
887 static const char *const log_facility_table[LOG_NFACILITIES] = {
888 [LOG_FAC(LOG_KERN)] = "kern",
889 [LOG_FAC(LOG_USER)] = "user",
890 [LOG_FAC(LOG_MAIL)] = "mail",
891 [LOG_FAC(LOG_DAEMON)] = "daemon",
892 [LOG_FAC(LOG_AUTH)] = "auth",
893 [LOG_FAC(LOG_SYSLOG)] = "syslog",
894 [LOG_FAC(LOG_LPR)] = "lpr",
895 [LOG_FAC(LOG_NEWS)] = "news",
896 [LOG_FAC(LOG_UUCP)] = "uucp",
897 [LOG_FAC(LOG_CRON)] = "cron",
898 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
899 [LOG_FAC(LOG_FTP)] = "ftp",
900 [LOG_FAC(LOG_LOCAL0)] = "local0",
901 [LOG_FAC(LOG_LOCAL1)] = "local1",
902 [LOG_FAC(LOG_LOCAL2)] = "local2",
903 [LOG_FAC(LOG_LOCAL3)] = "local3",
904 [LOG_FAC(LOG_LOCAL4)] = "local4",
905 [LOG_FAC(LOG_LOCAL5)] = "local5",
906 [LOG_FAC(LOG_LOCAL6)] = "local6",
907 [LOG_FAC(LOG_LOCAL7)] = "local7"
910 DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
912 static const char *const log_level_table[] = {
913 [LOG_EMERG] = "emerg",
914 [LOG_ALERT] = "alert",
917 [LOG_WARNING] = "warning",
918 [LOG_NOTICE] = "notice",
920 [LOG_DEBUG] = "debug"
923 DEFINE_STRING_TABLE_LOOKUP(log_level, int);
925 static const char* const sched_policy_table[] = {
926 [SCHED_OTHER] = "other",
927 [SCHED_BATCH] = "batch",
928 [SCHED_IDLE] = "idle",
929 [SCHED_FIFO] = "fifo",
933 DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
935 static const char* const rlimit_table[] = {
936 [RLIMIT_CPU] = "LimitCPU",
937 [RLIMIT_FSIZE] = "LimitFSIZE",
938 [RLIMIT_DATA] = "LimitDATA",
939 [RLIMIT_STACK] = "LimitSTACK",
940 [RLIMIT_CORE] = "LimitCORE",
941 [RLIMIT_RSS] = "LimitRSS",
942 [RLIMIT_NOFILE] = "LimitNOFILE",
943 [RLIMIT_AS] = "LimitAS",
944 [RLIMIT_NPROC] = "LimitNPROC",
945 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
946 [RLIMIT_LOCKS] = "LimitLOCKS",
947 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
948 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
949 [RLIMIT_NICE] = "LimitNICE",
950 [RLIMIT_RTPRIO] = "LimitRTPRIO",
951 [RLIMIT_RTTIME] = "LimitRTTIME"
954 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);