From: David Herrmann Date: Thu, 28 Nov 2013 16:05:34 +0000 (+0100) Subject: logind: make VT numbers unsigned X-Git-Tag: v209~1248 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=92bd5ff3a062c3f9475b9d9d39b9335bfeb7705e;hp=92fe133abf2de889659464ae8affd1db1710f72e logind: make VT numbers unsigned 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. --- diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 2aedbcf92..5547cb235 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -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; diff --git a/src/login/logind-core.c b/src/login/logind-core.c index 72ee9fe57..3f8e8139d 100644 --- a/src/login/logind-core.c +++ b/src/login/logind-core.c @@ -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( diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index e0333cd6b..4239b3788 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -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"); diff --git a/src/login/logind-seat.c b/src/login/logind-seat.c index ca0e8d7fc..eac5a5f26 100644 --- a/src/login/logind-seat.c +++ b/src/login/logind-seat.c @@ -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; } diff --git a/src/login/logind-seat.h b/src/login/logind-seat.h index 80c6b8bd9..1254268f1 100644 --- a/src/login/logind-seat.h +++ b/src/login/logind-seat.h @@ -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); diff --git a/src/login/logind-session.c b/src/login/logind-session.c index c12683c60..cd8708845 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -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); } diff --git a/src/login/logind-session.h b/src/login/logind-session.h index aab39b72e..939476af5 100644 --- a/src/login/logind-session.h +++ b/src/login/logind-session.h @@ -90,7 +90,7 @@ struct Session { char *scope_job; Seat *seat; - int vtnr; + unsigned int vtnr; int vtfd; sd_event_source *vt_source; diff --git a/src/login/logind.h b/src/login/logind.h index 814964ff9..b84137c78 100644 --- a/src/login/logind.h +++ b/src/login/logind.h @@ -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); diff --git a/src/login/pam-module.c b/src/login/pam-module.c index be5901fd7..45428a090 100644 --- a/src/login/pam-module.c +++ b/src/login/pam-module.c @@ -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"))