commit 528abc9

Michael Forney  ·  2014-12-24 20:40:36 +0000 UTC
parent 5d97f24
launch: Add -t flag for specifying the TTY to run on
1 files changed,  +33, -38
+33, -38
  1@@ -78,7 +78,7 @@ static void __attribute__((noreturn,format(printf,1,2)))
  2 
  3 static void __attribute__((noreturn)) usage(const char * name)
  4 {
  5-    fprintf(stderr, "Usage: %s [-s <server-socket>] [--] "
  6+    fprintf(stderr, "Usage: %s [-s <server-socket>] [-t <tty-device>] [--] "
  7                     "<server> [server arguments...]\n", name);
  8     exit(EXIT_FAILURE);
  9 }
 10@@ -294,48 +294,34 @@ static void handle_socket_data(int socket)
 11     send_fd(socket, fd, &response, sizeof response);
 12 }
 13 
 14-static int find_vt()
 15+static void find_vt(char * vt, size_t size)
 16 {
 17-    char * vt_string;
 18-    int vt;
 19-    int tty0_fd;
 20-
 21-    vt_string = getenv("XDG_VTNR");
 22+    char * vt_num_string;
 23 
 24-    if (vt_string)
 25+    if ((vt_num_string = getenv("XDG_VTNR")))
 26     {
 27-        char * end;
 28-        vt = strtoul(vt_string, &end, 10);
 29-        if (*end == '\0')
 30-            goto done;
 31+        if (snprintf(vt, size, "/dev/tty%s", vt_num_string) >= size)
 32+            die("XDG_VTNR is too long");
 33+    }
 34+    else
 35+    {
 36+        int tty0_fd, vt_num;
 37+
 38+        tty0_fd = open("/dev/tty0", O_RDWR);
 39+        if (tty0_fd == -1)
 40+            die("Could not open /dev/tty0 to find unused VT");
 41+        if (ioctl(tty0_fd, VT_OPENQRY, &vt_num) != 0)
 42+            die("Could not find unused VT");
 43+        close(tty0_fd);
 44+        if (snprintf(vt, size, "/dev/tty%d", vt_num) >= size)
 45+            die("VT number is too large");
 46     }
 47-
 48-    tty0_fd = open("/dev/tty0", O_RDWR);
 49-
 50-    if (tty0_fd == -1)
 51-        die("Could not open /dev/tty0 to find unused VT");
 52-
 53-    if (ioctl(tty0_fd, VT_OPENQRY, &vt) != 0)
 54-        die("Could not find unused VT");
 55-
 56-    close(tty0_fd);
 57-    fprintf(stderr, "Running on VT %d\n", vt);
 58-
 59-  done:
 60-    return vt;
 61 }
 62 
 63-static int open_tty(int vt)
 64+static int open_tty(const char * tty_name)
 65 {
 66-    char * current_tty_name;
 67-    char tty_name[64];
 68-
 69-    snprintf(tty_name, sizeof tty_name, "/dev/tty%d", vt);
 70-
 71     /* Check if we are running on the desired VT */
 72-    current_tty_name = ttyname(STDIN_FILENO);
 73-
 74-    if (strcmp(tty_name, current_tty_name) == 0)
 75+    if (strcmp(tty_name, ttyname(STDIN_FILENO)) == 0)
 76         return STDIN_FILENO;
 77     else
 78     {
 79@@ -426,17 +412,20 @@ int main(int argc, char * argv[])
 80 {
 81     int option;
 82     int sockets[2];
 83-    int vt;
 84+    char * vt = NULL, vt_buf[64];
 85     struct sigaction action = { 0 };
 86     sigset_t set;
 87 
 88-    while ((option = getopt(argc, argv, "s:")) != -1)
 89+    while ((option = getopt(argc, argv, "s:t:")) != -1)
 90     {
 91         switch (option)
 92         {
 93             case 's':
 94                 setenv("WAYLAND_DISPLAY", optarg, true);
 95                 break;
 96+            case 't':
 97+                vt = optarg;
 98+                break;
 99             default:
100                 usage(argv[0]);
101         }
102@@ -479,7 +468,13 @@ int main(int argc, char * argv[])
103     sigdelset(&set, SIGTERM);
104     sigprocmask(SIG_SETMASK, &set, NULL);
105 
106-    vt = find_vt();
107+    if (!vt)
108+    {
109+        find_vt(vt_buf, sizeof vt_buf);
110+        vt = vt_buf;
111+    }
112+
113+    fprintf(stderr, "Running on %s\n", vt);
114     launcher.tty_fd = open_tty(vt);
115     setup_tty(launcher.tty_fd);
116