chiark / gitweb /
Handle suspend, shutdown, reboot, etc within elogind
[elogind.git] / src / basic / util.c
index d5c758e6ff9aeb8edf64b2381dfc4304b60d4b31..38de72966ed7c606eaec020b938471100eb660f8 100644 (file)
@@ -79,7 +79,8 @@
 #include  "mkdir.h"
 #include  "path-util.h"
 // #include  "exit-status.h"
-// #include  "hashmap.h"
+#include  "hashmap.h"
+#include  "set.h"
 // #include  "env-util.h"
 #include  "fileio.h"
 // #include  "device-nodes.h"
@@ -373,6 +374,19 @@ int parse_pid(const char *s, pid_t* ret_pid) {
         return 0;
 }
 
+bool uid_is_valid(uid_t uid) {
+
+        /* Some libc APIs use UID_INVALID as special placeholder */
+        if (uid == (uid_t) 0xFFFFFFFF)
+                return false;
+
+        /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
+        if (uid == (uid_t) 0xFFFF)
+                return false;
+
+        return true;
+}
+
 int parse_uid(const char *s, uid_t* ret_uid) {
         unsigned long ul = 0;
         uid_t uid;
@@ -389,13 +403,11 @@ int parse_uid(const char *s, uid_t* ret_uid) {
         if ((unsigned long) uid != ul)
                 return -ERANGE;
 
-        /* Some libc APIs use UID_INVALID as special placeholder */
-        if (uid == (uid_t) 0xFFFFFFFF)
-                return -ENXIO;
-
-        /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
-        if (uid == (uid_t) 0xFFFF)
-                return -ENXIO;
+        if (!uid_is_valid(uid))
+                return -ENXIO; /* we return ENXIO instead of EINVAL
+                                * here, to make it easy to distuingish
+                                * invalid numeric uids invalid
+                                * strings. */
 
         if (ret_uid)
                 *ret_uid = uid;
@@ -2879,8 +2891,6 @@ bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
         return endswith(de->d_name, suffix);
 }
 
-/// UNNEEDED by elogind
-#if 0
 static int do_execute(char **directories, usec_t timeout, char *argv[]) {
         _cleanup_hashmap_free_free_ Hashmap *pids = NULL;
         _cleanup_set_free_free_ Set *seen = NULL;
@@ -3020,7 +3030,6 @@ void execute_directories(const char* const* directories, usec_t timeout, char *a
 
         wait_for_terminate_and_warn(name, executor_pid, true);
 }
-#endif // 0
 
 bool nulstr_contains(const char*nulstr, const char *needle) {
         const char *i;
@@ -4339,7 +4348,7 @@ bool is_locale_utf8(void) {
         /* Check result, but ignore the result if C was set
          * explicitly. */
         cached_answer =
-                streq(set, "C") &&
+                STR_IN_SET(set, "C", "POSIX") &&
                 !getenv("LC_ALL") &&
                 !getenv("LC_CTYPE") &&
                 !getenv("LANG");
@@ -4979,8 +4988,6 @@ int get_proc_cmdline_key(const char *key, char **value) {
 
 }
 
-/// UNNEEDED by elogind
-#if 0
 int container_get_leader(const char *machine, pid_t *pid) {
         _cleanup_free_ char *s = NULL, *class = NULL;
         const char *p;
@@ -5014,7 +5021,6 @@ int container_get_leader(const char *machine, pid_t *pid) {
         *pid = leader;
         return 0;
 }
-#endif // 0
 
 int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd) {
         _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, netnsfd = -1, usernsfd = -1;
@@ -5847,40 +5853,39 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
                 switch (state) {
 
                 case START:
-                        if (c == 0) {
-                                if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
-                                        if (!GREEDY_REALLOC(s, allocated, sz+1))
-                                                return -ENOMEM;
+                        if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
+                                if (!GREEDY_REALLOC(s, allocated, sz+1))
+                                        return -ENOMEM;
+
+                        if (c == 0)
                                 goto finish_force_terminate;
-                        else if (strchr(separators, c)) {
+                        else if (strchr(separators, c)) {
                                 if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
-                                        if (!GREEDY_REALLOC(s, allocated, sz+1))
-                                                return -ENOMEM;
                                         (*p) ++;
                                         goto finish_force_next;
                                 }
                                 break;
                         }
 
+                        /* We found a non-blank character, so we will always
+                         * want to return a string (even if it is empty),
+                         * allocate it here. */
+                        if (!GREEDY_REALLOC(s, allocated, sz+1))
+                                return -ENOMEM;
+
                         state = VALUE;
                         /* fallthrough */
 
                 case VALUE:
                         if (c == 0)
                                 goto finish_force_terminate;
-                        else if (c == '\'' && (flags & EXTRACT_QUOTES)) {
-                                if (!GREEDY_REALLOC(s, allocated, sz+1))
-                                        return -ENOMEM;
-
+                        else if (c == '\'' && (flags & EXTRACT_QUOTES))
                                 state = SINGLE_QUOTE;
-                        else if (c == '\\')
+                        else if (c == '\\')
                                 state = VALUE_ESCAPE;
-                        else if (c == '\"' && (flags & EXTRACT_QUOTES)) {
-                                if (!GREEDY_REALLOC(s, allocated, sz+1))
-                                        return -ENOMEM;
-
+                        else if (c == '\"' && (flags & EXTRACT_QUOTES))
                                 state = DOUBLE_QUOTE;
-                        else if (strchr(separators, c)) {
+                        else if (strchr(separators, c)) {
                                 if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
                                         (*p) ++;
                                         goto finish_force_next;
@@ -5984,8 +5989,6 @@ end_escape:
                 case SEPARATOR:
                         if (c == 0)
                                 goto finish_force_terminate;
-                        if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
-                                goto finish_force_next;
                         if (!strchr(separators, c))
                                 goto finish;
                         break;
@@ -6196,6 +6199,9 @@ int openpt_in_namespace(pid_t pid, int flags) {
                 if (master < 0)
                         _exit(EXIT_FAILURE);
 
+                if (unlockpt(master) < 0)
+                        _exit(EXIT_FAILURE);
+
                 cmsg = CMSG_FIRSTHDR(&mh);
                 cmsg->cmsg_level = SOL_SOCKET;
                 cmsg->cmsg_type = SCM_RIGHTS;