]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
login: tty chown.chmod refactoring
authorKarel Zak <kzak@redhat.com>
Fri, 26 Aug 2011 11:25:13 +0000 (13:25 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 26 Oct 2011 21:17:16 +0000 (23:17 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
login-utils/login.c

index 15c5feeb315cdaf09746dd8343b4286ec3b6a9bd..3e9fd4c371fbb81c214342786f2412f23b0fb995 100644 (file)
@@ -158,6 +158,43 @@ static void open_tty(const char *tty)
                close(fd);
 }
 
+#define chown_err(_what, _uid, _gid) \
+               syslog(LOG_ERR, _("chown (%s, %lu, %lu) failed: %m"), \
+                       (_what), (unsigned long) (_uid), (unsigned long) (_gid))
+
+#define chmod_err(_what, _mode) \
+               syslog(LOG_ERR, _("chmod (%s, %u) failed: %m"), (_what), (_mode))
+
+static void chown_tty(struct login_context *cxt)
+{
+       struct group *gr;
+       uid_t uid = cxt->pwd->pw_uid;
+       gid_t gid = cxt->pwd->pw_gid;
+
+       gr = getgrnam(TTYGRPNAME);
+       if (gr)
+               gid = gr->gr_gid;
+
+       if (fchown(0, uid, gid))                                /* tty */
+               chown_err(cxt->tty_name, uid, gid);
+       if (fchmod(0, TTY_MODE))
+               chmod_err(cxt->tty_name, TTY_MODE);
+
+#ifdef LOGIN_CHOWN_VCS
+       if (is_consoletty(0)) {
+               if (chown(cxt->vcs, uid, gid))                  /* vcs */
+                       chown_err(cxt->vcs, uid, gid);
+               if (chmod(cxt->vcs, TTY_MODE))
+                       chmod_err(cxt->vcs, TTY_MODE);
+
+               if (chown(cxt->vcsa, uid, gid))                 /* vcsa */
+                       chown_err(cxt->vcsa, uid, gid);
+               if (chmod(cxt->vcsa, TTY_MODE))
+                       chmod_err(cxt->vcsa, TTY_MODE);
+       }
+#endif
+}
+
 /*
  * Reads the currect terminal path and initialize cxt->tty_* variables.
  */
@@ -541,7 +578,6 @@ int main(int argc, char **argv)
 {
        extern int optind;
        extern char *optarg, **environ;
-       struct group *gr;
        register int ch;
        register char *p;
        int fflag, hflag, pflag, cnt;
@@ -872,26 +908,8 @@ int main(int argc, char **argv)
        log_audit(&cxt, 1);
        log_lastlog(&cxt);
 
-       if (fchown(0, pwd->pw_uid,
-                  (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid))
-               warn(_("change terminal owner failed"));
-
-       fchmod(0, TTY_MODE);
-
-#ifdef LOGIN_CHOWN_VCS
-       /* if tty is one of the VC's then change owner and mode of the
-          special /dev/vcs devices as well */
-       if (is_consoletty(0)) {
-
-               if (chown(vcsn, pwd->pw_uid, (gr ? gr->gr_gid : pwd->pw_gid)))
-                       warn(_("change terminal owner failed"));
-               if (chown(vcsan, pwd->pw_uid, (gr ? gr->gr_gid : pwd->pw_gid)))
-                       warn(_("change terminal owner failed"));
+       chown_tty(&cxt);
 
-               chmod(vcsn, TTY_MODE);
-               chmod(vcsan, TTY_MODE);
-       }
-#endif
 
        if (setgid(pwd->pw_gid) < 0 && pwd->pw_gid) {
                syslog(LOG_ALERT, _("setgid() failed"));