chiark / gitweb /
logind: make VT numbers unsigned
authorDavid Herrmann <dh.herrmann@gmail.com>
Thu, 28 Nov 2013 16:05:34 +0000 (17:05 +0100)
committerDavid Herrmann <dh.herrmann@gmail.com>
Thu, 28 Nov 2013 16:38:16 +0000 (17:38 +0100)
Fix the whole code to use "unsigned int" for vtnr. 0 is an invalid vtnr so
we don't need negative numbers at all.

Note that most code already assumes it's unsigned so in case there's a
negative vtnr, our code may, under special circumstances, silently break.
So this patch makes sure all sources of vtnrs verify the validity. Also
note that the dbus api already uses unsigned ints.

src/login/loginctl.c
src/login/logind-core.c
src/login/logind-dbus.c
src/login/logind-seat.c
src/login/logind-seat.h
src/login/logind-session.c
src/login/logind-session.h
src/login/logind.h
src/login/pam-module.c

index 2aedbcf9225c6cc14458ac7319592b3500f0109a..5547cb235879ead1652cbba2d74db76a6c5b0f13 100644 (file)
@@ -251,7 +251,7 @@ typedef struct SessionStatusInfo {
         uid_t uid;
         const char *name;
         usec_t timestamp;
-        int vtnr;
+        unsigned int vtnr;
         const char *seat;
         const char *tty;
         const char *display;
index 72ee9fe5727769bc1a78a9cd5f3ff578041661f8..3f8e8139da255f02737f95e149ba14911f65edd7 100644 (file)
@@ -436,7 +436,7 @@ bool manager_shall_kill(Manager *m, const char *user) {
         return strv_contains(m->kill_only_users, user);
 }
 
-static int vt_is_busy(int vtnr) {
+static int vt_is_busy(unsigned int vtnr) {
         struct vt_stat vt_stat;
         int r = 0, fd;
 
@@ -462,7 +462,7 @@ static int vt_is_busy(int vtnr) {
         return r;
 }
 
-int manager_spawn_autovt(Manager *m, int vtnr) {
+int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_free_ char *name = NULL;
         int r;
@@ -470,11 +470,11 @@ int manager_spawn_autovt(Manager *m, int vtnr) {
         assert(m);
         assert(vtnr >= 1);
 
-        if ((unsigned) vtnr > m->n_autovts &&
-            (unsigned) vtnr != m->reserve_vt)
+        if (vtnr > m->n_autovts &&
+            vtnr != m->reserve_vt)
                 return 0;
 
-        if ((unsigned) vtnr != m->reserve_vt) {
+        if (vtnr != m->reserve_vt) {
                 /* If this is the reserved TTY, we'll start the getty
                  * on it in any case, but otherwise only if it is not
                  * busy. */
@@ -486,7 +486,7 @@ int manager_spawn_autovt(Manager *m, int vtnr) {
                         return -EBUSY;
         }
 
-        if (asprintf(&name, "autovt@tty%i.service", vtnr) < 0)
+        if (asprintf(&name, "autovt@tty%u.service", vtnr) < 0)
                 return log_oom();
 
         r = sd_bus_call_method(
index e0333cd6b0bc883784551c88eb7a43554e523e22..4239b3788a83bd70b93130a297b7295874071b1e 100644 (file)
@@ -496,7 +496,7 @@ static int method_create_session(sd_bus *bus, sd_bus_message *message, void *use
                 if (v <= 0)
                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
 
-                if (vtnr <= 0)
+                if (!vtnr)
                         vtnr = (uint32_t) v;
                 else if (vtnr != (uint32_t) v)
                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
index ca0e8d7fcfdd0fc2ee2d24938c2845bf5c7596a9..eac5a5f26b144de064d61ea0ea0a9183a52c248f 100644 (file)
@@ -166,13 +166,13 @@ int seat_load(Seat *s) {
         return 0;
 }
 
-static int vt_allocate(int vtnr) {
+static int vt_allocate(unsigned int vtnr) {
         _cleanup_free_ char *p = NULL;
         _cleanup_close_ int fd = -1;
 
         assert(vtnr >= 1);
 
-        if (asprintf(&p, "/dev/tty%i", vtnr) < 0)
+        if (asprintf(&p, "/dev/tty%u", vtnr) < 0)
                 return -ENOMEM;
 
         fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
@@ -270,7 +270,7 @@ int seat_set_active(Seat *s, Session *session) {
         return 0;
 }
 
-int seat_active_vt_changed(Seat *s, int vtnr) {
+int seat_active_vt_changed(Seat *s, unsigned int vtnr) {
         Session *i, *new_active = NULL;
         int r;
 
@@ -280,7 +280,7 @@ int seat_active_vt_changed(Seat *s, int vtnr) {
         if (!seat_has_vts(s))
                 return -EINVAL;
 
-        log_debug("VT changed to %i", vtnr);
+        log_debug("VT changed to %u", vtnr);
 
         LIST_FOREACH(sessions_by_seat, i, s->sessions)
                 if (i->vtnr == vtnr) {
@@ -297,7 +297,8 @@ int seat_active_vt_changed(Seat *s, int vtnr) {
 int seat_read_active_vt(Seat *s) {
         char t[64];
         ssize_t k;
-        int r, vtnr;
+        unsigned int vtnr;
+        int r;
 
         assert(s);
 
@@ -320,13 +321,13 @@ int seat_read_active_vt(Seat *s) {
                 return -EIO;
         }
 
-        r = safe_atoi(t+3, &vtnr);
+        r = safe_atou(t+3, &vtnr);
         if (r < 0) {
                 log_error("Failed to parse VT number %s", t+3);
                 return r;
         }
 
-        if (vtnr <= 0) {
+        if (!vtnr) {
                 log_error("VT number invalid: %s", t+3);
                 return -EIO;
         }
index 80c6b8bd92f8343062ff928a4d7e2e120dbfe36d..1254268f1087f64974eb7f32216d53c353833c7b 100644 (file)
@@ -55,7 +55,7 @@ int seat_load(Seat *s);
 
 int seat_apply_acls(Seat *s, Session *old_active);
 int seat_set_active(Seat *s, Session *session);
-int seat_active_vt_changed(Seat *s, int vtnr);
+int seat_active_vt_changed(Seat *s, unsigned int vtnr);
 int seat_read_active_vt(Seat *s);
 int seat_preallocate_vts(Seat *s);
 
index c12683c60c1cd77e04413645f93234308c9ef3c0..cd87088456782b07d2b72756ca83a882d03277f4 100644 (file)
@@ -229,7 +229,7 @@ int session_save(Session *s) {
                 fprintf(f, "SERVICE=%s\n", s->service);
 
         if (s->seat && seat_has_vts(s->seat))
-                fprintf(f, "VTNR=%i\n", s->vtnr);
+                fprintf(f, "VTNR=%u\n", s->vtnr);
 
         if (s->leader > 0)
                 fprintf(f, "LEADER=%lu\n", (unsigned long) s->leader);
@@ -343,9 +343,9 @@ int session_load(Session *s) {
         }
 
         if (vtnr && s->seat && seat_has_vts(s->seat)) {
-                int v;
+                unsigned int v;
 
-                k = safe_atoi(vtnr, &v);
+                k = safe_atou(vtnr, &v);
                 if (k >= 0 && v >= 1)
                         s->vtnr = v;
         }
@@ -421,7 +421,7 @@ int session_activate(Session *s) {
 
         /* on seats with VTs, we let VTs manage session-switching */
         if (seat_has_vts(s->seat)) {
-                if (s->vtnr <= 0)
+                if (!s->vtnr)
                         return -ENOTSUP;
 
                 return chvt(s->vtnr);
@@ -981,13 +981,13 @@ int session_kill(Session *s, KillWho who, int signo) {
 static int session_open_vt(Session *s) {
         char path[128];
 
-        if (s->vtnr <= 0)
+        if (!s->vtnr)
                 return -1;
 
         if (s->vtfd >= 0)
                 return s->vtfd;
 
-        sprintf(path, "/dev/tty%d", s->vtnr);
+        sprintf(path, "/dev/tty%u", s->vtnr);
         s->vtfd = open(path, O_RDWR | O_CLOEXEC | O_NONBLOCK | O_NOCTTY);
         if (s->vtfd < 0) {
                 log_error("cannot open VT %s of session %s: %m", path, s->id);
@@ -1044,7 +1044,7 @@ void session_mute_vt(Session *s) {
         return;
 
 error:
-        log_error("cannot mute VT %d for session %s (%d/%d)", s->vtnr, s->id, r, errno);
+        log_error("cannot mute VT %u for session %s (%d/%d)", s->vtnr, s->id, r, errno);
         session_restore_vt(s);
 }
 
index aab39b72e3594365b6675af09c550137bc6806f0..939476af557d8ac7c732067458164d11f4fcad30 100644 (file)
@@ -90,7 +90,7 @@ struct Session {
         char *scope_job;
 
         Seat *seat;
-        int vtnr;
+        unsigned int vtnr;
         int vtfd;
         sd_event_source *vt_source;
 
index 814964ff9d5dfdb44ec67574b8bb3fca2516761d..b84137c78b0587c8dd4dfc805c310ee87fdf4c1d 100644 (file)
@@ -137,7 +137,7 @@ int manager_process_button_device(Manager *m, struct udev_device *d);
 
 int manager_startup(Manager *m);
 int manager_run(Manager *m);
-int manager_spawn_autovt(Manager *m, int vtnr);
+int manager_spawn_autovt(Manager *m, unsigned int vtnr);
 
 void manager_gc(Manager *m, bool drop_not_started);
 
index be5901fd77355e4040b2921bd0f7c1953a28994f..45428a090f40e767bbd500350cc20f50efeab309 100644 (file)
@@ -284,7 +284,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
         if (!isempty(cvtnr))
                 safe_atou32(cvtnr, &vtnr);
 
-        if (!isempty(display) && vtnr <= 0) {
+        if (!isempty(display) && !vtnr) {
                 if (isempty(seat))
                         get_seat_from_display(display, &seat, &vtnr);
                 else if (streq(seat, "seat0"))