This repository has been archived by the owner on Mar 12, 2023. It is now read-only.
/
dm.c
1387 lines (1212 loc) · 32.8 KB
/
dm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: dm.c,v 1.31 2005-05-23 21:39:42 rbasch Exp $
*
* Copyright (c) 1990, 1991 by the Massachusetts Institute of Technology
* For copying and distribution information, please see the file
* <mit-copyright.h>.
*
* This is the top-level of the display manager and console control
* for Athena's xlogin.
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <time.h>
#ifdef HAVE_SYS_STRREDIR_H
#include <sys/strredir.h>
#endif
#ifdef HAVE_SYS_STROPTS_H
#include <sys/stropts.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
/* If we have getutxent(), just use that. Otherwise, try to guess where
* our utmp file is.
*/
#ifdef HAVE_GETUTXENT
#include <utmpx.h>
#if defined(WTMPX_FILE)
#define WTFILE WTMPX_FILE
#else
#define WTFILE "/var/adm/wtmpx"
#endif
#else /* HAVE_GETUTXENT */
#include <utmp.h>
#if defined(_PATH_UTMP)
#define UTFILE _PATH_UTMP
#define WTFILE _PATH_WTMP
#elif defined(UTMP_FILE)
#define UTFILE UTMP_FILE
#define WTFILE WTMP_FILE
#else
#define UTFILE "/var/adm/utmp"
#define UTFILE "/var/adm/wtmp"
#endif
#endif /* HAVE_GETUTXENT */
#include <termios.h>
#include <syslog.h>
#ifdef HAVE_UTIL_H
#include <util.h>
#endif
#ifdef HAVE_PTY_H
#include <pty.h>
#endif
#ifdef HAVE_LOGIN_TTY
#include <utmp.h>
#endif
#include <X11/Xlib.h>
#include <X11/Xauth.h>
#include <al.h>
#ifndef lint
static const char rcsid[] = "$Id: dm.c,v 1.31 2005-05-23 21:39:42 rbasch Exp $";
#endif
/* Process states */
#define NONEXISTENT 0
#define RUNNING 1
#define STARTUP 2
#define CONSOLELOGIN 3
#define FAILED 4
#ifndef FALSE
#define FALSE 0
#define TRUE (!FALSE)
#endif
static sigset_t sig_zero;
static sigset_t sig_cur;
/* flags used by signal handlers */
pid_t xpid, consolepid, loginpid;
volatile int alarm_running = NONEXISTENT;
volatile int x_running = NONEXISTENT;
volatile int console_running = NONEXISTENT;
volatile int console_failed = FALSE;
volatile int login_running = NONEXISTENT;
char dpyname[10];
char *utmp_line = NULL;
int console_master_fd = -1;
int console_slave_fd = -1;
char *xpids = "/var/athena/X%d.pid";
char *xhosts = "/etc/X%d.hosts";
char *consolepidf = "/var/athena/console.pid";
char *dmpidf = "/var/athena/dm.pid";
char *consolelog = "/var/athena/console.log";
static void die(int signo);
static void child(int signo);
static void catchalarm(int signo);
static void xready(int signo);
static void shutdown(int signo);
static void loginready(int signo);
static char *getconf(char *file, char *name);
static char **parseargs(char *line, char *extra, char *extra1, char *extra2);
static void console_login(char *conf, char *msg);
static void start_console(int master_fd, int aux_fd, char **argv);
static void cleanup(char *line);
static pid_t fork_and_store(pid_t *var);
static void x_stop_wait(void);
static void writepid(char *file, pid_t pid);
#ifndef HAVE_LOGOUT
static void logout(const char *line);
#endif
#ifndef HAVE_LOGIN_TTY
static int login_tty(int fd);
#endif
#ifndef HAVE_OPENPTY
static int openpty(int *amaster, int *aslave, char *name,
struct termios *termp, struct winsize *winp);
static int termsetup(int fd, struct termios *termp, struct winsize *winp);
#endif
static int handle_xioerror(Display *dpy);
/* the console process will run as daemon */
#define DAEMON 1
#define X_START_WAIT 30 /* wait up to 30 seconds for X to be ready */
#define LOGIN_START_WAIT 60 /* wait up to 1 minute for Xlogin */
#ifndef BUFSIZ
#define BUFSIZ 1024
#endif
static int max_fd;
static char *conf;
/* Setup signals, start X, start console, start login, wait */
int main(int argc, char **argv)
{
char *consoletty, *p;
char **dmargv, **xargv, **consoleargv = NULL, **loginargv;
char xpidf[256], line[16], buf[256];
fd_set readfds;
int pgrp, file, tries, count, redir = TRUE;
char dpyacl[40];
Display *dpy;
XHostAddress *hosts, localhost;
int nhosts, dpynum = 0;
struct stat hostsinfo;
Bool state;
time_t now, last_console_failure = 0;
struct sigaction sigact;
sigset_t mask;
#if defined(SRIOCSREDIR) || defined(TIOCCONS)
int on;
#endif
int fd;
int conspipe[2];
XIOErrorHandler xioerror_handler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
(void) sigemptyset(&sig_zero);
/* Create a localhost entity for access control purposes. */
localhost.family = FamilyLocalHost;
localhost.length = 0;
localhost.address = "";
/*
* Note about setting environment variables in dm:
*
* All environment variables passed to dm and set in dm are
* subsequently passed to any children of dm. This is usually
* true of processes that exec in children, so that's not a
* big surprise.
*
* However, xlogin is one of the children dm forks, and it goes
* to lengths to ensure that the environments of users logging in
* are ISOLATED from xlogin's own environment. Therefore, do not
* expect that setting an environment variable here will reach the
* user unless you have gone to lengths to make sure that xlogin
* passes it on. Put another way, if you set a new environment
* variable here, consider whether or not it should be seen by the
* user. If it should, go modify verify.c as well. Consider also
* whether the variable should be seen _only_ by the user. If so,
* make the change only in xlogin, and not here.
*
* As an added complication, xlogin _does_ pass environment variables
* on to the pre-login options. Therefore, if you set an environment
* variable that should _not_ be seen, you must filter it in xlogin.c.
*
* Confused? Too bad. I'm in a nasty, if verbose, mood this year.
*
* General summary:
*
* If you add an environment variable here there are three likely
* possibilities:
*
* 1. It's for the user only, not needed by any of dm's children.
* --> Don't set it here. Set it in verify.c for users and in
* --> xlogin.c for the pre-login options, if appropriate.
*
* 2. It's for dm and its children only, and _should not_ be seen
* by the user or pre-login options.
* --> You must filter the option from the pre-login options
* --> in xlogin.c. No changes to verify.c are required.
*
* 3. It's for dm and the user and the pre-login options.
* --> You must pass the option explicitly to the user in
* --> verify.c. No changes to xlogin.c are required.
*
* --- cfields
*/
#ifdef notdef
putenv("LD_LIBRARY_PATH=/usr/openwin/lib");
putenv("OPENWINHOME=/usr/openwin");
#endif
if (argc < 2)
{
fprintf(stderr, "dm: first argument must be configuration file\n");
sleep(60);
exit(1);
}
conf = argv[1];
if (argc != 4 && (argc != 5 || strcmp(argv[3], "-noconsole")))
{
fprintf(stderr,
"usage: %s configfile logintty [-noconsole] consoletty\n",
argv[0]);
console_login(conf, NULL);
}
if (argc == 5)
redir = FALSE;
/* parse argument lists */
/* ignore argv[2] */
consoletty = argv[argc - 1];
#ifdef SOLARIS
/* On Solaris, use the console tty name for the utmp line field,
* as the Solaris finger requires an actual device name there.
* Elsewhere, we will use the display name (see below).
*/
utmp_line = consoletty;
#endif
openlog("dm", 0, LOG_USER);
/* We use options from the config file rather than taking
* them from the command line because the current command
* line form is gross (why???), and I don't see a good way
* to extend it without making things grosser or breaking
* backwards compatibility. So, we take a line from the
* config file and use real parsing.
*/
p = getconf(conf, "dm");
if (p != NULL)
{
dmargv = parseargs(p, NULL, NULL, NULL);
while (*dmargv)
{
if (!strcmp(*dmargv, "-display"))
{
dmargv++;
if (*dmargv)
{
dpynum = atoi(*(dmargv) + 1);
dmargv++;
}
}
else
dmargv++;
}
}
p = getconf(conf, "X");
if (p == NULL)
console_login(conf, "\ndm: Can't find X command line\n");
xargv = parseargs(p, NULL, NULL, NULL);
p = getconf(conf, "console");
if (p == NULL)
console_login(conf, "\ndm: Can't find console command line\n");
/* We will pass the read side of the pipe created below to console
* on descriptor 3.
*/
consoleargv = parseargs(p, "-inputfd", "3", NULL);
/* Signal Setup */
sigact.sa_handler = SIG_IGN;
sigaction(SIGTSTP, &sigact, NULL);
sigaction(SIGTTIN, &sigact, NULL);
sigaction(SIGTTOU, &sigact, NULL);
/* so that X pipe errors don't nuke us */
sigaction(SIGPIPE, &sigact, NULL);
sigact.sa_handler = shutdown;
sigaction(SIGFPE, &sigact, NULL);
sigact.sa_handler = die;
sigaction(SIGHUP, &sigact, NULL);
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigact.sa_handler = child;
sigaction(SIGCHLD, &sigact, NULL);
sigact.sa_handler = catchalarm;
sigaction(SIGALRM, &sigact, NULL);
strcpy(line, "/dev/");
strcat(line, consoletty);
fd = open(line, O_RDWR);
if (fd == -1)
{
syslog(LOG_ERR, "Cannot open %s: %m", line);
/* This probably won't work, but it seems to be the appropriate
punt location. */
console_login(conf, "Cannot open tty.\n");
}
if (login_tty(fd) == -1)
syslog(LOG_ERR, "Cannot set the console as a login terminal (%s)",
strerror(errno));
else
{
/* Set the console characteristics so we don't lose later */
setpgid(0, pgrp = getpid()); /* Reset the tty pgrp */
if (tcsetpgrp(0, pgrp) == -1)
syslog(LOG_ERR, "tcsetpgrp failed for console terminal (%s)",
strerror(errno));
}
/* save our pid file */
writepid(dmpidf, getpid());
/* Fire up X */
xpid = 0;
for (tries = 0; tries < 3; tries++)
{
syslog(LOG_DEBUG, "Starting X, try #%d", tries + 1);
x_running = STARTUP;
sigact.sa_handler = xready;
sigaction(SIGUSR1, &sigact, NULL);
switch (fork_and_store(&xpid))
{
case 0:
if (fcntl(2, F_SETFD, 1) == -1)
close(2);
(void) sigprocmask(SIG_SETMASK, &sig_zero, (sigset_t *) 0);
/* ignoring SIGUSR1 will cause the server to send us a SIGUSR1
* when it is ready to accept connections
*/
sigact.sa_handler = SIG_IGN;
sigaction(SIGUSR1, &sigact, NULL);
p = *xargv;
*xargv = "X";
execv(p, xargv);
fprintf(stderr, "dm: X server failed exec: %s\n", strerror(errno));
_exit(1);
case -1:
fprintf(stderr, "dm: Unable to fork to start X server: %s\n",
strerror(errno));
break;
default:
sprintf(xpidf, xpids, dpynum);
writepid(xpidf, xpid);
if (x_running == STARTUP)
{
alarm(X_START_WAIT);
alarm_running = RUNNING;
sigsuspend(&sig_zero);
}
if (x_running != RUNNING)
{
syslog(LOG_DEBUG, "X failed to start; alarm_running=%d",
alarm_running);
if (alarm_running == NONEXISTENT)
fprintf(stderr, "dm: Unable to start X\n");
else
fprintf(stderr, "dm: X failed to become ready\n");
/* If X wouldn't run, it could be that an existing X
* process hasn't shut down. Wait X_STOP_WAIT seconds
* for that to happen.
*/
x_stop_wait();
}
sigact.sa_handler = SIG_IGN;
sigaction(SIGUSR1, &sigact, NULL);
}
if (x_running == RUNNING)
break;
}
alarm(0);
if (x_running != RUNNING)
{
syslog(LOG_DEBUG, "Giving up on starting X.");
console_login(conf, "\nUnable to start X, doing console login "
"instead.\n");
}
/* Tighten up security a little bit. Remove all hosts from X's
* access control list, assuming /etc/X0.hosts does not exist or
* has zero length. If it does exist with nonzero length, this
* behavior is not wanted. The desired effect of removing all hosts
* is that only connections from the Unix domain socket will be
* allowed.
* More secure code using Xau also exists, but there wasn't
* time to completely flesh it out and resolve a couple of
* issues. This code is probably good enough, but we'll see.
* Maybe next time.
* This code has the added benefit of leaving an X display
* connection open, owned by dm. This provides a less-hacky
* solution to the config_console problem, where if config_console
* is the first program run on user login, it causes the only
* X app running at the time, console, to exit, thus resetting
* the X server. Thus this code also allows the removal of the
* hack in xlogin that attempts to solve the same problem, but
* fails on the RS/6000 for reasons unexplored.
* P.S. Don't run this code under Solaris 2.2- (2.3 is safe).
* Removing all hosts from the acl on that server results in
* no connections, not even from the Unix domain socket, being
* allowed. --- cfields
*/
sprintf(dpyacl, xhosts, dpynum);
sprintf(dpyname, ":%d", dpynum);
#ifndef SOLARIS
/* Use the display name for the utmp line field, except on Solaris. */
utmp_line = dpyname;
#endif
/* Put in our own error handler, open the display, then reset the handler. */
xioerror_handler = XSetIOErrorHandler(handle_xioerror);
dpy = XOpenDisplay(dpyname);
XSetIOErrorHandler(xioerror_handler);
if (dpy != NULL && (stat(dpyacl, &hostsinfo) || hostsinfo.st_size == 0))
{
hosts = XListHosts(dpy, &nhosts, &state);
if (hosts != NULL)
{
XRemoveHosts(dpy, hosts, nhosts);
XFree(hosts);
}
XAddHost(dpy, &localhost);
XFlush(dpy);
}
/* else if (dpy == NULL)
* Could've sworn the X server was running now.
* Follow the original code path. No need introducing new bugs
* to this hairy code, just preserve the old behavior as though
* this code had never been added.
*/
/* set up the console pty */
if (openpty(&console_master_fd, &console_slave_fd, NULL, NULL, NULL) == -1)
console_login(conf, "Cannot allocate pseudo-terminal\n");
if (redir)
{
/* Redirect /dev/console output to the pty slave. */
#ifdef SRIOCSREDIR
on = open("/dev/console", O_RDONLY);
if (on >= 0)
{
ioctl(on, SRIOCSREDIR, console_slave_fd);
close(on);
}
#else
#ifdef TIOCCONS
on = 1;
ioctl(console_slave_fd, TIOCCONS, &on);
#endif
#endif
}
/* Set up the console pipe. */
if (pipe(conspipe) == -1)
console_login(conf, "Cannot create pipe for console\n");
/* start up console */
start_console(console_master_fd, conspipe[0], consoleargv);
/* Set up to invoke xlogin. */
p = getconf(conf, "login");
if (p == NULL)
console_login(conf, "\ndm: Can't find login command line\n");
loginargv = parseargs(p, "-line", utmp_line, NULL);
/* Fire up the X login */
for (tries = 0; tries < 3; tries++)
{
syslog(LOG_DEBUG, "Starting xlogin, try #%d", tries + 1);
login_running = STARTUP;
sigact.sa_handler = loginready;
sigaction(SIGUSR1, &sigact, NULL);
switch (fork_and_store(&loginpid))
{
case 0:
max_fd = sysconf(_SC_OPEN_MAX);
for (file = 3; file < max_fd; file++)
{
if (file != conspipe[1])
close(file);
}
setsid();
file = open("/dev/null", O_RDONLY);
if (file >= 0)
{
dup2(file, 0);
if (file != 0)
close(file);
}
file = conspipe[1];
if (file == -1)
file = open("/dev/null", O_WRONLY);
if (file >= 0)
{
if (file != 1)
dup2(file, 1);
if (file != 2)
dup2(file, 2);
if (file != 1 && file != 2)
close(file);
}
(void) sigprocmask(SIG_SETMASK, &sig_zero, (sigset_t *) 0);
/* ignoring SIGUSR1 will cause xlogin to send us a SIGUSR1
* when it is ready
*/
sigact.sa_handler = SIG_IGN;
sigaction(SIGUSR1, &sigact, NULL);
/* dm ignores sigpipe; because of this, all of the children (ie, */
/* the entire session) inherit this unless we fix it now */
sigact.sa_handler = SIG_DFL;
sigaction(SIGPIPE, &sigact, NULL);
execv(loginargv[0], loginargv);
fprintf(stderr, "dm: X login failed exec: %s\n", strerror(errno));
_exit(1);
case -1:
fprintf(stderr, "dm: Unable to fork to start X login: %s\n",
strerror(errno));
break;
default:
alarm(LOGIN_START_WAIT);
alarm_running = RUNNING;
while (login_running == STARTUP && alarm_running == RUNNING)
sigsuspend(&sig_zero);
if (login_running != RUNNING)
{
syslog(LOG_DEBUG, "xlogin failed to start; alarm_running=%d",
alarm_running);
kill(loginpid, SIGKILL);
if (alarm_running != NONEXISTENT)
fprintf(stderr, "dm: Unable to start Xlogin\n");
else
fprintf(stderr, "dm: Xlogin failed to become ready\n");
}
}
if (login_running == RUNNING)
break;
}
sigact.sa_handler = SIG_IGN;
sigaction(SIGUSR1, &sigact, NULL);
alarm(0);
if (login_running != RUNNING)
{
syslog(LOG_DEBUG, "Giving up on starting xlogin.");
console_login(conf, "\nUnable to start xlogin, doing console login "
"instead.\n");
}
/* main loop. Wait for SIGCHLD, waking up every minute anyway. */
(void) sigemptyset(&sig_cur);
(void) sigaddset(&sig_cur, SIGCHLD);
(void) sigprocmask(SIG_BLOCK, &sig_cur, NULL);
while (1)
{
/* Wait for something to hapen */
if (console_failed)
{
/* if no console is running, we must copy bits from the console
* (master side of pty) to the real console to appear as black
* bar messages.
*/
FD_ZERO(&readfds);
FD_SET(console_master_fd, &readfds);
(void) sigprocmask(SIG_SETMASK, &sig_zero, &mask);
count = select(console_master_fd + 1, &readfds, NULL, NULL, NULL);
(void) sigprocmask(SIG_BLOCK, &mask, NULL);
if (count > 0 && FD_ISSET(console_master_fd, &readfds))
{
file = read(console_master_fd, buf, sizeof(buf));
if (file != -1)
write(1, buf, file);
}
}
else
{
alarm(60);
sigsuspend(&sig_zero);
}
if (login_running == STARTUP)
{
(void) sigprocmask(SIG_SETMASK, &sig_zero, NULL);
console_login(conf, "\nConsole login requested.\n");
}
if (console_running == FAILED)
{
console_running = NONEXISTENT;
time(&now);
if (now - last_console_failure <= 3)
{
/* Give up on console. Set the console characteristics so
* we don't lose later. */
syslog(LOG_ERR, "Giving up on the console");
setpgid(0, pgrp = getpid()); /* Reset the tty pgrp */
tcsetpgrp(0, pgrp);
console_failed = TRUE;
}
else
last_console_failure = now;
}
if (console_running == NONEXISTENT && !console_failed)
start_console(console_master_fd, conspipe[0], consoleargv);
if (login_running == NONEXISTENT || x_running == NONEXISTENT)
{
syslog(LOG_DEBUG, "login_running=%d, x_running=%d, quitting",
login_running, x_running);
(void) sigprocmask(SIG_SETMASK, &sig_zero, NULL);
cleanup(utmp_line);
_exit(0);
}
}
}
/* Start a login on the raw console */
static void console_login(char *conf, char *msg)
{
int i, cfirst = TRUE;
char *nl = "\r\n";
struct termios ttybuf;
char *p, **cargv;
int fd;
syslog(LOG_DEBUG, "Performing console login: %s", msg);
sigemptyset(&sig_zero);
if (login_running != NONEXISTENT && login_running != STARTUP)
kill(loginpid, SIGKILL);
if (console_running != NONEXISTENT)
{
if (cfirst)
kill(consolepid, SIGHUP);
else
kill(consolepid, SIGKILL);
cfirst = FALSE;
}
if (x_running != NONEXISTENT)
kill(xpid, SIGTERM);
x_stop_wait();
unlink(dmpidf);
p = getconf(conf, "ttylogin");
if (p == NULL)
{
fprintf(stderr, "dm: Can't find login command line\n");
exit(1);
}
cargv = parseargs(p, NULL, NULL, NULL);
setpgid(0, 0); /* We have to reset the tty pgrp */
if (tcsetpgrp(0, getpgrp()) == -1)
syslog(LOG_ERR, "tcsetpgrp failed in console login (%s)",
strerror(errno));
tcflush(0, TCIOFLUSH);
(void) tcgetattr(0, &ttybuf);
ttybuf.c_lflag |= (ICANON | ISIG | ECHO);
(void) tcsetattr(0, TCSADRAIN, &ttybuf);
(void) sigprocmask(SIG_SETMASK, &sig_zero, NULL);
max_fd = sysconf(_SC_OPEN_MAX);
for (i = 3; i < max_fd; i++)
close(i);
if (msg)
fprintf(stderr, "%s", msg);
else
fprintf(stderr, "%s", nl);
fd = open("/dev/console", O_RDWR);
if (fd >= 0)
login_tty(fd);
execv(p, cargv);
fprintf(stderr, "dm: Unable to start console login: %s\n", strerror(errno));
_exit(1);
}
/* Start the console program. It will have stdin set to the controlling
* side of the console pty, stdout/stderr set to /dev/console inherited
* from the display manager, and descriptor 3 (auxiliary input) set to
* the read side of the pipe used for output by the session children.
*/
static void start_console(int master_fd, int aux_fd, char **argv)
{
int file;
syslog(LOG_DEBUG, "Starting console");
/* Create console log file owned by daemon */
if (access(consolelog, F_OK) != 0)
{
file = open(consolelog, O_CREAT, 0644);
close(file);
}
chown(consolelog, DAEMON, 0);
console_running = RUNNING;
switch (fork_and_store(&consolepid))
{
case 0:
/* Close all file descriptors except stdout/stderr */
close(0);
max_fd = sysconf(_SC_OPEN_MAX);
for (file = 3; file < max_fd; file++)
if (file != master_fd && file != aux_fd)
close(file);
setsid();
dup2(master_fd, 0);
close(master_fd);
if (aux_fd != 3)
{
dup2(aux_fd, 3);
close(aux_fd);
}
setgid(DAEMON);
setuid(DAEMON);
(void) sigprocmask(SIG_SETMASK, &sig_zero, (sigset_t *) 0);
execv(argv[0], argv);
fprintf(stderr, "dm: Failed to exec console: %s\n", strerror(errno));
_exit(1);
case -1:
fprintf(stderr, "dm: Unable to fork to start console: %s\n",
strerror(errno));
_exit(1);
default:
writepid(consolepidf, consolepid);
}
}
/* Kill children and hang around forever */
static void shutdown(int signo)
{
struct termios tc;
#ifdef TIOCCONS
int console_fd;
int on = 1;
#endif
if (login_running == RUNNING)
kill(loginpid, SIGHUP);
if (console_running == RUNNING)
kill(consolepid, SIGHUP);
if (x_running == RUNNING)
kill(xpid, SIGTERM);
setpgid(0, 0); /* We have to reset the tty pgrp */
tcsetpgrp(0, getpgrp());
tcflush(0, TCIOFLUSH);
(void) tcgetattr(0, &tc);
tc.c_lflag |= (ICANON | ISIG | ECHO);
(void) tcsetattr(0, TCSADRAIN, &tc);
(void) sigprocmask(SIG_SETMASK, &sig_zero, (sigset_t *) 0);
/* Stop console redirection. In the SRIOCSREDIR case, it is
* sufficient merely to close the slave side of the pty.
* In the TIOCCONS case, explicitly redirect to /dev/console.
*/
close(console_slave_fd);
#ifdef TIOCCONS
console_fd = open("/dev/console", O_RDWR);
if (console_fd >= 0)
ioctl(console_fd, TIOCCONS, &on);
#endif
close(console_master_fd);
while (1)
pause();
}
/* Kill children, remove password entry */
static void cleanup(char *line)
{
int ret;
#ifdef HAVE_GETUTXENT
struct utmpx *utx;
#else
int file;
struct utmp ut;
#endif /* HAVE_GETUTXENT */
char *errr, *login = NULL;
if (login_running == RUNNING)
kill(loginpid, SIGHUP);
if (console_running == RUNNING)
kill(consolepid, SIGHUP);
if (x_running == RUNNING)
{
kill(xpid, SIGTERM);
x_stop_wait();
}
if (line != NULL)
{
/* Find out what the login name was, so we can feed it to libAL. */
#ifdef HAVE_GETUTXENT
while ((utx = getutxent()) != NULL)
{
if (utx->ut_type != USER_PROCESS || utx->ut_user[0] == 0)
continue;
if (!strncmp(utx->ut_line, line, sizeof(utx->ut_line)))
{
login = malloc(sizeof(utx->ut_user) + 1);
strncpy(login, utx->ut_user, sizeof(utx->ut_user));
login[sizeof(utx->ut_user)] = '\0';
}
}
endutxent();
#else /* HAVE_GETUTXENT */
if ((file = open(UTFILE, O_RDWR, 0)) >= 0)
{
while (read(file, (char *) &ut, sizeof(utmp)) > 0)
{
if (!strncmp(ut.ut_line, line, sizeof(ut.ut_line)))
{
login = malloc(sizeof(ut.ut_name) + 1);
strncpy(login, ut.ut_name, sizeof(ut.ut_name));
login[sizeof(ut.ut_name)] = '\0';
}
}
close(file);
}
#endif
/* Update the utmp & wtmp. */
logout(line);
}
if (login != NULL)
{
ret = al_acct_revert(login, loginpid);
if (ret != AL_SUCCESS)
{
syslog(LOG_ERR, "al_acct_revert(%s, %d): %s", login, loginpid,
al_strerror(ret, &errr));
al_free_errmem(errr);
}
free(login);
}
tcflush(0, TCIOFLUSH);
}
/* When we get sigchild, figure out which child it was and set
* appropriate flags
*/
static void child(int signo)
{
int pid;
int status;
struct sigaction sigact;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigact.sa_handler = child;
sigaction(SIGCHLD, &sigact, NULL);
while (1)
{
pid = waitpid(-1, &status, WNOHANG);
if (pid == 0 || pid == -1)
return;
if (pid == xpid)
x_running = NONEXISTENT;
else if (pid == consolepid)
{
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
console_running = FAILED;
else
console_running = NONEXISTENT;
}
else if (pid == loginpid)
{
if (WIFEXITED(status) && WEXITSTATUS(status) == CONSOLELOGIN)
login_running = STARTUP;
else
login_running = NONEXISTENT;
}
else
{
syslog(LOG_DEBUG, "Received SIGCHLD for unknown pid %d", pid);
fprintf(stderr, "dm: Unexpected SIGCHLD from pid %d\n", pid);
}
}
}
static void xready(int signo)
{
x_running = RUNNING;
}
static void loginready(int signo)
{
login_running = RUNNING;
}
/* When an alarm happens, just note it and return */
static void catchalarm(int signo)
{
alarm_running = NONEXISTENT;
}
/* kill children and go away */
static void die(int signo)
{
cleanup(utmp_line);
_exit(0);
}
/* Takes a command line, returns an argv-style NULL terminated array
* of strings. The array is in malloc'ed memory.
*/
static char **parseargs(char *line, char *extra, char *extra1, char *extra2)
{
int i = 0;
char *p = line;
char **ret;
while (*p)
{
while (*p && isspace((unsigned char)*p))
p++;
while (*p && !isspace((unsigned char)*p))
p++;
i++;
}
ret = (char **) malloc(sizeof(char *) * (i + 4));
p = line;
i = 0;
while (*p)
{