]>
git.ipfire.org Git - people/ms/systemd.git/blob - util.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
12 #include <sys/resource.h>
13 #include <linux/sched.h>
20 usec_t
now(clockid_t clock
) {
23 assert_se(clock_gettime(clock
, &ts
) == 0);
25 return timespec_load(&ts
);
28 usec_t
timespec_load(const struct timespec
*ts
) {
32 (usec_t
) ts
->tv_sec
* USEC_PER_SEC
+
33 (usec_t
) ts
->tv_nsec
/ NSEC_PER_USEC
;
36 struct timespec
*timespec_store(struct timespec
*ts
, usec_t u
) {
39 ts
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
40 ts
->tv_nsec
= (long int) ((u
% USEC_PER_SEC
) * NSEC_PER_USEC
);
45 usec_t
timeval_load(const struct timeval
*tv
) {
49 (usec_t
) tv
->tv_sec
* USEC_PER_SEC
+
53 struct timeval
*timeval_store(struct timeval
*tv
, usec_t u
) {
56 tv
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
57 tv
->tv_usec
= (suseconds_t
) (u
% USEC_PER_SEC
);
62 bool endswith(const char *s
, const char *postfix
) {
74 return memcmp(s
+ sl
- pl
, postfix
, pl
) == 0;
77 bool startswith(const char *s
, const char *prefix
) {
89 return memcmp(s
, prefix
, pl
) == 0;
92 bool first_word(const char *s
, const char *word
) {
104 if (memcmp(s
, word
, wl
) != 0)
107 return (s
[wl
] == 0 ||
108 strchr(WHITESPACE
, s
[wl
]));
111 int close_nointr(int fd
) {
117 if ((r
= close(fd
)) >= 0)
125 void close_nointr_nofail(int fd
) {
127 /* like close_nointr() but cannot fail, and guarantees errno
130 assert_se(close_nointr(fd
) == 0);
133 int parse_boolean(const char *v
) {
136 if (streq(v
, "1") || v
[0] == 'y' || v
[0] == 'Y' || v
[0] == 't' || v
[0] == 'T' || !strcasecmp(v
, "on"))
138 else if (streq(v
, "0") || v
[0] == 'n' || v
[0] == 'N' || v
[0] == 'f' || v
[0] == 'F' || !strcasecmp(v
, "off"))
144 int safe_atou(const char *s
, unsigned *ret_u
) {
152 l
= strtoul(s
, &x
, 0);
154 if (!x
|| *x
|| errno
)
155 return errno
? -errno
: -EINVAL
;
157 if ((unsigned long) (unsigned) l
!= l
)
160 *ret_u
= (unsigned) l
;
164 int safe_atoi(const char *s
, int *ret_i
) {
172 l
= strtol(s
, &x
, 0);
174 if (!x
|| *x
|| errno
)
175 return errno
? -errno
: -EINVAL
;
177 if ((long) (int) l
!= l
)
184 int safe_atolu(const char *s
, long unsigned *ret_lu
) {
192 l
= strtoul(s
, &x
, 0);
194 if (!x
|| *x
|| errno
)
195 return errno
? -errno
: -EINVAL
;
201 int safe_atoli(const char *s
, long int *ret_li
) {
209 l
= strtol(s
, &x
, 0);
211 if (!x
|| *x
|| errno
)
212 return errno
? -errno
: -EINVAL
;
218 int safe_atollu(const char *s
, long long unsigned *ret_llu
) {
220 unsigned long long l
;
226 l
= strtoull(s
, &x
, 0);
228 if (!x
|| *x
|| errno
)
229 return errno
? -errno
: -EINVAL
;
235 int safe_atolli(const char *s
, long long int *ret_lli
) {
243 l
= strtoll(s
, &x
, 0);
245 if (!x
|| *x
|| errno
)
246 return errno
? -errno
: -EINVAL
;
252 /* Split a string into words. */
253 char *split_spaces(const char *c
, size_t *l
, char **state
) {
256 current
= *state
? *state
: (char*) c
;
258 if (!*current
|| *c
== 0)
261 current
+= strspn(current
, WHITESPACE
);
262 *l
= strcspn(current
, WHITESPACE
);
265 return (char*) current
;
268 /* Split a string into words, but consider strings enclosed in '' and
269 * "" as words even if they include spaces. */
270 char *split_quoted(const char *c
, size_t *l
, char **state
) {
273 current
= *state
? *state
: (char*) c
;
275 if (!*current
|| *c
== 0)
278 current
+= strspn(current
, WHITESPACE
);
280 if (*current
== '\'') {
282 *l
= strcspn(current
, "'");
287 } else if (*current
== '\"') {
289 *l
= strcspn(current
, "\"");
295 *l
= strcspn(current
, WHITESPACE
);
299 /* FIXME: Cannot deal with strings that have spaces AND ticks
302 return (char*) current
;
305 int get_parent_of_pid(pid_t pid
, pid_t
*_ppid
) {
308 char fn
[132], line
[256], *p
;
309 long long unsigned ppid
;
314 assert_se(snprintf(fn
, sizeof(fn
)-1, "/proc/%llu/stat", (unsigned long long) pid
) < (int) (sizeof(fn
)-1));
315 fn
[sizeof(fn
)-1] = 0;
317 if (!(f
= fopen(fn
, "r")))
320 if (!(fgets(line
, sizeof(line
), f
))) {
328 /* Let's skip the pid and comm fields. The latter is enclosed
329 * in () but does not escape any () in its value, so let's
330 * skip over it manually */
332 if (!(p
= strrchr(line
, ')')))
343 if ((long long unsigned) (pid_t
) ppid
!= ppid
)
346 *_ppid
= (pid_t
) ppid
;
351 int write_one_line_file(const char *fn
, const char *line
) {
358 if (!(f
= fopen(fn
, "we")))
361 if (fputs(line
, f
) < 0) {
372 int read_one_line_file(const char *fn
, char **line
) {
380 if (!(f
= fopen(fn
, "re")))
383 if (!(fgets(t
, sizeof(t
), f
))) {
388 if (!(c
= strdup(t
))) {
401 char *strappend(const char *s
, const char *suffix
) {
411 if (!(r
= new(char, a
+b
+1)))
415 memcpy(r
+a
, suffix
, b
);
421 int readlink_malloc(const char *p
, char **r
) {
431 if (!(c
= new(char, l
)))
434 if ((n
= readlink(p
, c
, l
-1)) < 0) {
440 if ((size_t) n
< l
-1) {
451 char *file_name_from_path(const char *p
) {
456 if ((r
= strrchr(p
, '/')))
462 bool path_is_absolute(const char *p
) {
468 bool is_path(const char *p
) {
470 return !!strchr(p
, '/');
473 char *path_make_absolute(const char *p
, const char *prefix
) {
478 if (path_is_absolute(p
) || !prefix
)
481 if (asprintf(&r
, "%s/%s", prefix
, p
) < 0)
487 int reset_all_signal_handlers(void) {
490 for (sig
= 1; sig
< _NSIG
; sig
++) {
493 if (sig
== SIGKILL
|| sig
== SIGSTOP
)
497 sa
.sa_handler
= SIG_DFL
;
498 sa
.sa_flags
= SA_RESTART
;
500 /* On Linux the first two RT signals are reserved by
501 * glibc, and sigaction() will return EINVAL for them. */
502 if ((sigaction(sig
, &sa
, NULL
) < 0))
510 char *strstrip(char *s
) {
513 /* Drops trailing whitespace. Modifies the string in
514 * place. Returns pointer to first non-space character */
516 s
+= strspn(s
, WHITESPACE
);
519 if (!strchr(WHITESPACE
, *e
))
531 char *file_in_same_dir(const char *path
, const char *filename
) {
538 /* This removes the last component of path and appends
539 * filename, unless the latter is absolute anyway or the
542 if (path_is_absolute(filename
))
543 return strdup(filename
);
545 if (!(e
= strrchr(path
, '/')))
546 return strdup(filename
);
548 k
= strlen(filename
);
549 if (!(r
= new(char, e
-path
+1+k
+1)))
552 memcpy(r
, path
, e
-path
+1);
553 memcpy(r
+(e
-path
)+1, filename
, k
+1);
558 char hexchar(int x
) {
559 static const char table
[16] = "0123456789abcdef";
561 return table
[x
& 15];
564 int unhexchar(char c
) {
566 if (c
>= '0' && c
<= '9')
569 if (c
>= 'a' && c
<= 'f')
572 if (c
>= 'A' && c
<= 'F')
578 char octchar(int x
) {
579 return '0' + (x
& 7);
582 int unoctchar(char c
) {
584 if (c
>= '0' && c
<= '7')
590 char *cescape(const char *s
) {
596 /* Does C style string escaping. */
598 if (!(r
= new(char, strlen(s
)*4 + 1)))
601 for (f
= s
, t
= r
; *f
; f
++)
647 /* For special chars we prefer octal over
648 * hexadecimal encoding, simply because glib's
649 * g_strescape() does the same */
650 if ((*f
< ' ') || (*f
>= 127)) {
652 *(t
++) = octchar((unsigned char) *f
>> 6);
653 *(t
++) = octchar((unsigned char) *f
>> 3);
654 *(t
++) = octchar((unsigned char) *f
);
665 char *cunescape(const char *s
) {
671 /* Undoes C style string escaping */
673 if (!(r
= new(char, strlen(s
)+1)))
676 for (f
= s
, t
= r
; *f
; f
++) {
719 /* hexadecimal encoding */
722 if ((a
= unhexchar(f
[1])) < 0 ||
723 (b
= unhexchar(f
[2])) < 0) {
724 /* Invalid escape code, let's take it literal then */
728 *(t
++) = (char) ((a
<< 4) | b
);
746 if ((a
= unoctchar(f
[0])) < 0 ||
747 (b
= unoctchar(f
[1])) < 0 ||
748 (c
= unoctchar(f
[2])) < 0) {
749 /* Invalid escape code, let's take it literal then */
753 *(t
++) = (char) ((a
<< 6) | (b
<< 3) | c
);
761 /* premature end of string.*/
766 /* Invalid escape code, let's take it literal then */
779 char *xescape(const char *s
, const char *bad
) {
783 /* Escapes all chars in bad, in addition to \ and all special
784 * chars, in \xFF style escaping. May be reversed with
787 if (!(r
= new(char, strlen(s
)*4+1)))
790 for (f
= s
, t
= r
; *f
; f
++) {
792 if (*f
< ' ' || *f
>= 127 ||
793 *f
== '\\' || strchr(bad
, *f
)) {
796 *(t
++) = hexchar(*f
>> 4);
797 *(t
++) = hexchar(*f
);
807 char *bus_path_escape(const char *s
) {
813 /* Escapes all chars that D-Bus' object path cannot deal
814 * with. Can be reverse with bus_path_unescape() */
816 if (!(r
= new(char, strlen(s
)*3+1)))
819 for (f
= s
, t
= r
; *f
; f
++) {
821 if (!(*f
>= 'A' && *f
<= 'Z') &&
822 !(*f
>= 'a' && *f
<= 'z') &&
823 !(*f
>= '0' && *f
<= '9')) {
825 *(t
++) = hexchar(*f
>> 4);
826 *(t
++) = hexchar(*f
);
836 char *bus_path_unescape(const char *s
) {
842 if (!(r
= new(char, strlen(s
)+1)))
845 for (f
= s
, t
= r
; *f
; f
++) {
850 if ((a
= unhexchar(f
[1])) < 0 ||
851 (b
= unhexchar(f
[2])) < 0) {
852 /* Invalid escape code, let's take it literal then */
855 *(t
++) = (char) ((a
<< 4) | b
);
867 char *path_kill_slashes(char *path
) {
871 /* Removes redundant inner and trailing slashes. Modifies the
872 * passed string in-place.
874 * ///foo///bar/ becomes /foo/bar
877 for (f
= path
, t
= path
; *f
; f
++) {
892 /* Special rule, if we are talking of the root directory, a
893 trailing slash is good */
895 if (t
== path
&& slash
)
902 bool path_startswith(const char *path
, const char *prefix
) {
906 if ((path
[0] == '/') != (prefix
[0] == '/'))
912 path
+= strspn(path
, "/");
913 prefix
+= strspn(prefix
, "/");
921 a
= strcspn(path
, "/");
922 b
= strcspn(prefix
, "/");
927 if (memcmp(path
, prefix
, a
) != 0)
935 char *ascii_strlower(char *path
) {
940 for (p
= path
; *p
; p
++)
941 if (*p
>= 'A' && *p
<= 'Z')
947 static const char *const ioprio_class_table
[] = {
948 [IOPRIO_CLASS_NONE
] = "none",
949 [IOPRIO_CLASS_RT
] = "realtime",
950 [IOPRIO_CLASS_BE
] = "best-effort",
951 [IOPRIO_CLASS_IDLE
] = "idle"
954 DEFINE_STRING_TABLE_LOOKUP(ioprio_class
, int);
956 static const char *const sigchld_code_table
[] = {
957 [CLD_EXITED
] = "exited",
958 [CLD_KILLED
] = "killed",
959 [CLD_DUMPED
] = "dumped",
960 [CLD_TRAPPED
] = "trapped",
961 [CLD_STOPPED
] = "stopped",
962 [CLD_CONTINUED
] = "continued",
965 DEFINE_STRING_TABLE_LOOKUP(sigchld_code
, int);
967 static const char *const log_facility_table
[LOG_NFACILITIES
] = {
968 [LOG_FAC(LOG_KERN
)] = "kern",
969 [LOG_FAC(LOG_USER
)] = "user",
970 [LOG_FAC(LOG_MAIL
)] = "mail",
971 [LOG_FAC(LOG_DAEMON
)] = "daemon",
972 [LOG_FAC(LOG_AUTH
)] = "auth",
973 [LOG_FAC(LOG_SYSLOG
)] = "syslog",
974 [LOG_FAC(LOG_LPR
)] = "lpr",
975 [LOG_FAC(LOG_NEWS
)] = "news",
976 [LOG_FAC(LOG_UUCP
)] = "uucp",
977 [LOG_FAC(LOG_CRON
)] = "cron",
978 [LOG_FAC(LOG_AUTHPRIV
)] = "authpriv",
979 [LOG_FAC(LOG_FTP
)] = "ftp",
980 [LOG_FAC(LOG_LOCAL0
)] = "local0",
981 [LOG_FAC(LOG_LOCAL1
)] = "local1",
982 [LOG_FAC(LOG_LOCAL2
)] = "local2",
983 [LOG_FAC(LOG_LOCAL3
)] = "local3",
984 [LOG_FAC(LOG_LOCAL4
)] = "local4",
985 [LOG_FAC(LOG_LOCAL5
)] = "local5",
986 [LOG_FAC(LOG_LOCAL6
)] = "local6",
987 [LOG_FAC(LOG_LOCAL7
)] = "local7"
990 DEFINE_STRING_TABLE_LOOKUP(log_facility
, int);
992 static const char *const log_level_table
[] = {
993 [LOG_EMERG
] = "emerg",
994 [LOG_ALERT
] = "alert",
997 [LOG_WARNING
] = "warning",
998 [LOG_NOTICE
] = "notice",
1000 [LOG_DEBUG
] = "debug"
1003 DEFINE_STRING_TABLE_LOOKUP(log_level
, int);
1005 static const char* const sched_policy_table
[] = {
1006 [SCHED_OTHER
] = "other",
1007 [SCHED_BATCH
] = "batch",
1008 [SCHED_IDLE
] = "idle",
1009 [SCHED_FIFO
] = "fifo",
1013 DEFINE_STRING_TABLE_LOOKUP(sched_policy
, int);
1015 static const char* const rlimit_table
[] = {
1016 [RLIMIT_CPU
] = "LimitCPU",
1017 [RLIMIT_FSIZE
] = "LimitFSIZE",
1018 [RLIMIT_DATA
] = "LimitDATA",
1019 [RLIMIT_STACK
] = "LimitSTACK",
1020 [RLIMIT_CORE
] = "LimitCORE",
1021 [RLIMIT_RSS
] = "LimitRSS",
1022 [RLIMIT_NOFILE
] = "LimitNOFILE",
1023 [RLIMIT_AS
] = "LimitAS",
1024 [RLIMIT_NPROC
] = "LimitNPROC",
1025 [RLIMIT_MEMLOCK
] = "LimitMEMLOCK",
1026 [RLIMIT_LOCKS
] = "LimitLOCKS",
1027 [RLIMIT_SIGPENDING
] = "LimitSIGPENDING",
1028 [RLIMIT_MSGQUEUE
] = "LimitMSGQUEUE",
1029 [RLIMIT_NICE
] = "LimitNICE",
1030 [RLIMIT_RTPRIO
] = "LimitRTPRIO",
1031 [RLIMIT_RTTIME
] = "LimitRTTIME"
1034 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);