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 librararies 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 *************************************<+>*************************************/
59 #include <sys/access.h>
61 #include <X11/Intrinsic.h>
62 #include <X11/StringDefs.h>
64 #if defined (USE_X11SSEXT)
65 #include <X11/extensions/scrnsaver.h>
66 #endif /* USE_X11SSEXT */
69 #include <Dt/UserMsg.h>
70 #include <Dt/EnvControlP.h>
74 #include "SmGlobals.h"
76 #include "SmRestore.h"
78 #include "SrvPalette.h"
79 #include "SmProtocol.h"
85 void main(int, char **);
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"
99 #define SECURE_SYS_PATH "/etc/shadow"
104 /*************************************<->*************************************
111 * Controls the startup and event dispatching of the session manager.
116 * argc = command line options
117 * argv = number of command line options
127 *************************************<->***********************************/
136 XWindowAttributes windAtt;
137 XPropertyEvent *pEvent = (XPropertyEvent *) &next;
141 struct sigaction stopvec;
146 # include <sys/sysinfo.h>
147 # include <sys/proc.h>
151 unsigned long nbytes = 1;
153 unsigned long flag = 0;
159 buffer[0] = SSIN_UACPROC;
160 buffer[1] = UAC_NOPRINT;
162 buffer[1] |= UAC_SIGBUS;
165 ssi_status = setsysinfo ( op, buffer, nbytes, arg, flag );
168 setlocale( LC_ALL, "" );
169 XtSetLanguageProc( NULL, NULL, NULL );
171 smGD.smState = IN_PROCESS;
172 smGD.programName = strdup (argv[0]);
175 * We report some errors before we call the DtInitialize procedure.
176 * The Dt initialize procedure sets this name. If we report
177 * an error via DtSimple error before this is set we see the message
178 * prepended with <unknown program name> in the error log.
180 DtProgName = SM_RESOURCE_NAME ;
193 * Set our effective gid to the real gid until we need it to be
194 * sys (during contention management)
196 smGD.runningGID = getgid();
197 smGD.conMgmtGID = getegid();
200 * Set uid up according to whether this is a secure system
201 * Secure systems need root priviledges to read the /etc/passwd file
203 smGD.runningUID = getuid();
205 #ifdef SECURE_SYS_PATH
206 status = stat(SECURE_SYS_PATH, &buf);
212 * Initialize LANG if it isn't defined.
214 if ((lang = getenv ("LANG")) == NULL)
217 (void) strcpy (lang, "LANG=C");
218 (void) putenv (lang);
224 * this is not a secure system - remove all suid priviledges
226 smGD.unLockUID = smGD.runningUID;
227 smGD.secureSystem = False;
228 SM_SETESUID(smGD.runningUID);
233 * Save the root priviledge to be restored when trying to unlock
235 smGD.unLockUID = geteuid();
236 smGD.secureSystem = True;
237 SM_SETEUID(smGD.runningUID);
241 setresgid(-1, smGD.runningGID, -1);
242 #else /* _AIX or any other system */
244 setregid(smGD.conMgmtGID, smGD.runningGID);
246 setgid(smGD.conMgmtGID);
247 setegid(smGD.runningGID);
252 * Set up POSIX sigaction structs
256 * Must set SIGPIPE to SIG_IGN so the process does not exit
257 * if a child terminates unexpectedly during an I/O operation
258 * that raises SIGPIPE. Note that before this process exec's
259 * any subprocesses, SIGPIPE must be set to SIG_DFL.
261 sigemptyset(&stopvec.sa_mask);
262 stopvec.sa_flags = 0;
263 stopvec.sa_handler = SIG_IGN;
264 (void) sigaction(SIGPIPE, &stopvec, (struct sigaction *) NULL);
266 stopvec.sa_handler = StopAll;
267 sigemptyset(&stopvec.sa_mask);
268 stopvec.sa_flags = 0;
270 smGD.childvec.sa_handler = WaitChildDeath;
271 sigemptyset(&smGD.childvec.sa_mask);
272 smGD.childvec.sa_flags = 0;
274 smGD.defvec.sa_handler = SIG_DFL;
275 sigemptyset(&smGD.defvec.sa_mask);
276 smGD.defvec.sa_flags = 0;
282 * This must be done before XtToolkitInitialize
283 * to set up the local environment
285 _DtEnvControl(DT_ENV_SET);
286 _DtEnvControl(DT_ENV_SET_BIN);
289 * Set up NLS error messages first
294 * Set the paths of where resources are to be restored from
296 SetRestorePath(argc, argv);
299 * The first thing that must happen is that resources must be restored
300 * so that my resources will be correct
302 if(smGD.compatMode == False)
305 * Load session resources.
307 RestoreResources(False,
311 smGD.resourcePath[0] != '\0' ? "-file" : NULL,
317 * Set up to catch SIGTERM
319 stopvec.sa_handler = StopAll;
320 sigemptyset(&stopvec.sa_mask);
321 stopvec.sa_flags = 0;
323 sigaction(SIGTERM, &stopvec, (struct sigaction *) NULL);
326 * Now set up a communication with the toolkit
328 * Create one display connection for dtsession, and one for
329 * the color server. We cannot share a display connection since
330 * motif creates a display object for the color server's display during
331 * color server initialization. Since the color server is not yet
332 * operational, any dialogs (ie the dtsession logout confirmation
333 * dialogs) created on that display do not get the color server colors.
334 * The dtsession display object is created after color server
335 * initialization is complete.
337 if (smGD.appCon == 0) {
339 * The following code will have been initialized if the
340 * session creation code posted a dialog.
342 XtToolkitInitialize();
343 smGD.appCon = XtCreateApplicationContext();
344 smGD.display = XtOpenDisplay(smGD.appCon, NULL, argv[0],
346 NULL, 0, &argc, argv);
349 srvDisplay = XtOpenDisplay(smGD.appCon, NULL, argv[0], SM_RESOURCE_CLASS,
350 NULL, 0, &argc, argv);
355 if (!InitXSMP (argv[0]))
358 /* Added the following check to exit in case of error RK 09.08.93 */
359 if(smGD.display == NULL)
361 PrintError(DtError, GETMESSAGE(4, 1, "Invalid display name - exiting."));
366 * Lock out other session managers from running - if there is one
367 * running already - exit
369 if(!_DtGetLock(smGD.display, SM_RUNNING_LOCK))
371 PrintError(DtError, GETMESSAGE(2, 2, "Another dtsession is currently running - exiting."));
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] != NULL) || (smGD.compatMode == False))
447 RestoreIndependentResources();
451 * Now restore the rest of the clients and the settings
453 if((smGD.clientPath[0] != NULL) && (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;
515 /* If we've just populated a new .dtprofile into the user's home
516 * directory, put up a popup dialog explaining the need to edit it
517 * to allow .login/.profile to work properly
520 arg = getenv("DTNEWPROFILE");
527 XtAppNextEvent(smGD.appCon, &next);
530 #if defined (USE_X11SSEXT)
531 if (next.type == smGD.ssEventType)
534 * We should simply be calling XtDispatchEvent() but the toolkit
535 * doesn't seem to know how to dispatch a run-time generated
538 ProcessEvent(smGD.topLevelWid, NULL, &next, NULL);
542 XtDispatchEvent(&next);
545 XtDispatchEvent(&next);
554 /*************************************<->*************************************
561 * Signal handler for SIGTERM. Causes dtsession to do a normal shutdown
562 * procedure without saving any state.
567 * shutDown (state flag)
577 * We want to be careful to ignore the TERM signal if we are already
578 * shutting down when it occurs.
580 *************************************<->***********************************/
585 ImmediateExit(-1, 0, True);
588 /*************************************<->*************************************
590 * RegisterX11ScreenSaver
595 * Register with X11 screen saver server extension for screen saver events.
599 * display - display from XtOpenDisplay()
600 * pssEventType - pointer to buffer in which to return ss event type
604 * pssEventType - (rc=0) screen saver event type, (rc!=0) undefined
608 * 0 - successfully registered for screen saver events
609 * other - failed to register for screen saver events
614 *************************************<->***********************************/
616 #if defined (USE_X11SSEXT)
618 RegisterX11ScreenSaver(
623 * Register with X11 screen saver server extension.
632 XSetWindowAttributes attr;
635 if (XScreenSaverQueryExtension(display, pssEventType, &ssErrorBase) &&
636 XScreenSaverQueryVersion(display, &majorVersion, &minorVersion) &&
640 * Server supports requested version of X11 screen saver extension.
642 screen = DefaultScreen(display);
643 root = DefaultRootWindow(display);
645 XGrabServer(display);
646 if (!XScreenSaverGetRegistered(display, screen, &xid, &type))
649 * No other clients registered with this server so register this one.
651 XScreenSaverRegister(display, screen, XtWindow(smGD.topLevelWid), XA_WINDOW);
654 XUngrabServer(display);
659 * Registration successful.
661 XScreenSaverSelectInput(display, root,
662 ScreenSaverNotifyMask|ScreenSaverCycleMask);
663 XScreenSaverSetAttributes(display, root, 0, 0, 1, 1, 0, CopyFromParent,
664 CopyFromParent, CopyFromParent, CWBackPixel, &attr);
669 #endif /* USE_X11SSEXT */