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.
uid_t uid;
const char *name;
usec_t timestamp;
uid_t uid;
const char *name;
usec_t timestamp;
const char *seat;
const char *tty;
const char *display;
const char *seat;
const char *tty;
const char *display;
return strv_contains(m->kill_only_users, 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;
struct vt_stat vt_stat;
int r = 0, fd;
-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;
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_free_ char *name = NULL;
int r;
assert(m);
assert(vtnr >= 1);
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)
- 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. */
/* If this is the reserved TTY, we'll start the getty
* on it in any case, but otherwise only if it is not
* busy. */
- 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(
return log_oom();
r = sd_bus_call_method(
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 (v <= 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
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");
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");
-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);
_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);
return -ENOMEM;
fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
-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;
Session *i, *new_active = NULL;
int r;
if (!seat_has_vts(s))
return -EINVAL;
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) {
LIST_FOREACH(sessions_by_seat, i, s->sessions)
if (i->vtnr == vtnr) {
int seat_read_active_vt(Seat *s) {
char t[64];
ssize_t k;
int seat_read_active_vt(Seat *s) {
char t[64];
ssize_t k;
+ unsigned int vtnr;
+ int r;
- 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 (r < 0) {
log_error("Failed to parse VT number %s", t+3);
return r;
}
log_error("VT number invalid: %s", t+3);
return -EIO;
}
log_error("VT number invalid: %s", t+3);
return -EIO;
}
int seat_apply_acls(Seat *s, Session *old_active);
int seat_set_active(Seat *s, Session *session);
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);
int seat_read_active_vt(Seat *s);
int seat_preallocate_vts(Seat *s);
fprintf(f, "SERVICE=%s\n", s->service);
if (s->seat && seat_has_vts(s->seat))
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);
if (s->leader > 0)
fprintf(f, "LEADER=%lu\n", (unsigned long) s->leader);
}
if (vtnr && s->seat && seat_has_vts(s->seat)) {
}
if (vtnr && s->seat && seat_has_vts(s->seat)) {
- k = safe_atoi(vtnr, &v);
+ k = safe_atou(vtnr, &v);
if (k >= 0 && v >= 1)
s->vtnr = v;
}
if (k >= 0 && v >= 1)
s->vtnr = v;
}
/* on seats with VTs, we let VTs manage session-switching */
if (seat_has_vts(s->seat)) {
/* on seats with VTs, we let VTs manage session-switching */
if (seat_has_vts(s->seat)) {
return -ENOTSUP;
return chvt(s->vtnr);
return -ENOTSUP;
return chvt(s->vtnr);
static int session_open_vt(Session *s) {
char path[128];
static int session_open_vt(Session *s) {
char path[128];
return -1;
if (s->vtfd >= 0)
return s->vtfd;
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);
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);
- 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);
char *scope_job;
Seat *seat;
char *scope_job;
Seat *seat;
int vtfd;
sd_event_source *vt_source;
int vtfd;
sd_event_source *vt_source;
int manager_startup(Manager *m);
int manager_run(Manager *m);
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);
void manager_gc(Manager *m, bool drop_not_started);
if (!isempty(cvtnr))
safe_atou32(cvtnr, &vtnr);
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"))
if (isempty(seat))
get_seat_from_display(display, &seat, &vtnr);
else if (streq(seat, "seat0"))