inittab: detect active console from kernel if no console= specified
[project/procd.git] / utils / utils.c
index 8d76129a02b334a25a89dcc4a40f7de2de733faf..e90feecb47f3e555ed67cf79ff907561039d00d7 100644 (file)
@@ -135,6 +135,28 @@ blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2)
        return true;
 }
 
+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 = read(fd, line, sizeof(line) - 1);
+
+       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(const char* name, char* out, int len)
 {
        char line[CMDLINE_SIZE + 1], *c, *sptr;