# HG changeset patch
[vlock.git] / vlock.c
blob04d7b4228083e5741881a460c06e7779f67bbb55
1 /* vlock.c -- main routine for vlock, the VT locking program for linux
3 * This program is copyright (C) 1994 Michael K. Johnson, and is free
4 * software which is freely distributable under the terms of the
5 * GNU public license, included as the file COPYING in this
6 * distribution. It is NOT public domain software, and any
7 * redistribution not permitted by the GNU Public License is
8 * expressly forbidden without prior written permission from
9 * the author.
13 /* RCS log:
14 * $Log: vlock.c,v $
15 * Revision 1.11 1994/07/03 12:41:43 johnsonm
16 * Removed emacs variables.
18 * Revision 1.10 1994/07/03 12:15:22 johnsonm
19 * *** empty log message ***
21 * Revision 1.9 1994/03/23 17:00:47 johnsonm
22 * Removed appendages for patterns.
23 * Added support for non-VT ttys.
25 * Revision 1.8 1994/03/20 11:21:20 johnsonm
26 * Now check /dev/console after opening it to make sure the call succeeded
28 * Revision 1.7 1994/03/19 17:54:42 johnsonm
29 * Moved printing the lock message to get_password. Added a return
30 * code again.
32 * Revision 1.6 1994/03/19 14:36:08 johnsonm
33 * Made a better explanation for when the --all or -a flag is chosen.
35 * Revision 1.5 1994/03/19 14:25:16 johnsonm
36 * Removed silly two-process model. Must have been half asleep when
37 * I came up with that idea. vlock works now.
39 * Revision 1.4 1994/03/16 20:14:06 johnsonm
40 * Cleaned up, putting most real work into child functions, which
41 * cleaned up the interface. The whole program is almost all
42 * working now.
44 * Revision 1.3 1994/03/15 18:27:33 johnsonm
45 * Moved terminal handling stuff into terminal.c, and changed the
46 * end to support a two-process model for async I/O.
48 * Revision 1.2 1994/03/13 17:28:56 johnsonm
49 * Now using SIGUSR{1,2} correctly to announce VC switches. Fixed a few
50 * other minor bugs.
52 * Revision 1.1 1994/03/13 16:28:16 johnsonm
53 * Initial revision
58 #include <stdio.h>
59 #include <unistd.h>
60 #include <fcntl.h>
61 #include <getopt.h>
62 #include <termios.h>
63 #include <signal.h>
64 #include <sys/vt.h>
65 #include <sys/kd.h>
66 #include <sys/ioctl.h>
67 #include "vlock.h"
68 #include "version.h"
71 static char rcsid[] = "$Id: vlock.c,v 1.12 1996/05/17 02:52:03 johnsonm Exp $";
73 /* Option globals */
74 /* This determines whether the default behavior is to lock only the */
75 /* current VT or all of them. 0 means current, 1 means all. */
76 int o_lock_all = 0;
78 /* Other globals */
79 struct vt_mode ovtm;
80 struct termios oterm;
81 int vfd;
82 int is_vt;
84 int main(int argc, char **argv) {
86 static struct option long_options[] = { /* For parsing long arguments */
87 {"current", 0, &o_lock_all, 0},
88 {"all", 0, &o_lock_all, 1},
89 {"version", no_argument, 0, O_VERSION},
90 {"help", no_argument, 0, O_HELP},
91 {0, 0, 0, 0},
93 int option_index; /* Unused */
94 int c;
95 struct vt_mode vtm;
97 /* First we parse all the command line arguments */
98 while ((c = getopt_long(argc, argv, "acvh",
99 long_options, &option_index)) != -1) {
100 switch(c) {
101 case 'c':
102 o_lock_all = 0;
103 break;
104 case 'a':
105 o_lock_all = 1;
106 break;
107 case 'v':
108 case O_VERSION:
109 fprintf(stderr, VERSION);
110 exit(0);
111 break;
112 case 'h':
113 case O_HELP:
114 print_help(0);
115 break;
116 case '?':
117 print_help(1);
118 break;
122 /* Now we have parsed the options, and can get on with life */
124 /* Get the user's and root's encrypted passwords. This needs
125 to run as root when using shadow passwords, but will drop
126 root privileges as soon as they are no longer needed. */
127 init_passwords();
129 if ((vfd = open("/dev/tty", O_RDWR)) < 0) {
130 perror("vlock: could not open /dev/tty");
131 exit (1);
134 /* First we will set process control of VC switching; if this fails, */
135 /* then we know that we aren't on a VC, and will print a warning message */
136 /* If it doesn't fail, it gets the current VT status... */
137 c = ioctl(vfd, VT_GETMODE, &vtm);
138 if (c < 0) {
139 fprintf(stderr, " *** This tty is not a VC (virtual console). ***\n"
140 " *** It may not be securely locked. ***\n\n");
141 is_vt = 0;
142 o_lock_all = 0;
143 } else {
144 is_vt = 1;
147 /* Now set the signals so we can't be summarily executed or stopped, */
148 /* and handle SIGUSR{1,2} and SIGCHLD */
149 mask_signals();
151 if (is_vt) {
152 ovtm = vtm; /* Keep a copy around to restore at appropriate times */
153 vtm.mode = VT_PROCESS;
154 vtm.relsig = SIGUSR1; /* handled by release_vt() */
155 vtm.acqsig = SIGUSR2; /* handled by acquire_vt() */
156 ioctl(vfd, VT_SETMODE, &vtm);
159 /* get_password() sets the terminal characteristics and does not */
160 /* return until the correct password has been read. */
161 get_password();
163 /* vt status was restored in restore_terminal() already... */
165 /* we should really return something... */
166 return (0);