libbb: introduce and use xrename and rename_or_warn.
[oweals/busybox.git] / loginutils / vlock.c
index d472c09251d788311f8ce89b23de43fd0beec7c0..6e928e2391d2098d51f9fbb7e97a65e395534615 100644 (file)
 /* Fixed by Erik Andersen to do passwords the tinylogin way...
  * It now works with md5, sha1, etc passwords. */
 
-#include "busybox.h"
+#include "libbb.h"
 #include <sys/vt.h>
 
-static struct passwd *pw;
-static struct vt_mode ovtm;
-static struct termios oterm;
-static int vfd;
-static unsigned long o_lock_all;
+enum { vfd = 3 };
+
+/* static unsigned long o_lock_all; */
 
 static void release_vt(int signo)
 {
-       if (!o_lock_all)
-               ioctl(vfd, VT_RELDISP, 1);
-       else
-               ioctl(vfd, VT_RELDISP, 0);
+       ioctl(vfd, VT_RELDISP, (unsigned long) !option_mask32 /*!o_lock_all*/);
 }
 
 static void acquire_vt(int signo)
@@ -38,34 +33,32 @@ static void acquire_vt(int signo)
        ioctl(vfd, VT_RELDISP, VT_ACKACQ);
 }
 
-static void restore_terminal(void)
-{
-       ioctl(vfd, VT_SETMODE, &ovtm);
-       tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
-}
-
+int vlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int vlock_main(int argc, char **argv)
 {
        sigset_t sig;
        struct sigaction sa;
        struct vt_mode vtm;
        struct termios term;
+       struct termios oterm;
+       struct vt_mode ovtm;
+       uid_t uid;
+       struct passwd *pw;
+
+       uid = getuid();
+       pw = getpwuid(uid);
+       if (pw == NULL)
+               bb_error_msg_and_die("unknown uid %d", uid);
 
        if (argc > 2) {
                bb_show_usage();
        }
 
-       o_lock_all = bb_getopt_ulflags (argc, argv, "a");
+       /*o_lock_all = */getopt32(argv, "a");
 
-       if((pw = getpwuid(getuid())) == NULL) {
-               bb_error_msg_and_die("Unknown uid %d", getuid());
-       }
-
-       vfd = xopen(CURRENT_TTY, O_RDWR);
-
-       if (ioctl(vfd, VT_GETMODE, &vtm) < 0) {
-               bb_perror_msg_and_die("VT_GETMODE");
-       }
+       /* Avoid using statics - use constant fd */
+       xmove_fd(xopen(CURRENT_TTY, O_RDWR), vfd);
+       xioctl(vfd, VT_GETMODE, &vtm);
 
        /* mask a bunch of signals */
        sigprocmask(SIG_SETMASK, NULL, &sig);
@@ -108,13 +101,17 @@ int vlock_main(int argc, char **argv)
        tcsetattr(STDIN_FILENO, TCSANOW, &term);
 
        do {
-               printf("Virtual Console%s locked by %s.\n", (o_lock_all) ? "s" : "", pw->pw_name);
+               printf("Virtual console%s locked by %s.\n",
+                               option_mask32 /*o_lock_all*/ ? "s" : "",
+                               pw->pw_name);
                if (correct_password(pw)) {
                        break;
                }
                bb_do_delay(FAIL_DELAY);
                puts("Password incorrect");
        } while (1);
-       restore_terminal();
-       return 0;
+
+       ioctl(vfd, VT_SETMODE, &ovtm);
+       tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
+       fflush_stdout_and_exit(0);
 }