]>
git.ipfire.org Git - thirdparty/systemd.git/blob - util.c
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 string into words, but consider strings enclosed in '' and
288 * "" as words even if they include spaces. */
289 char *split_quoted(const char *c
, size_t *l
, char **state
) {
292 current
= *state
? *state
: (char*) c
;
294 if (!*current
|| *c
== 0)
297 current
+= strspn(current
, WHITESPACE
);
299 if (*current
== '\'') {
301 *l
= strcspn(current
, "'");
306 } else if (*current
== '\"') {
308 *l
= strcspn(current
, "\"");
314 *l
= strcspn(current
, WHITESPACE
);
318 /* FIXME: Cannot deal with strings that have spaces AND ticks
321 return (char*) current
;
324 int get_parent_of_pid(pid_t pid
, pid_t
*_ppid
) {
327 char fn
[132], line
[256], *p
;
328 long long unsigned ppid
;
333 assert_se(snprintf(fn
, sizeof(fn
)-1, "/proc/%llu/stat", (unsigned long long) pid
) < (int) (sizeof(fn
)-1));
334 fn
[sizeof(fn
)-1] = 0;
336 if (!(f
= fopen(fn
, "r")))
339 if (!(fgets(line
, sizeof(line
), f
))) {
347 /* Let's skip the pid and comm fields. The latter is enclosed
348 * in () but does not escape any () in its value, so let's
349 * skip over it manually */
351 if (!(p
= strrchr(line
, ')')))
362 if ((long long unsigned) (pid_t
) ppid
!= ppid
)
365 *_ppid
= (pid_t
) ppid
;
370 int write_one_line_file(const char *fn
, const char *line
) {
377 if (!(f
= fopen(fn
, "we")))
380 if (fputs(line
, f
) < 0) {
391 int read_one_line_file(const char *fn
, char **line
) {
399 if (!(f
= fopen(fn
, "re")))
402 if (!(fgets(t
, sizeof(t
), f
))) {
407 if (!(c
= strdup(t
))) {
420 char *strappend(const char *s
, const char *suffix
) {
430 if (!(r
= new(char, a
+b
+1)))
434 memcpy(r
+a
, suffix
, b
);
440 int readlink_malloc(const char *p
, char **r
) {
450 if (!(c
= new(char, l
)))
453 if ((n
= readlink(p
, c
, l
-1)) < 0) {
459 if ((size_t) n
< l
-1) {
470 char *file_name_from_path(const char *p
) {
475 if ((r
= strrchr(p
, '/')))
481 bool path_is_absolute(const char *p
) {
487 bool is_path(const char *p
) {
489 return !!strchr(p
, '/');
492 char *path_make_absolute(const char *p
, const char *prefix
) {
497 if (path_is_absolute(p
) || !prefix
)
500 if (asprintf(&r
, "%s/%s", prefix
, p
) < 0)
506 int reset_all_signal_handlers(void) {
509 for (sig
= 1; sig
< _NSIG
; sig
++) {
512 if (sig
== SIGKILL
|| sig
== SIGSTOP
)
516 sa
.sa_handler
= SIG_DFL
;
517 sa
.sa_flags
= SA_RESTART
;
519 /* On Linux the first two RT signals are reserved by
520 * glibc, and sigaction() will return EINVAL for them. */
521 if ((sigaction(sig
, &sa
, NULL
) < 0))
529 char *strstrip(char *s
) {
532 /* Drops trailing whitespace. Modifies the string in
533 * place. Returns pointer to first non-space character */
535 s
+= strspn(s
, WHITESPACE
);
538 if (!strchr(WHITESPACE
, *e
))
550 char *file_in_same_dir(const char *path
, const char *filename
) {
557 /* This removes the last component of path and appends
558 * filename, unless the latter is absolute anyway or the
561 if (path_is_absolute(filename
))
562 return strdup(filename
);
564 if (!(e
= strrchr(path
, '/')))
565 return strdup(filename
);
567 k
= strlen(filename
);
568 if (!(r
= new(char, e
-path
+1+k
+1)))
571 memcpy(r
, path
, e
-path
+1);
572 memcpy(r
+(e
-path
)+1, filename
, k
+1);
577 char hexchar(int x
) {
578 static const char table
[16] = "0123456789abcdef";
580 return table
[x
& 15];
583 int unhexchar(char c
) {
585 if (c
>= '0' && c
<= '9')
588 if (c
>= 'a' && c
<= 'f')
591 if (c
>= 'A' && c
<= 'F')
597 char octchar(int x
) {
598 return '0' + (x
& 7);
601 int unoctchar(char c
) {
603 if (c
>= '0' && c
<= '7')
609 char *cescape(const char *s
) {
615 /* Does C style string escaping. */
617 if (!(r
= new(char, strlen(s
)*4 + 1)))
620 for (f
= s
, t
= r
; *f
; f
++)
666 /* For special chars we prefer octal over
667 * hexadecimal encoding, simply because glib's
668 * g_strescape() does the same */
669 if ((*f
< ' ') || (*f
>= 127)) {
671 *(t
++) = octchar((unsigned char) *f
>> 6);
672 *(t
++) = octchar((unsigned char) *f
>> 3);
673 *(t
++) = octchar((unsigned char) *f
);
684 char *cunescape(const char *s
) {
690 /* Undoes C style string escaping */
692 if (!(r
= new(char, strlen(s
)+1)))
695 for (f
= s
, t
= r
; *f
; f
++) {
738 /* hexadecimal encoding */
741 if ((a
= unhexchar(f
[1])) < 0 ||
742 (b
= unhexchar(f
[2])) < 0) {
743 /* Invalid escape code, let's take it literal then */
747 *(t
++) = (char) ((a
<< 4) | b
);
765 if ((a
= unoctchar(f
[0])) < 0 ||
766 (b
= unoctchar(f
[1])) < 0 ||
767 (c
= unoctchar(f
[2])) < 0) {
768 /* Invalid escape code, let's take it literal then */
772 *(t
++) = (char) ((a
<< 6) | (b
<< 3) | c
);
780 /* premature end of string.*/
785 /* Invalid escape code, let's take it literal then */
798 char *xescape(const char *s
, const char *bad
) {
802 /* Escapes all chars in bad, in addition to \ and all special
803 * chars, in \xFF style escaping. May be reversed with
806 if (!(r
= new(char, strlen(s
)*4+1)))
809 for (f
= s
, t
= r
; *f
; f
++) {
811 if (*f
< ' ' || *f
>= 127 ||
812 *f
== '\\' || strchr(bad
, *f
)) {
815 *(t
++) = hexchar(*f
>> 4);
816 *(t
++) = hexchar(*f
);
826 char *bus_path_escape(const char *s
) {
832 /* Escapes all chars that D-Bus' object path cannot deal
833 * with. Can be reverse with bus_path_unescape() */
835 if (!(r
= new(char, strlen(s
)*3+1)))
838 for (f
= s
, t
= r
; *f
; f
++) {
840 if (!(*f
>= 'A' && *f
<= 'Z') &&
841 !(*f
>= 'a' && *f
<= 'z') &&
842 !(*f
>= '0' && *f
<= '9')) {
844 *(t
++) = hexchar(*f
>> 4);
845 *(t
++) = hexchar(*f
);
855 char *bus_path_unescape(const char *s
) {
861 if (!(r
= new(char, strlen(s
)+1)))
864 for (f
= s
, t
= r
; *f
; f
++) {
869 if ((a
= unhexchar(f
[1])) < 0 ||
870 (b
= unhexchar(f
[2])) < 0) {
871 /* Invalid escape code, let's take it literal then */
874 *(t
++) = (char) ((a
<< 4) | b
);
886 char *path_kill_slashes(char *path
) {
890 /* Removes redundant inner and trailing slashes. Modifies the
891 * passed string in-place.
893 * ///foo///bar/ becomes /foo/bar
896 for (f
= path
, t
= path
; *f
; f
++) {
911 /* Special rule, if we are talking of the root directory, a
912 trailing slash is good */
914 if (t
== path
&& slash
)
921 bool path_startswith(const char *path
, const char *prefix
) {
925 if ((path
[0] == '/') != (prefix
[0] == '/'))
931 path
+= strspn(path
, "/");
932 prefix
+= strspn(prefix
, "/");
940 a
= strcspn(path
, "/");
941 b
= strcspn(prefix
, "/");
946 if (memcmp(path
, prefix
, a
) != 0)
954 char *ascii_strlower(char *path
) {
959 for (p
= path
; *p
; p
++)
960 if (*p
>= 'A' && *p
<= 'Z')
966 static const char *const ioprio_class_table
[] = {
967 [IOPRIO_CLASS_NONE
] = "none",
968 [IOPRIO_CLASS_RT
] = "realtime",
969 [IOPRIO_CLASS_BE
] = "best-effort",
970 [IOPRIO_CLASS_IDLE
] = "idle"
973 DEFINE_STRING_TABLE_LOOKUP(ioprio_class
, int);
975 static const char *const sigchld_code_table
[] = {
976 [CLD_EXITED
] = "exited",
977 [CLD_KILLED
] = "killed",
978 [CLD_DUMPED
] = "dumped",
979 [CLD_TRAPPED
] = "trapped",
980 [CLD_STOPPED
] = "stopped",
981 [CLD_CONTINUED
] = "continued",
984 DEFINE_STRING_TABLE_LOOKUP(sigchld_code
, int);
986 static const char *const log_facility_table
[LOG_NFACILITIES
] = {
987 [LOG_FAC(LOG_KERN
)] = "kern",
988 [LOG_FAC(LOG_USER
)] = "user",
989 [LOG_FAC(LOG_MAIL
)] = "mail",
990 [LOG_FAC(LOG_DAEMON
)] = "daemon",
991 [LOG_FAC(LOG_AUTH
)] = "auth",
992 [LOG_FAC(LOG_SYSLOG
)] = "syslog",
993 [LOG_FAC(LOG_LPR
)] = "lpr",
994 [LOG_FAC(LOG_NEWS
)] = "news",
995 [LOG_FAC(LOG_UUCP
)] = "uucp",
996 [LOG_FAC(LOG_CRON
)] = "cron",
997 [LOG_FAC(LOG_AUTHPRIV
)] = "authpriv",
998 [LOG_FAC(LOG_FTP
)] = "ftp",
999 [LOG_FAC(LOG_LOCAL0
)] = "local0",
1000 [LOG_FAC(LOG_LOCAL1
)] = "local1",
1001 [LOG_FAC(LOG_LOCAL2
)] = "local2",
1002 [LOG_FAC(LOG_LOCAL3
)] = "local3",
1003 [LOG_FAC(LOG_LOCAL4
)] = "local4",
1004 [LOG_FAC(LOG_LOCAL5
)] = "local5",
1005 [LOG_FAC(LOG_LOCAL6
)] = "local6",
1006 [LOG_FAC(LOG_LOCAL7
)] = "local7"
1009 DEFINE_STRING_TABLE_LOOKUP(log_facility
, int);
1011 static const char *const log_level_table
[] = {
1012 [LOG_EMERG
] = "emerg",
1013 [LOG_ALERT
] = "alert",
1014 [LOG_CRIT
] = "crit",
1016 [LOG_WARNING
] = "warning",
1017 [LOG_NOTICE
] = "notice",
1018 [LOG_INFO
] = "info",
1019 [LOG_DEBUG
] = "debug"
1022 DEFINE_STRING_TABLE_LOOKUP(log_level
, int);
1024 static const char* const sched_policy_table
[] = {
1025 [SCHED_OTHER
] = "other",
1026 [SCHED_BATCH
] = "batch",
1027 [SCHED_IDLE
] = "idle",
1028 [SCHED_FIFO
] = "fifo",
1032 DEFINE_STRING_TABLE_LOOKUP(sched_policy
, int);
1034 static const char* const rlimit_table
[] = {
1035 [RLIMIT_CPU
] = "LimitCPU",
1036 [RLIMIT_FSIZE
] = "LimitFSIZE",
1037 [RLIMIT_DATA
] = "LimitDATA",
1038 [RLIMIT_STACK
] = "LimitSTACK",
1039 [RLIMIT_CORE
] = "LimitCORE",
1040 [RLIMIT_RSS
] = "LimitRSS",
1041 [RLIMIT_NOFILE
] = "LimitNOFILE",
1042 [RLIMIT_AS
] = "LimitAS",
1043 [RLIMIT_NPROC
] = "LimitNPROC",
1044 [RLIMIT_MEMLOCK
] = "LimitMEMLOCK",
1045 [RLIMIT_LOCKS
] = "LimitLOCKS",
1046 [RLIMIT_SIGPENDING
] = "LimitSIGPENDING",
1047 [RLIMIT_MSGQUEUE
] = "LimitMSGQUEUE",
1048 [RLIMIT_NICE
] = "LimitNICE",
1049 [RLIMIT_RTPRIO
] = "LimitRTPRIO",
1050 [RLIMIT_RTTIME
] = "LimitRTTIME"
1053 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);