1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 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.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include <sys/resource.h>
32 #include <linux/sched.h>
39 usec_t now(clockid_t clock_id) {
42 assert_se(clock_gettime(clock_id, &ts) == 0);
44 return timespec_load(&ts);
47 usec_t timespec_load(const struct timespec *ts) {
51 (usec_t) ts->tv_sec * USEC_PER_SEC +
52 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
55 struct timespec *timespec_store(struct timespec *ts, usec_t u) {
58 ts->tv_sec = (time_t) (u / USEC_PER_SEC);
59 ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
64 usec_t timeval_load(const struct timeval *tv) {
68 (usec_t) tv->tv_sec * USEC_PER_SEC +
72 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
75 tv->tv_sec = (time_t) (u / USEC_PER_SEC);
76 tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
81 bool endswith(const char *s, const char *postfix) {
93 return memcmp(s + sl - pl, postfix, pl) == 0;
96 bool startswith(const char *s, const char *prefix) {
108 return memcmp(s, prefix, pl) == 0;
111 bool first_word(const char *s, const char *word) {
123 if (memcmp(s, word, wl) != 0)
126 return (s[wl] == 0 ||
127 strchr(WHITESPACE, s[wl]));
130 int close_nointr(int fd) {
136 if ((r = close(fd)) >= 0)
144 void close_nointr_nofail(int fd) {
146 /* like close_nointr() but cannot fail, and guarantees errno
149 assert_se(close_nointr(fd) == 0);
152 int parse_boolean(const char *v) {
155 if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
157 else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
163 int safe_atou(const char *s, unsigned *ret_u) {
171 l = strtoul(s, &x, 0);
173 if (!x || *x || errno)
174 return errno ? -errno : -EINVAL;
176 if ((unsigned long) (unsigned) l != l)
179 *ret_u = (unsigned) l;
183 int safe_atoi(const char *s, int *ret_i) {
191 l = strtol(s, &x, 0);
193 if (!x || *x || errno)
194 return errno ? -errno : -EINVAL;
196 if ((long) (int) l != l)
203 int safe_atolu(const char *s, long unsigned *ret_lu) {
211 l = strtoul(s, &x, 0);
213 if (!x || *x || errno)
214 return errno ? -errno : -EINVAL;
220 int safe_atoli(const char *s, long int *ret_li) {
228 l = strtol(s, &x, 0);
230 if (!x || *x || errno)
231 return errno ? -errno : -EINVAL;
237 int safe_atollu(const char *s, long long unsigned *ret_llu) {
239 unsigned long long l;
245 l = strtoull(s, &x, 0);
247 if (!x || *x || errno)
248 return errno ? -errno : -EINVAL;
254 int safe_atolli(const char *s, long long int *ret_lli) {
262 l = strtoll(s, &x, 0);
264 if (!x || *x || errno)
265 return errno ? -errno : -EINVAL;
271 /* Split a string into words. */
272 char *split_spaces(const char *c, size_t *l, char **state) {
275 current = *state ? *state : (char*) c;
277 if (!*current || *c == 0)
280 current += strspn(current, WHITESPACE);
281 *l = strcspn(current, WHITESPACE);
284 return (char*) current;
287 /* Split a path into filenames. */
288 char *split_slash(const char *c, size_t *l, char **state) {
291 current = *state ? *state : (char*) c;
293 if (!*current || *c == 0)
296 current += strspn(current, "/");
297 *l = strcspn(current, "/");
300 return (char*) current;
303 /* Split a string into words, but consider strings enclosed in '' and
304 * "" as words even if they include spaces. */
305 char *split_quoted(const char *c, size_t *l, char **state) {
308 current = *state ? *state : (char*) c;
310 if (!*current || *c == 0)
313 current += strspn(current, WHITESPACE);
315 if (*current == '\'') {
317 *l = strcspn(current, "'");
322 } else if (*current == '\"') {
324 *l = strcspn(current, "\"");
330 *l = strcspn(current, WHITESPACE);
334 /* FIXME: Cannot deal with strings that have spaces AND ticks
337 return (char*) current;
340 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
343 char fn[132], line[256], *p;
344 long long unsigned ppid;
349 assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%llu/stat", (unsigned long long) pid) < (int) (sizeof(fn)-1));
350 fn[sizeof(fn)-1] = 0;
352 if (!(f = fopen(fn, "r")))
355 if (!(fgets(line, sizeof(line), f))) {
363 /* Let's skip the pid and comm fields. The latter is enclosed
364 * in () but does not escape any () in its value, so let's
365 * skip over it manually */
367 if (!(p = strrchr(line, ')')))
378 if ((long long unsigned) (pid_t) ppid != ppid)
381 *_ppid = (pid_t) ppid;
386 int write_one_line_file(const char *fn, const char *line) {
393 if (!(f = fopen(fn, "we")))
396 if (fputs(line, f) < 0) {
407 int read_one_line_file(const char *fn, char **line) {
415 if (!(f = fopen(fn, "re")))
418 if (!(fgets(t, sizeof(t), f))) {
423 if (!(c = strdup(t))) {
436 char *strappend(const char *s, const char *suffix) {
446 if (!(r = new(char, a+b+1)))
450 memcpy(r+a, suffix, b);
456 int readlink_malloc(const char *p, char **r) {
466 if (!(c = new(char, l)))
469 if ((n = readlink(p, c, l-1)) < 0) {
475 if ((size_t) n < l-1) {
486 char *file_name_from_path(const char *p) {
491 if ((r = strrchr(p, '/')))
497 bool path_is_absolute(const char *p) {
503 bool is_path(const char *p) {
505 return !!strchr(p, '/');
508 char *path_make_absolute(const char *p, const char *prefix) {
513 if (path_is_absolute(p) || !prefix)
516 if (asprintf(&r, "%s/%s", prefix, p) < 0)
522 int reset_all_signal_handlers(void) {
525 for (sig = 1; sig < _NSIG; sig++) {
528 if (sig == SIGKILL || sig == SIGSTOP)
532 sa.sa_handler = SIG_DFL;
533 sa.sa_flags = SA_RESTART;
535 /* On Linux the first two RT signals are reserved by
536 * glibc, and sigaction() will return EINVAL for them. */
537 if ((sigaction(sig, &sa, NULL) < 0))
545 char *strstrip(char *s) {
548 /* Drops trailing whitespace. Modifies the string in
549 * place. Returns pointer to first non-space character */
551 s += strspn(s, WHITESPACE);
554 if (!strchr(WHITESPACE, *e))
566 char *file_in_same_dir(const char *path, const char *filename) {
573 /* This removes the last component of path and appends
574 * filename, unless the latter is absolute anyway or the
577 if (path_is_absolute(filename))
578 return strdup(filename);
580 if (!(e = strrchr(path, '/')))
581 return strdup(filename);
583 k = strlen(filename);
584 if (!(r = new(char, e-path+1+k+1)))
587 memcpy(r, path, e-path+1);
588 memcpy(r+(e-path)+1, filename, k+1);
593 char hexchar(int x) {
594 static const char table[16] = "0123456789abcdef";
596 return table[x & 15];
599 int unhexchar(char c) {
601 if (c >= '0' && c <= '9')
604 if (c >= 'a' && c <= 'f')
607 if (c >= 'A' && c <= 'F')
613 char octchar(int x) {
614 return '0' + (x & 7);
617 int unoctchar(char c) {
619 if (c >= '0' && c <= '7')
625 char *cescape(const char *s) {
631 /* Does C style string escaping. */
633 if (!(r = new(char, strlen(s)*4 + 1)))
636 for (f = s, t = r; *f; f++)
682 /* For special chars we prefer octal over
683 * hexadecimal encoding, simply because glib's
684 * g_strescape() does the same */
685 if ((*f < ' ') || (*f >= 127)) {
687 *(t++) = octchar((unsigned char) *f >> 6);
688 *(t++) = octchar((unsigned char) *f >> 3);
689 *(t++) = octchar((unsigned char) *f);
700 char *cunescape(const char *s) {
706 /* Undoes C style string escaping */
708 if (!(r = new(char, strlen(s)+1)))
711 for (f = s, t = r; *f; f++) {
754 /* hexadecimal encoding */
757 if ((a = unhexchar(f[1])) < 0 ||
758 (b = unhexchar(f[2])) < 0) {
759 /* Invalid escape code, let's take it literal then */
763 *(t++) = (char) ((a << 4) | b);
781 if ((a = unoctchar(f[0])) < 0 ||
782 (b = unoctchar(f[1])) < 0 ||
783 (c = unoctchar(f[2])) < 0) {
784 /* Invalid escape code, let's take it literal then */
788 *(t++) = (char) ((a << 6) | (b << 3) | c);
796 /* premature end of string.*/
801 /* Invalid escape code, let's take it literal then */
814 char *xescape(const char *s, const char *bad) {
818 /* Escapes all chars in bad, in addition to \ and all special
819 * chars, in \xFF style escaping. May be reversed with
822 if (!(r = new(char, strlen(s)*4+1)))
825 for (f = s, t = r; *f; f++) {
827 if (*f < ' ' || *f >= 127 ||
828 *f == '\\' || strchr(bad, *f)) {
831 *(t++) = hexchar(*f >> 4);
832 *(t++) = hexchar(*f);
842 char *bus_path_escape(const char *s) {
848 /* Escapes all chars that D-Bus' object path cannot deal
849 * with. Can be reverse with bus_path_unescape() */
851 if (!(r = new(char, strlen(s)*3+1)))
854 for (f = s, t = r; *f; f++) {
856 if (!(*f >= 'A' && *f <= 'Z') &&
857 !(*f >= 'a' && *f <= 'z') &&
858 !(*f >= '0' && *f <= '9')) {
860 *(t++) = hexchar(*f >> 4);
861 *(t++) = hexchar(*f);
871 char *bus_path_unescape(const char *s) {
877 if (!(r = new(char, strlen(s)+1)))
880 for (f = s, t = r; *f; f++) {
885 if ((a = unhexchar(f[1])) < 0 ||
886 (b = unhexchar(f[2])) < 0) {
887 /* Invalid escape code, let's take it literal then */
890 *(t++) = (char) ((a << 4) | b);
902 char *path_kill_slashes(char *path) {
906 /* Removes redundant inner and trailing slashes. Modifies the
907 * passed string in-place.
909 * ///foo///bar/ becomes /foo/bar
912 for (f = path, t = path; *f; f++) {
927 /* Special rule, if we are talking of the root directory, a
928 trailing slash is good */
930 if (t == path && slash)
937 bool path_startswith(const char *path, const char *prefix) {
941 if ((path[0] == '/') != (prefix[0] == '/'))
947 path += strspn(path, "/");
948 prefix += strspn(prefix, "/");
956 a = strcspn(path, "/");
957 b = strcspn(prefix, "/");
962 if (memcmp(path, prefix, a) != 0)
970 char *ascii_strlower(char *path) {
975 for (p = path; *p; p++)
976 if (*p >= 'A' && *p <= 'Z')
982 static const char *const ioprio_class_table[] = {
983 [IOPRIO_CLASS_NONE] = "none",
984 [IOPRIO_CLASS_RT] = "realtime",
985 [IOPRIO_CLASS_BE] = "best-effort",
986 [IOPRIO_CLASS_IDLE] = "idle"
989 DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
991 static const char *const sigchld_code_table[] = {
992 [CLD_EXITED] = "exited",
993 [CLD_KILLED] = "killed",
994 [CLD_DUMPED] = "dumped",
995 [CLD_TRAPPED] = "trapped",
996 [CLD_STOPPED] = "stopped",
997 [CLD_CONTINUED] = "continued",
1000 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
1002 static const char *const log_facility_table[LOG_NFACILITIES] = {
1003 [LOG_FAC(LOG_KERN)] = "kern",
1004 [LOG_FAC(LOG_USER)] = "user",
1005 [LOG_FAC(LOG_MAIL)] = "mail",
1006 [LOG_FAC(LOG_DAEMON)] = "daemon",
1007 [LOG_FAC(LOG_AUTH)] = "auth",
1008 [LOG_FAC(LOG_SYSLOG)] = "syslog",
1009 [LOG_FAC(LOG_LPR)] = "lpr",
1010 [LOG_FAC(LOG_NEWS)] = "news",
1011 [LOG_FAC(LOG_UUCP)] = "uucp",
1012 [LOG_FAC(LOG_CRON)] = "cron",
1013 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
1014 [LOG_FAC(LOG_FTP)] = "ftp",
1015 [LOG_FAC(LOG_LOCAL0)] = "local0",
1016 [LOG_FAC(LOG_LOCAL1)] = "local1",
1017 [LOG_FAC(LOG_LOCAL2)] = "local2",
1018 [LOG_FAC(LOG_LOCAL3)] = "local3",
1019 [LOG_FAC(LOG_LOCAL4)] = "local4",
1020 [LOG_FAC(LOG_LOCAL5)] = "local5",
1021 [LOG_FAC(LOG_LOCAL6)] = "local6",
1022 [LOG_FAC(LOG_LOCAL7)] = "local7"
1025 DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
1027 static const char *const log_level_table[] = {
1028 [LOG_EMERG] = "emerg",
1029 [LOG_ALERT] = "alert",
1030 [LOG_CRIT] = "crit",
1032 [LOG_WARNING] = "warning",
1033 [LOG_NOTICE] = "notice",
1034 [LOG_INFO] = "info",
1035 [LOG_DEBUG] = "debug"
1038 DEFINE_STRING_TABLE_LOOKUP(log_level, int);
1040 static const char* const sched_policy_table[] = {
1041 [SCHED_OTHER] = "other",
1042 [SCHED_BATCH] = "batch",
1043 [SCHED_IDLE] = "idle",
1044 [SCHED_FIFO] = "fifo",
1048 DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
1050 static const char* const rlimit_table[] = {
1051 [RLIMIT_CPU] = "LimitCPU",
1052 [RLIMIT_FSIZE] = "LimitFSIZE",
1053 [RLIMIT_DATA] = "LimitDATA",
1054 [RLIMIT_STACK] = "LimitSTACK",
1055 [RLIMIT_CORE] = "LimitCORE",
1056 [RLIMIT_RSS] = "LimitRSS",
1057 [RLIMIT_NOFILE] = "LimitNOFILE",
1058 [RLIMIT_AS] = "LimitAS",
1059 [RLIMIT_NPROC] = "LimitNPROC",
1060 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
1061 [RLIMIT_LOCKS] = "LimitLOCKS",
1062 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
1063 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
1064 [RLIMIT_NICE] = "LimitNICE",
1065 [RLIMIT_RTPRIO] = "LimitRTPRIO",
1066 [RLIMIT_RTTIME] = "LimitRTTIME"
1069 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);