pty: set XDG_VTNR if requested by caller
authorDavid Herrmann <dh.herrmann@gmail.com>
Wed, 12 Jun 2013 13:50:03 +0000 (15:50 +0200)
committerDavid Herrmann <dh.herrmann@gmail.com>
Wed, 12 Jun 2013 13:50:03 +0000 (15:50 +0200)
A caller can now pass a VT-num to the PTY which will get set as XDG_VTNR
in the environment of new childs.

This can be used to tell systemd-logind to associate the session with
the correct VT. But note that this still cannot overwrite VT-associations
if the pty is created from within an existing session.

Reported-by: Thomas Hebb
Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
src/pty.c
src/pty.h

index 3e17512..3494104 100644 (file)
--- a/src/pty.c
+++ b/src/pty.c
@@ -63,6 +63,7 @@ struct kmscon_pty {
        char *colorterm;
        char **argv;
        char *seat;
+       char *vtnr;
        bool env_reset;
 };
 
@@ -119,6 +120,7 @@ void kmscon_pty_unref(struct kmscon_pty *pty)
 
        log_debug("free pty object");
        kmscon_pty_close(pty);
+       free(pty->vtnr);
        free(pty->seat);
        free(pty->argv);
        free(pty->colorterm);
@@ -193,6 +195,23 @@ int kmscon_pty_set_seat(struct kmscon_pty *pty, const char *seat)
        return 0;
 }
 
+int kmscon_pty_set_vtnr(struct kmscon_pty *pty, unsigned int vtnr)
+{
+       char *t;
+       int ret;
+
+       if (!pty)
+               return -EINVAL;
+
+       ret = asprintf(&t, "%u", vtnr);
+       if (ret < 0)
+               return -ENOMEM;
+       free(pty->vtnr);
+       pty->vtnr = t;
+
+       return 0;
+}
+
 void kmscon_pty_set_env_reset(struct kmscon_pty *pty, bool do_reset)
 {
        if (!pty)
@@ -224,7 +243,7 @@ static bool pty_is_open(struct kmscon_pty *pty)
 
 static void __attribute__((noreturn))
 exec_child(const char *term, const char *colorterm, char **argv,
-          const char *seat, bool env_reset)
+          const char *seat, const char *vtnr, bool env_reset)
 {
        char **env;
        char **def_argv;
@@ -255,6 +274,8 @@ exec_child(const char *term, const char *colorterm, char **argv,
                setenv("COLORTERM", colorterm, 1);
        if (seat)
                setenv("XDG_SEAT", seat, 1);
+       if (vtnr)
+               setenv("XDG_VTNR", vtnr, 1);
 
        execve(argv[0], argv, environ);
 
@@ -376,7 +397,7 @@ static int pty_spawn(struct kmscon_pty *pty, int master,
        case 0:
                setup_child(master, &ws);
                exec_child(pty->term, pty->colorterm, pty->argv, pty->seat,
-                          pty->env_reset);
+                          pty->vtnr, pty->env_reset);
                exit(EXIT_FAILURE);
        default:
                log_debug("forking child %d", pid);
index 67dc124..e5e051a 100644 (file)
--- a/src/pty.h
+++ b/src/pty.h
@@ -58,6 +58,7 @@ int kmscon_pty_set_term(struct kmscon_pty *pty, const char *term);
 int kmscon_pty_set_colorterm(struct kmscon_pty *pty, const char *colorterm);
 int kmscon_pty_set_argv(struct kmscon_pty *pty, char **argv);
 int kmscon_pty_set_seat(struct kmscon_pty *pty, const char *seat);
+int kmscon_pty_set_vtnr(struct kmscon_pty *pty, unsigned int vtnr);
 void kmscon_pty_set_env_reset(struct kmscon_pty *pty, bool do_reset);
 
 int kmscon_pty_get_fd(struct kmscon_pty *pty);