2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: SmMain.c /main/18 1998/04/20 12:59:26 mgreess $ */
25 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
26 * (c) Copyright 1993, 1994 International Business Machines Corp. *
27 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
28 * (c) Copyright 1993, 1994 Novell, Inc. *
30 /*************************************<+>*************************************
31 *****************************************************************************
35 ** Project: HP DT Session Manager (dtsession)
39 ** This is the controlling program for the session manager. It
40 ** calls routines to start the BMS, initialize globals and handlers,
41 ** and restore the correct session.
44 *******************************************************************
45 ** (c) Copyright Hewlett-Packard Company, 1990. All rights are
46 ** reserved. Copying or other reproduction of this program
47 ** except for archival purposes is prohibited without prior
48 ** written consent of Hewlett-Packard Company.
49 ********************************************************************
53 *****************************************************************************
54 *************************************<+>*************************************/
58 #include <X11/Intrinsic.h>
59 #include <X11/StringDefs.h>
61 #if defined (USE_X11SSEXT)
62 #include <X11/extensions/scrnsaver.h>
63 #endif /* USE_X11SSEXT */
66 #include <Dt/UserMsg.h>
67 #include <Dt/EnvControlP.h>
71 #include <DtXinerama.h> /* JET - Xinerama support */
75 #include "SmGlobals.h"
77 #include "SmRestore.h"
79 #include "SrvPalette.h"
80 #include "SmProtocol.h"
86 static void StopAll(int i);
87 static int RegisterX11ScreenSaver(Display *display, int *ssEventType);
93 #define SECURE_SYS_PATH "/.secure/etc/passwd"
96 #define SECURE_SYS_PATH "/etc/security/passwd"
98 #if defined(SVR4) || defined(__linux__)
99 #define SECURE_SYS_PATH "/etc/shadow"
102 #define SECURE_SYS_PATH "/etc/master.passwd"
107 /*************************************<->*************************************
114 * Controls the startup and event dispatching of the session manager.
119 * argc = command line options
120 * argv = number of command line options
130 *************************************<->***********************************/
132 main (int argc, char **argv)
138 XWindowAttributes windAtt;
139 XPropertyEvent *pEvent = (XPropertyEvent *) &next;
143 struct sigaction stopvec;
146 setlocale( LC_ALL, "" );
147 XtSetLanguageProc( NULL, NULL, NULL );
149 smGD.smState = IN_PROCESS;
150 smGD.programName = strdup (argv[0]);
152 /* JET - By default, we always want to leave when SmExit() is called. */
153 smGD.ExitComplete = True;
156 * We report some errors before we call the DtInitialize procedure.
157 * The Dt initialize procedure sets this name. If we report
158 * an error via DtSimple error before this is set we see the message
159 * prepended with <unknown program name> in the error log.
161 DtProgName = SM_RESOURCE_NAME ;
174 * Set our effective gid to the real gid until we need it to be
175 * sys (during contention management)
177 smGD.runningGID = getgid();
178 smGD.conMgmtGID = getegid();
181 * Set uid up according to whether this is a secure system
182 * Secure systems need root privileges to read the /etc/passwd file
184 smGD.runningUID = getuid();
186 # ifdef SECURE_SYS_PATH
187 status = stat(SECURE_SYS_PATH, &buf);
195 * this is not a secure system - remove all suid privileges
197 smGD.unLockUID = smGD.runningUID;
198 smGD.secureSystem = False;
199 SM_SETESUID(smGD.runningUID);
204 * Save the root privilege to be restored when trying to unlock
206 smGD.unLockUID = geteuid();
207 smGD.secureSystem = True;
208 SM_SETEUID(smGD.runningUID);
212 * Initialize LANG if it isn't defined.
214 if ((lang = getenv ("LANG")) == NULL)
217 (void) strcpy (lang, "LANG=C");
218 (void) putenv (lang);
222 setresgid(-1, smGD.runningGID, -1);
223 #else /* _AIX or any other system */
225 setregid(smGD.conMgmtGID, smGD.runningGID);
227 setgid(smGD.conMgmtGID);
228 setegid(smGD.runningGID);
233 * Set up POSIX sigaction structs
237 * Must set SIGPIPE to SIG_IGN so the process does not exit
238 * if a child terminates unexpectedly during an I/O operation
239 * that raises SIGPIPE. Note that before this process exec's
240 * any subprocesses, SIGPIPE must be set to SIG_DFL.
242 sigemptyset(&stopvec.sa_mask);
243 stopvec.sa_flags = 0;
244 stopvec.sa_handler = SIG_IGN;
245 (void) sigaction(SIGPIPE, &stopvec, (struct sigaction *) NULL);
247 stopvec.sa_handler = StopAll;
248 sigemptyset(&stopvec.sa_mask);
249 stopvec.sa_flags = 0;
251 smGD.childvec.sa_handler = WaitChildDeath;
252 sigemptyset(&smGD.childvec.sa_mask);
253 smGD.childvec.sa_flags = 0;
255 smGD.defvec.sa_handler = SIG_DFL;
256 sigemptyset(&smGD.defvec.sa_mask);
257 smGD.defvec.sa_flags = 0;
263 * This must be done before XtToolkitInitialize
264 * to set up the local environment
266 _DtEnvControl(DT_ENV_SET);
267 _DtEnvControl(DT_ENV_SET_BIN);
270 * Set up NLS error messages first
275 * Set the paths of where resources are to be restored from
277 SetRestorePath(argc, argv);
280 * The first thing that must happen is that resources must be restored
281 * so that my resources will be correct
283 if(smGD.compatMode == False)
286 * Load session resources.
288 RestoreResources(False,
292 smGD.resourcePath[0] != '\0' ? "-file" : NULL,
298 * Set up to catch SIGTERM
300 stopvec.sa_handler = StopAll;
301 sigemptyset(&stopvec.sa_mask);
302 stopvec.sa_flags = 0;
304 sigaction(SIGTERM, &stopvec, (struct sigaction *) NULL);
307 * Now set up a communication with the toolkit
309 * Create one display connection for dtsession, and one for
310 * the color server. We cannot share a display connection since
311 * motif creates a display object for the color server's display during
312 * color server initialization. Since the color server is not yet
313 * operational, any dialogs (ie the dtsession logout confirmation
314 * dialogs) created on that display do not get the color server colors.
315 * The dtsession display object is created after color server
316 * initialization is complete.
318 if (smGD.appCon == 0) {
320 * The following code will have been initialized if the
321 * session creation code posted a dialog.
323 XtToolkitInitialize();
324 smGD.appCon = XtCreateApplicationContext();
325 smGD.display = XtOpenDisplay(smGD.appCon, NULL, argv[0],
327 NULL, 0, &argc, argv);
330 srvDisplay = XtOpenDisplay(smGD.appCon, NULL, argv[0], SM_RESOURCE_CLASS,
331 NULL, 0, &argc, argv);
336 if (!InitXSMP (argv[0]))
339 /* Added the following check to exit in case of error RK 09.08.93 */
340 if(smGD.display == NULL)
342 PrintError(DtError, GETMESSAGE(4, 1, "Invalid display name - exiting."));
347 * Lock out other session managers from running - if there is one
348 * running already - exit
350 if(!_DtGetLock(smGD.display, SM_RUNNING_LOCK))
352 PrintError(DtError, GETMESSAGE(2, 2, "Another dtsession is currently running - exiting."));
356 /* JET - initialize for Xinerama, if present 4/12/2001 */
358 smGD.DtXineramaInfo = _DtXineramaInit(smGD.display);
361 if (smGD.DtXineramaInfo == NULL)
362 { /* No xinerama, how... sad. */
363 fprintf(stderr, "### JET SmMain: Xinerama NOT available.\n");
367 fprintf(stderr, "### JET SmMain: Xinerama available, scrns = %d\n",
368 dpyinfo.DtXineramaInfo->numscreens);
376 * Restore preferences
378 if (smGD.resourcePath[0] != '\0')
380 RestorePreferences(smGD.resourcePath);
384 * Start up the color server
386 InitializeDtcolor(srvDisplay, smGD.sessionType);
389 * AFTER the colors are set up - create the top level widget
390 * Set up a NULL WM_COMMAND property
393 XtSetArg(args[n], XmNbackground,
394 XBlackPixel(smGD.display, XDefaultScreen(smGD.display))); n++;
395 XtSetArg(args[n], XmNmappedWhenManaged, False); n++;
396 XtSetArg (args[n], XmNwidth, 1); n++;
397 XtSetArg (args[n], XmNheight, 1); n++;
398 smGD.topLevelWid = XtAppCreateShell (SM_RESOURCE_NAME, SM_RESOURCE_CLASS,
399 applicationShellWidgetClass,
400 smGD.display, args, n);
401 XtRealizeWidget(smGD.topLevelWid);
404 * Select to get the lock on timeout if the user requests it
405 * and if the server supports it. This must be done before
406 * InitSMGlobals() so smGD.lockOnTimeoutStatus is known.
408 #if defined (USE_HPSSEXT)
409 #ifdef USE_HP_SPECIFIC_XLIB
410 tmp = XHPSSChangeNotify(smGD.display, &XaSmScreenSaveRet, 3);
411 #else /* USE_HP_SPECIFIC_XLIB */
413 #endif /* USE_HP_SPECIFIC_XLIB */
414 #elif defined (USE_X11SSEXT)
415 tmp = RegisterX11ScreenSaver(smGD.display, &smGD.ssEventType);
422 smGD.lockOnTimeoutStatus = False;
423 XaSmScreenSaveRet = None;
424 #if defined (USE_X11SSEXT)
425 smGD.ssEventType = None;
430 smGD.lockOnTimeoutStatus = True;
436 * Put the program into a wait state
443 * Restore resources for lang/resolution independence
445 if((smGD.resourcePath[0] != 0) || (smGD.compatMode == False))
447 RestoreIndependentResources();
451 * Now restore the rest of the clients and the settings
453 if((smGD.clientPath[0] != 0) && (smGD.compatMode == False))
455 if(RestoreState() == -1)
462 if(smGD.compatMode == True)
465 * dtstyle needs the DT_SESSION_STATE even in
474 * Run the user's startup script if there is one
477 if(smGD.compatMode == False)
479 StartEtc(False); /* run sessionetc */
485 * If we are in compatibility mode - kill the parent and leave the
486 * child running so that the script exits
488 if(smGD.compatMode == True)
494 * Select for property notify on the top level window - so that
495 * when the style manager changes something - it is known
497 XGetWindowAttributes(smGD.display, smGD.topLevelWindow, &windAtt);
498 XSelectInput(smGD.display, smGD.topLevelWindow,
499 windAtt.your_event_mask | PropertyChangeMask);
501 ShowWaitState(False);
504 * Register for events
505 * - PropertyChange (maskable)
506 * - ClientMessage (non-maskable)
508 XtAddEventHandler(smGD.topLevelWid,
510 True, ProcessEvent, NULL);
512 smGD.smState = READY;
516 XtAppNextEvent(smGD.appCon, &next);
519 #if defined (USE_X11SSEXT)
520 if (next.type == smGD.ssEventType)
523 * We should simply be calling XtDispatchEvent() but the toolkit
524 * doesn't seem to know how to dispatch a run-time generated
527 ProcessEvent(smGD.topLevelWid, NULL, &next, NULL);
531 XtDispatchEvent(&next);
534 XtDispatchEvent(&next);
543 /*************************************<->*************************************
550 * Signal handler for SIGTERM. Causes dtsession to do a normal shutdown
551 * procedure without saving any state.
556 * shutDown (state flag)
566 * We want to be careful to ignore the TERM signal if we are already
567 * shutting down when it occurs.
569 *************************************<->***********************************/
574 ImmediateExit(-1, 0, True);
577 /*************************************<->*************************************
579 * RegisterX11ScreenSaver
584 * Register with X11 screen saver server extension for screen saver events.
588 * display - display from XtOpenDisplay()
589 * pssEventType - pointer to buffer in which to return ss event type
593 * pssEventType - (rc=0) screen saver event type, (rc!=0) undefined
597 * 0 - successfully registered for screen saver events
598 * other - failed to register for screen saver events
603 *************************************<->***********************************/
605 #if defined (USE_X11SSEXT)
607 RegisterX11ScreenSaver(
612 * Register with X11 screen saver server extension.
623 if (XScreenSaverQueryExtension(display, pssEventType, &ssErrorBase) &&
624 XScreenSaverQueryVersion(display, &majorVersion, &minorVersion) &&
628 * Server supports requested version of X11 screen saver extension.
630 screen = DefaultScreen(display);
631 root = DefaultRootWindow(display);
633 XGrabServer(display);
634 if (!XScreenSaverGetRegistered(display, screen, &xid, &type))
637 * No other clients registered with this server so register this one.
639 XScreenSaverRegister(display, screen, XtWindow(smGD.topLevelWid), XA_WINDOW);
642 XUngrabServer(display);
646 XSetWindowAttributes attr;
649 * Registration successful.
651 XScreenSaverSelectInput(display, root,
652 ScreenSaverNotifyMask|ScreenSaverCycleMask);
654 /* Even though OverrideRedirect is the default attribute in this
655 * check, lets make sure it's set, and make sure only the
656 * CWOverrideRedirect attr is being looked at. The rest is
657 * just random garbage anyway, since it's allocated from the stack.
659 attr.override_redirect = True;
660 XScreenSaverSetAttributes(display, root, 0, 0, 1, 1, 0, CopyFromParent,
661 CopyFromParent, CopyFromParent, CWOverrideRedirect, &attr);
666 #endif /* USE_X11SSEXT */