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 bool first_word(const char *s, const char *word) {
103 if (memcmp(s, word, wl) != 0)
106 return (s[wl] == 0 ||
107 strchr(WHITESPACE, s[wl]));
110 int close_nointr(int fd) {
116 if ((r = close(fd)) >= 0)
124 void close_nointr_nofail(int fd) {
126 /* like close_nointr() but cannot fail, and guarantees errno
129 assert_se(close_nointr(fd) == 0);
132 int parse_boolean(const char *v) {
135 if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
137 else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
143 int safe_atou(const char *s, unsigned *ret_u) {
151 l = strtoul(s, &x, 0);
153 if (!x || *x || errno)
154 return errno ? -errno : -EINVAL;
156 if ((unsigned long) (unsigned) l != l)
159 *ret_u = (unsigned) l;
163 int safe_atoi(const char *s, int *ret_i) {
171 l = strtol(s, &x, 0);
173 if (!x || *x || errno)
174 return errno ? -errno : -EINVAL;
176 if ((long) (int) l != l)
183 int safe_atolu(const char *s, long unsigned *ret_lu) {
191 l = strtoul(s, &x, 0);
193 if (!x || *x || errno)
194 return errno ? -errno : -EINVAL;
200 int safe_atoli(const char *s, long int *ret_li) {
208 l = strtol(s, &x, 0);
210 if (!x || *x || errno)
211 return errno ? -errno : -EINVAL;
217 int safe_atollu(const char *s, long long unsigned *ret_llu) {
219 unsigned long long l;
225 l = strtoull(s, &x, 0);
227 if (!x || *x || errno)
228 return errno ? -errno : -EINVAL;
234 int safe_atolli(const char *s, long long int *ret_lli) {
242 l = strtoll(s, &x, 0);
244 if (!x || *x || errno)
245 return errno ? -errno : -EINVAL;
251 /* Split a string into words. */
252 char *split_spaces(const char *c, size_t *l, char **state) {
255 current = *state ? *state : (char*) c;
257 if (!*current || *c == 0)
260 current += strspn(current, WHITESPACE);
261 *l = strcspn(current, WHITESPACE);
264 return (char*) current;
267 /* Split a string into words, but consider strings enclosed in '' and
268 * "" as words even if they include spaces. */
269 char *split_quoted(const char *c, size_t *l, char **state) {
272 current = *state ? *state : (char*) c;
274 if (!*current || *c == 0)
277 current += strspn(current, WHITESPACE);
279 if (*current == '\'') {
281 *l = strcspn(current, "'");
286 } else if (*current == '\"') {
288 *l = strcspn(current, "\"");
294 *l = strcspn(current, WHITESPACE);
298 /* FIXME: Cannot deal with strings that have spaces AND ticks
301 return (char*) current;
304 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
307 char fn[132], line[256], *p;
308 long long unsigned ppid;
313 assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%llu/stat", (unsigned long long) pid) < (int) (sizeof(fn)-1));
314 fn[sizeof(fn)-1] = 0;
316 if (!(f = fopen(fn, "r")))
319 if (!(fgets(line, sizeof(line), f))) {
327 /* Let's skip the pid and comm fields. The latter is enclosed
328 * in () but does not escape any () in its value, so let's
329 * skip over it manually */
331 if (!(p = strrchr(line, ')')))
342 if ((long long unsigned) (pid_t) ppid != ppid)
345 *_ppid = (pid_t) ppid;
350 int write_one_line_file(const char *fn, const char *line) {
357 if (!(f = fopen(fn, "we")))
360 if (fputs(line, f) < 0) {
371 int read_one_line_file(const char *fn, char **line) {
379 if (!(f = fopen(fn, "re")))
382 if (!(fgets(t, sizeof(t), f))) {
387 if (!(c = strdup(t))) {
400 char *strappend(const char *s, const char *suffix) {
410 if (!(r = new(char, a+b+1)))
414 memcpy(r+a, suffix, b);
420 int readlink_malloc(const char *p, char **r) {
430 if (!(c = new(char, l)))
433 if ((n = readlink(p, c, l-1)) < 0) {
439 if ((size_t) n < l-1) {
450 char *file_name_from_path(const char *p) {
455 if ((r = strrchr(p, '/')))
461 bool path_is_absolute(const char *p) {
467 bool is_path(const char *p) {
469 return !!strchr(p, '/');
472 char *path_make_absolute(const char *p, const char *prefix) {
477 if (path_is_absolute(p) || !prefix)
480 if (asprintf(&r, "%s/%s", prefix, p) < 0)
486 int reset_all_signal_handlers(void) {
489 for (sig = 1; sig < _NSIG; sig++) {
492 if (sig == SIGKILL || sig == SIGSTOP)
496 sa.sa_handler = SIG_DFL;
497 sa.sa_flags = SA_RESTART;
499 /* On Linux the first two RT signals are reserved by
500 * glibc, and sigaction() will return EINVAL for them. */
501 if ((sigaction(sig, &sa, NULL) < 0))
509 char *strstrip(char *s) {
512 /* Drops trailing whitespace. Modifies the string in
513 * place. Returns pointer to first non-space character */
515 s += strspn(s, WHITESPACE);
518 if (!strchr(WHITESPACE, *e))
530 char *file_in_same_dir(const char *path, const char *filename) {
537 /* This removes the last component of path and appends
538 * filename, unless the latter is absolute anyway or the
541 if (path_is_absolute(filename))
542 return strdup(filename);
544 if (!(e = strrchr(path, '/')))
545 return strdup(filename);
547 k = strlen(filename);
548 if (!(r = new(char, e-path+1+k+1)))
551 memcpy(r, path, e-path+1);
552 memcpy(r+(e-path)+1, filename, k+1);
557 char hexchar(int x) {
558 static const char table[16] = "0123456789abcdef";
560 return table[x & 15];
563 int unhexchar(char c) {
565 if (c >= '0' && c <= '9')
568 if (c >= 'a' && c <= 'f')
571 if (c >= 'A' && c <= 'F')
577 char octchar(int x) {
578 return '0' + (x & 7);
581 int unoctchar(char c) {
583 if (c >= '0' && c <= '7')
589 char *cescape(const char *s) {
595 /* Does C style string escaping. */
597 if (!(r = new(char, strlen(s)*4 + 1)))
600 for (f = s, t = r; *f; f++)
646 /* For special chars we prefer octal over
647 * hexadecimal encoding, simply because glib's
648 * g_strescape() does the same */
649 if ((*f < ' ') || (*f >= 127)) {
651 *(t++) = octchar((unsigned char) *f >> 6);
652 *(t++) = octchar((unsigned char) *f >> 3);
653 *(t++) = octchar((unsigned char) *f);
664 char *cunescape(const char *s) {
670 /* Undoes C style string escaping */
672 if (!(r = new(char, strlen(s)+1)))
675 for (f = s, t = r; *f; f++) {
718 /* hexadecimal encoding */
721 if ((a = unhexchar(f[1])) < 0 ||
722 (b = unhexchar(f[2])) < 0) {
723 /* Invalid escape code, let's take it literal then */
727 *(t++) = (char) ((a << 4) | b);
745 if ((a = unoctchar(f[0])) < 0 ||
746 (b = unoctchar(f[1])) < 0 ||
747 (c = unoctchar(f[2])) < 0) {
748 /* Invalid escape code, let's take it literal then */
752 *(t++) = (char) ((a << 6) | (b << 3) | c);
760 /* premature end of string.*/
765 /* Invalid escape code, let's take it literal then */
778 char *xescape(const char *s, const char *bad) {
782 /* Escapes all chars in bad, in addition to \ and all special
783 * chars, in \xFF style escaping. May be reversed with
786 if (!(r = new(char, strlen(s)*4+1)))
789 for (f = s, t = r; *f; f++) {
791 if (*f < ' ' || *f >= 127 ||
792 *f == '\\' || strchr(bad, *f)) {
795 *(t++) = hexchar(*f >> 4);
796 *(t++) = hexchar(*f);
806 char *bus_path_escape(const char *s) {
812 /* Escapes all chars that D-Bus' object path cannot deal
813 * with. Can be reverse with bus_path_unescape() */
815 if (!(r = new(char, strlen(s)*3+1)))
818 for (f = s, t = r; *f; f++) {
820 if (!(*f >= 'A' && *f <= 'Z') &&
821 !(*f >= 'a' && *f <= 'z') &&
822 !(*f >= '0' && *f <= '9')) {
824 *(t++) = hexchar(*f >> 4);
825 *(t++) = hexchar(*f);
835 char *bus_path_unescape(const char *s) {
841 if (!(r = new(char, strlen(s)+1)))
844 for (f = s, t = r; *f; f++) {
849 if ((a = unhexchar(f[1])) < 0 ||
850 (b = unhexchar(f[2])) < 0) {
851 /* Invalid escape code, let's take it literal then */
854 *(t++) = (char) ((a << 4) | b);
866 char *path_kill_slashes(char *path) {
870 /* Removes redundant inner and trailing slashes. Modifies the
871 * passed string in-place.
873 * ///foo///bar/ becomes /foo/bar
876 for (f = path, t = path; *f; f++) {
891 /* Special rule, if we are talking of the root directory, a
892 trailing slash is good */
894 if (t == path && slash)
901 bool path_startswith(const char *path, const char *prefix) {
905 if ((path[0] == '/') != (prefix[0] == '/'))
911 path += strspn(path, "/");
912 prefix += strspn(prefix, "/");
920 a = strcspn(path, "/");
921 b = strcspn(prefix, "/");
926 if (memcmp(path, prefix, a) != 0)
934 char *ascii_strlower(char *path) {
939 for (p = path; *p; p++)
940 if (*p >= 'A' && *p <= 'Z')
946 static const char *const ioprio_class_table[] = {
947 [IOPRIO_CLASS_NONE] = "none",
948 [IOPRIO_CLASS_RT] = "realtime",
949 [IOPRIO_CLASS_BE] = "best-effort",
950 [IOPRIO_CLASS_IDLE] = "idle"
953 DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
955 static const char *const sigchld_code_table[] = {
956 [CLD_EXITED] = "exited",
957 [CLD_KILLED] = "killed",
958 [CLD_DUMPED] = "dumped",
959 [CLD_TRAPPED] = "trapped",
960 [CLD_STOPPED] = "stopped",
961 [CLD_CONTINUED] = "continued",
964 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
966 static const char *const log_facility_table[LOG_NFACILITIES] = {
967 [LOG_FAC(LOG_KERN)] = "kern",
968 [LOG_FAC(LOG_USER)] = "user",
969 [LOG_FAC(LOG_MAIL)] = "mail",
970 [LOG_FAC(LOG_DAEMON)] = "daemon",
971 [LOG_FAC(LOG_AUTH)] = "auth",
972 [LOG_FAC(LOG_SYSLOG)] = "syslog",
973 [LOG_FAC(LOG_LPR)] = "lpr",
974 [LOG_FAC(LOG_NEWS)] = "news",
975 [LOG_FAC(LOG_UUCP)] = "uucp",
976 [LOG_FAC(LOG_CRON)] = "cron",
977 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
978 [LOG_FAC(LOG_FTP)] = "ftp",
979 [LOG_FAC(LOG_LOCAL0)] = "local0",
980 [LOG_FAC(LOG_LOCAL1)] = "local1",
981 [LOG_FAC(LOG_LOCAL2)] = "local2",
982 [LOG_FAC(LOG_LOCAL3)] = "local3",
983 [LOG_FAC(LOG_LOCAL4)] = "local4",
984 [LOG_FAC(LOG_LOCAL5)] = "local5",
985 [LOG_FAC(LOG_LOCAL6)] = "local6",
986 [LOG_FAC(LOG_LOCAL7)] = "local7"
989 DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
991 static const char *const log_level_table[] = {
992 [LOG_EMERG] = "emerg",
993 [LOG_ALERT] = "alert",
996 [LOG_WARNING] = "warning",
997 [LOG_NOTICE] = "notice",
999 [LOG_DEBUG] = "debug"
1002 DEFINE_STRING_TABLE_LOOKUP(log_level, int);
1004 static const char* const sched_policy_table[] = {
1005 [SCHED_OTHER] = "other",
1006 [SCHED_BATCH] = "batch",
1007 [SCHED_IDLE] = "idle",
1008 [SCHED_FIFO] = "fifo",
1012 DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
1014 static const char* const rlimit_table[] = {
1015 [RLIMIT_CPU] = "LimitCPU",
1016 [RLIMIT_FSIZE] = "LimitFSIZE",
1017 [RLIMIT_DATA] = "LimitDATA",
1018 [RLIMIT_STACK] = "LimitSTACK",
1019 [RLIMIT_CORE] = "LimitCORE",
1020 [RLIMIT_RSS] = "LimitRSS",
1021 [RLIMIT_NOFILE] = "LimitNOFILE",
1022 [RLIMIT_AS] = "LimitAS",
1023 [RLIMIT_NPROC] = "LimitNPROC",
1024 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
1025 [RLIMIT_LOCKS] = "LimitLOCKS",
1026 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
1027 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
1028 [RLIMIT_NICE] = "LimitNICE",
1029 [RLIMIT_RTPRIO] = "LimitRTPRIO",
1030 [RLIMIT_RTTIME] = "LimitRTTIME"
1033 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);