~ubuntu-branches/ubuntu/quantal/xdm/quantal

« back to all changes in this revision

Viewing changes to dm.c

  • Committer: Bazaar Package Importer
  • Author(s): Brice Goglin
  • Date: 2010-03-14 11:44:38 UTC
  • mto: (9.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20100314114438-xce9zkmf88jtlq0l
Tags: upstream-1.1.9
ImportĀ upstreamĀ versionĀ 1.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $XdotOrg: $ */
2
 
/* $Xorg: dm.c,v 1.5 2001/02/09 02:05:40 xorgcvs Exp $ */
3
1
/*
4
2
 
5
3
Copyright 1988, 1998  The Open Group
27
25
from The Open Group.
28
26
 
29
27
*/
30
 
/* $XFree86: xc/programs/xdm/dm.c,v 3.23 2003/09/17 05:48:32 herrb Exp $ */
31
28
 
32
29
/*
33
30
 * xdm - display manager daemon
36
33
 * display manager
37
34
 */
38
35
 
39
 
# include       "dm.h"
40
 
# include       "dm_auth.h"
41
 
# include       "dm_error.h"
 
36
#include        "dm.h"
 
37
#include        "dm_auth.h"
 
38
#include        "dm_error.h"
42
39
 
43
 
# include       <stdio.h>
 
40
#include        <stdio.h>
44
41
#ifdef X_POSIX_C_SOURCE
45
 
#define _POSIX_C_SOURCE X_POSIX_C_SOURCE
46
 
#include <signal.h>
47
 
#undef _POSIX_C_SOURCE
48
 
#else
49
 
#if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
50
 
#include <signal.h>
51
 
#else
52
 
#define _POSIX_SOURCE
53
 
#include <signal.h>
54
 
#undef _POSIX_SOURCE
55
 
#endif
 
42
# define _POSIX_C_SOURCE X_POSIX_C_SOURCE
 
43
# include <signal.h>
 
44
# undef _POSIX_C_SOURCE
 
45
#else
 
46
# if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
 
47
#  include <signal.h>
 
48
# else
 
49
#  define _POSIX_SOURCE
 
50
#  include <signal.h>
 
51
#  undef _POSIX_SOURCE
 
52
# endif
56
53
#endif
57
54
#ifdef __NetBSD__
58
 
#include <sys/param.h>
 
55
# include <sys/param.h>
59
56
#endif
60
57
#ifdef USESECUREWARE
61
 
#include <prot.h>
 
58
# include <prot.h>
62
59
#endif
63
60
 
64
61
#ifndef sigmask
65
 
#define sigmask(m)  (1 << ((m - 1)))
 
62
# define sigmask(m)  (1 << ((m - 1)))
66
63
#endif
67
64
 
68
 
# include       <sys/stat.h>
69
 
# include       <errno.h>
70
 
# include       <X11/Xfuncproto.h>
71
 
# include       <X11/Xatom.h>
72
 
# include       <stdarg.h>
73
 
# include       <stdint.h>
 
65
#include        <sys/stat.h>
 
66
#include        <errno.h>
 
67
#include        <X11/Xfuncproto.h>
 
68
#include        <X11/Xatom.h>
 
69
#include        <stdarg.h>
 
70
#include        <stdint.h>
74
71
 
75
72
#ifndef F_TLOCK
76
 
#ifndef X_NOT_POSIX
77
 
# include       <unistd.h>
78
 
#endif
 
73
# ifndef X_NOT_POSIX
 
74
#  include      <unistd.h>
 
75
# endif
79
76
#endif
80
77
 
81
78
 
107
104
#endif
108
105
 
109
106
static int StorePid (void);
 
107
static void RemovePid (void);
110
108
 
111
109
static pid_t parent_pid = -1;   /* PID of parent xdm process */
112
110
 
145
143
    }
146
144
    if (debugLevel == 0 && daemonMode)
147
145
        BecomeDaemon ();
 
146
    if (debugLevel == 0)
 
147
        InitErrorLog ();
148
148
    if (debugLevel >= 10)
149
149
        nofork_session = 1;
150
150
    /* SUPPRESS 560 */
157
157
                 pidFile, oldpid);
158
158
        exit (1);
159
159
    }
160
 
    if (debugLevel == 0)
161
 
        InitErrorLog ();
 
160
 
 
161
    LogInfo ("Starting\n");
 
162
 
 
163
    if (atexit (RemovePid))
 
164
        LogError ("could not register RemovePid() with atexit()\n");
162
165
 
163
166
    if (nofork_session == 0) {
164
167
        /* Clean up any old Authorization files */
199
202
#ifndef UNRELIABLE_SIGNALS
200
203
    (void) Signal (SIGCHLD, ChildNotify);
201
204
#endif
 
205
    Debug ("startup successful; entering main loop\n");
202
206
    while (
203
207
#ifdef XDMCP
204
208
           AnyWellKnownSockets() ||
217
221
#endif
218
222
    }
219
223
    Debug ("Nothing left to do, exiting\n");
 
224
    LogInfo ("Exiting\n");
220
225
    exit(0);
221
226
    /*NOTREACHED*/
222
227
}
373
378
 
374
379
    if (parent_pid != getpid())
375
380
    {
376
 
        /* 
 
381
        /*
377
382
         * We are a child xdm process that was killed by the
378
383
         * master xdm before we were able to return from fork()
379
384
         * and remove this signal handler.
380
385
         *
381
386
         * See defect XWSog08655 for more information.
382
387
         */
383
 
        Debug ("Child xdm caught SIGTERM before it remove that signal.\n");
 
388
        Debug ("Child xdm caught SIGTERM before it removed that signal.\n");
384
389
        (void) Signal (n, SIG_DFL);
385
390
        TerminateProcess (getpid(), SIGTERM);
386
391
        errno = olderrno;
387
392
        return;
388
393
    }
389
394
    Debug ("Shutting down entire manager\n");
 
395
    LogInfo ("Shutting down\n");
390
396
#ifdef XDMCP
391
397
    DestroyWellKnownSockets ();
392
398
#endif
414
420
    int olderrno = errno;
415
421
 
416
422
    ChildReady = 1;
417
 
#ifdef ISC
 
423
# ifdef ISC
418
424
    (void) Signal (SIGCHLD, ChildNotify);
419
 
#endif
 
425
# endif
420
426
    errno = olderrno;
421
427
}
422
428
#endif
437
443
    /* XXX classic System V signal race condition here with RescanNotify */
438
444
    if ((pid = wait (&status)) != -1)
439
445
#else
440
 
#ifndef X_NOT_POSIX
 
446
# ifndef X_NOT_POSIX
441
447
    sigemptyset(&mask);
442
448
    sigaddset(&mask, SIGCHLD);
443
449
    sigaddset(&mask, SIGHUP);
444
450
    sigprocmask(SIG_BLOCK, &mask, &omask);
445
451
    Debug ("signals blocked\n");
446
 
#else
 
452
# else
447
453
    omask = sigblock (sigmask (SIGCHLD) | sigmask (SIGHUP));
448
454
    Debug ("signals blocked, mask was 0x%x\n", omask);
449
 
#endif
 
455
# endif
450
456
    if (!ChildReady && !Rescan)
451
 
#ifndef X_NOT_POSIX
 
457
# ifndef X_NOT_POSIX
452
458
        sigsuspend(&omask);
453
 
#else
 
459
# else
454
460
        sigpause (omask);
455
 
#endif
 
461
# endif
456
462
    ChildReady = 0;
457
 
#ifndef X_NOT_POSIX
 
463
# ifndef X_NOT_POSIX
458
464
    sigprocmask(SIG_SETMASK, &omask, (sigset_t *)NULL);
459
 
#else
 
465
# else
460
466
    sigsetmask (omask);
461
 
#endif
 
467
# endif
462
468
    while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
463
469
#endif
464
470
    {
518
524
                  Time_t Time;
519
525
                  time(&Time);
520
526
                  Debug("time %i %i\n",Time,d->lastCrash);
521
 
                  if (d->lastCrash && 
 
527
                  if (d->lastCrash &&
522
528
                      ((Time - d->lastCrash) < XDM_BROKEN_INTERVAL)) {
523
529
                    Debug("Server crash frequency too high:"
524
530
                          " removing display %s\n",d->name);
528
534
                    AddTimerEntropy();
529
535
#endif
530
536
                    RemoveDisplay (d);
531
 
                  } else 
 
537
                  } else
532
538
                    d->lastCrash = Time;
533
539
                }
534
540
                break;
637
643
        const char *windowpath;
638
644
        char *newwindowpath;
639
645
        unsigned long num;
640
 
        char nums[10];
641
 
        int numn;
642
646
 
643
647
        prop = XInternAtom(d->dpy, "XFree86_VT", False);
644
648
        if (prop == None) {
645
649
                fprintf(stderr, "no XFree86_VT atom\n");
646
650
                return;
647
651
        }
648
 
        if (XGetWindowProperty(d->dpy, DefaultRootWindow(d->dpy), prop, 0, 1, 
649
 
                False, AnyPropertyType, &actualtype, &actualformat, 
 
652
        if (XGetWindowProperty(d->dpy, DefaultRootWindow(d->dpy), prop, 0, 1,
 
653
                False, AnyPropertyType, &actualtype, &actualformat,
650
654
                &nitems, &bytes_after, &buf)) {
651
655
                fprintf(stderr, "no XFree86_VT property\n");
652
656
                return;
683
687
        }
684
688
        XFree(buf);
685
689
        windowpath = getenv("WINDOWPATH");
686
 
        numn = snprintf(nums, sizeof(nums), "%lu", num);
687
690
        if (!windowpath) {
688
 
                newwindowpath = malloc(numn + 1);
689
 
                sprintf(newwindowpath, "%s", nums);
 
691
                asprintf(&newwindowpath, "%lu", num);
690
692
        } else {
691
 
                newwindowpath = malloc(strlen(windowpath) + 1 + numn + 1);
692
 
                sprintf(newwindowpath, "%s:%s", windowpath, nums);
 
693
                asprintf(&newwindowpath, "%s:%lu", windowpath, num);
693
694
        }
694
695
        if (d->windowPath)
695
696
                free(d->windowPath);
702
703
    pid_t       pid;
703
704
 
704
705
    Debug ("StartDisplay %s\n", d->name);
 
706
    LogInfo ("Starting X server on %s\n", d->name);
705
707
    LoadServerResources (d);
706
708
    if (d->displayType.location == Local)
707
709
    {
874
876
        if (lockPidFile)
875
877
        {
876
878
#ifdef F_SETLK
877
 
#ifndef SEEK_SET
878
 
#define SEEK_SET 0
879
 
#endif
 
879
# ifndef SEEK_SET
 
880
#  define SEEK_SET 0
 
881
# endif
880
882
            struct flock lock_data;
881
883
            lock_data.l_type = F_WRLCK;
882
884
            lock_data.l_whence = SEEK_SET;
889
891
                    return -1;
890
892
            }
891
893
#else
892
 
#ifdef LOCK_EX
 
894
# ifdef LOCK_EX
893
895
            if (flock (pidFd, LOCK_EX|LOCK_NB) == -1)
894
896
            {
895
897
                if (errno == EWOULDBLOCK)
897
899
                else
898
900
                    return -1;
899
901
            }
900
 
#else
 
902
# else
901
903
            if (lockf (pidFd, F_TLOCK, 0) == -1)
902
904
            {
903
905
                if (errno == EACCES)
905
907
                else
906
908
                    return -1;
907
909
            }
908
 
#endif
 
910
# endif
909
911
#endif
910
912
        }
 
913
        ftruncate(pidFd, 0);
911
914
        fprintf (pidFilePtr, "%5ld\n", (long)getpid ());
912
915
        (void) fflush (pidFilePtr);
913
916
        RegisterCloseOnFork (pidFd);
915
918
    return 0;
916
919
}
917
920
 
 
921
/*
 
922
 * Remove process ID file.  This function is registered with atexit().
 
923
 */
 
924
static void
 
925
RemovePid (void)
 
926
{
 
927
    Debug ("unlinking process ID file %s\n", pidFile);
 
928
    if (unlink (pidFile))
 
929
        if (errno != ENOENT)
 
930
            LogError ("cannot remove process ID file %s: %s\n", pidFile,
 
931
                      _SysErrorMsg (errno));
 
932
}
 
933
 
918
934
#if 0
919
935
void
920
936
UnlockPidFile (void)
921
937
{
922
938
    if (lockPidFile)
923
 
#ifdef F_SETLK
 
939
# ifdef F_SETLK
924
940
    {
925
941
        struct flock lock_data;
926
942
        lock_data.l_type = F_UNLCK;
928
944
        lock_data.l_start = lock_data.l_len = 0;
929
945
        (void) fcntl(pidFd, F_SETLK, &lock_data);
930
946
    }
931
 
#else
932
 
#ifdef F_ULOCK
 
947
# else
 
948
#  ifdef F_ULOCK
933
949
        lockf (pidFd, F_ULOCK, 0);
934
 
#else
 
950
#  else
935
951
        flock (pidFd, LOCK_UN);
936
 
#endif
937
 
#endif
 
952
#  endif
 
953
# endif
938
954
    close (pidFd);
939
955
    fclose (pidFilePtr);
940
956
}
943
959
#ifndef HAS_SETPROCTITLE
944
960
void SetTitle (char *name, ...)
945
961
{
946
 
#ifndef NOXDMTITLE
 
962
# ifndef NOXDMTITLE
947
963
    char        *p = Title;
948
964
    int left = TitleLen;
949
965
    char        *s;
968
984
        --left;
969
985
    }
970
986
    va_end(args);
971
 
#endif  
 
987
# endif
972
988
}
973
989
#endif