trace: use standard POSIX header for basename()
[project/procd.git] / utils / utils.c
index c0d1cd536ae6870d1386d7ac3b8be0428b5db3a0..e5b1297e6388831edeb00049311272a74b654507 100644 (file)
  * GNU General Public License for more details.
  */
 
+#define _GNU_SOURCE
 #include <libubox/avl.h>
 #include <libubox/avl-cmp.h>
 #include "utils.h"
-#include <asm-generic/setup.h>
 #include <regex.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+
+#include "../log.h"
+
+#ifndef O_PATH
+#define O_PATH         010000000
+#endif
 
 void
 __blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp)
@@ -127,10 +135,38 @@ blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2)
        return true;
 }
 
-char* get_cmdline_val(const char* name, char* out, int len)
+char *get_active_console(char *out, int len)
+{
+       char line[CMDLINE_SIZE + 1];
+       int fd = open("/sys/class/tty/console/active", O_RDONLY);
+       ssize_t r;
+
+       if (fd < 0)
+               return NULL;
+
+       r = read(fd, line, sizeof(line) - 1);
+       line[CMDLINE_SIZE] = '\0';
+
+       close(fd);
+
+       if (r <= 0)
+               return NULL;
+
+       /* The active file is terminated by a newline which we need to strip */
+       char *newline = strtok(line, "\n");
+
+       if (newline != NULL) {
+               strncpy(out, newline, len);
+               return out;
+       }
+
+       return NULL;
+}
+
+char *get_cmdline_val_offset(const char *name, char *out, int len, int offset)
 {
        char line[CMDLINE_SIZE + 1], *c, *sptr;
-       int fd = open("/proc/cmdline", O_RDONLY);
+       int i, fd = open("/proc/cmdline", O_RDONLY);
        ssize_t r = read(fd, line, sizeof(line) - 1);
        close(fd);
 
@@ -139,13 +175,18 @@ char* get_cmdline_val(const char* name, char* out, int len)
 
        line[r] = 0;
 
-       for (c = strtok_r(line, " \t\n", &sptr); c;
+       for (i = 0, c = strtok_r(line, " \t\n", &sptr); c;
                        c = strtok_r(NULL, " \t\n", &sptr)) {
                char *sep = strchr(c, '=');
+               if (sep == NULL)
+                       continue;
+
                ssize_t klen = sep - c;
-               if (klen < 0 || strncmp(name, c, klen) || name[klen] != 0)
+               if (strncmp(name, c, klen) || name[klen] != 0)
                        continue;
 
+               if (i++ < offset)
+                       continue;
                strncpy(out, &sep[1], len);
                out[len-1] = 0;
                return out;
@@ -153,3 +194,53 @@ char* get_cmdline_val(const char* name, char* out, int len)
 
        return NULL;
 }
+
+int patch_fd(const char *device, int fd, int flags)
+{
+       int dfd, nfd;
+
+       if (device == NULL)
+               device = "/dev/null";
+
+       if (*device != '/') {
+               dfd = open("/dev", O_PATH|O_DIRECTORY);
+
+               if (dfd < 0)
+                       return -1;
+
+               nfd = openat(dfd, device, flags);
+
+               close(dfd);
+       } else {
+               nfd = open(device, flags);
+       }
+
+       if (nfd < 0 && strcmp(device, "/dev/null"))
+               nfd = open("/dev/null", flags);
+
+       if (nfd < 0)
+               return -1;
+
+       fd = dup2(nfd, fd);
+
+       if (nfd > STDERR_FILENO)
+               close(nfd);
+
+       return (fd < 0) ? -1 : 0;
+}
+
+int patch_stdio(const char *device)
+{
+       int fd, rv = 0;
+       const char *fdname[3] = { "stdin", "stdout", "stderr" };
+
+       for (fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) {
+               if (patch_fd(device, fd, fd ? O_WRONLY : O_RDONLY)) {
+                       ERROR("Failed to redirect %s to %s: %m\n",
+                             fdname[fd], device);
+                       rv = -1;
+               }
+       }
+
+       return rv;
+}