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 /******************************************************************************
24 ******************************************************************************
27 ** RCS: $XConsortium: manager.c /main/8 1996/10/30 19:10:15 cde-hp $
31 ** (c) Copyright 1995, Hewlett-Packard Company, all rights reserved.
33 ******************************************************************************
34 *****************************************************************************/
38 #define OPTIONAL_PXAUTH_PRETEST 1
47 /******************************************************************************
48 ******************************************************************************
50 * The following PDM_MANAGER : PDM_START routines are registered
51 * per child, and dispatched out of XtAppNextEvent
54 /********************************************************************
56 * message_pipe_handler()
58 * Takes stderr from the child, via a pipe, and stores the output
59 * in the client tracking record.
62 static void message_pipe_handler( XtPointer w, int *source, XtInputId *id)
65 struct stat statBuffer;
70 * Find out who is generating output.
74 for ( i = 0; i < g.serviceRecNum; i++ ) {
75 if ( g.serviceRecs[i]->message_pipe[0] == *source ) {
78 * Fetch size and grow message_string to hold more.
80 if ( fstat(*source, &statBuffer) ) {
81 /* unable to get size */
82 statBuffer.st_size = 0; /* bail out below */
85 if ( statBuffer.st_size > 0 ) {
86 if ( g.serviceRecs[i]->message_string ) {
87 inc = strlen( g.serviceRecs[i]->message_string );
89 g.serviceRecs[i]->message_string = Xrealloc(
90 (char *) g.serviceRecs[i]->message_string,
91 statBuffer.st_size + inc + 1 );
96 g.serviceRecs[i]->message_string = Xmalloc(
97 statBuffer.st_size + 1 );
101 * Read off what we know is there.
105 n = read( *source, &(g.serviceRecs[i]->message_string[inc]),
106 statBuffer.st_size );
108 if ( n == statBuffer.st_size ) {
109 /* read all there is (per the previous stat) */
112 else if (( n == -1 ) && ( errno == EINTR )) {
113 /* an interrupt came in before the read could start */
116 else if (( n == -1 ) || ( n == 0 )) {
117 /* problems - bail out */
118 g.serviceRecs[i]->message_pipe[0] = -1;
124 /* only a partial read, probably a sig, try for more */
126 statBuffer.st_size -= n;
132 * NULL terminate what we have so far to make it look
135 g.serviceRecs[i]->message_string[statBuffer.st_size+inc] = '\0';
139 * No more to read - this really means the pipe is
142 g.serviceRecs[i]->message_pipe[0] = -1;
151 * Some stray pipe that we no longer have information on.
158 * Scan client records and see who is ready to be
165 /******************************************************************************
166 ******************************************************************************
168 * The following PDM_MANAGER : PDM_START routines are called by
169 * the dispatch routines to service selection requests and
173 /********************************************************************
175 * Setup a child service record per the selection request.
177 void mgr_initialize( XEvent *report, XpPdmServiceRec *rec )
182 Display *selection_display;
185 unsigned long tafter;
187 XTextProperty text_prop;
192 * Grab the PDM_CLIENT_PROP from which all the juicy
193 * information is retrieved.
195 selection_display = report->xselectionrequest.display;
196 requestor = report->xselectionrequest.requestor;
197 prop_atom = report->xselectionrequest.property;
199 if ( XGetWindowProperty( selection_display, requestor, prop_atom,
200 0, 100000, True, AnyPropertyType,
205 &text_prop.value ) != Success ) {
209 rec->pdm_exec_errorcode = g.pdm_start_error;
211 sprintf( buf, PDMD_MSG_5, g.prog_name );
212 rec->pdm_exec_errormessage = xpstrdup( buf );
217 if ( text_prop.format != 8 ) {
221 rec->pdm_exec_errorcode = g.pdm_start_error;
223 sprintf( buf, PDMD_MSG_6, g.prog_name );
224 rec->pdm_exec_errormessage = xpstrdup( buf );
229 if ( XmbTextPropertyToTextList( selection_display, &text_prop,
230 &list, &list_cnt ) < 0 ) {
234 rec->pdm_exec_errorcode = g.pdm_start_error;
236 sprintf( buf, PDMD_MSG_7, g.prog_name );
237 rec->pdm_exec_errormessage = xpstrdup( buf );
243 * Fill in the PDM_MANAGER portion of the client record.
245 rec->video_display_str = xpstrdup( list[0] );
246 rec->video_window = strtol(list[1], (char **)NULL, 16);
247 rec->print_display_str = xpstrdup( list[2] );
248 rec->print_window = strtol(list[3], (char **)NULL, 16);
249 rec->print_context = strtol(list[4], (char **)NULL, 16);
250 rec->locale_hint = xpstrdup( list[5] );
251 XFreeStringList( list );
253 rec->selection_display = selection_display;
254 rec->requestor = requestor;
255 rec->prop_atom = prop_atom;
256 rec->selection = report->xselectionrequest.selection;
257 rec->time = report->xselectionrequest.time;
259 rec->mgr_flag = True; /* mgr portion of rec now valid */
262 * Optimization. The only live display connection, for which we
263 * need to trap XIO errors, is "selection display". For the
264 * "video" and "print" displays, we have the display strings and
265 * can establish connections as we need them. Since they are rarely
266 * used, and opening them up here would create XIO liability problems
267 * and a startup performance hit, we won't establish connections now.
269 * One optimization however is to see if the "print" display would
270 * just happen to be the same at the "selection display" currently
273 if ( !strcmp( XDisplayString(rec->selection_display),
274 rec->print_display_str ) ) {
275 rec->seldpy_as_printdpy = True;
278 rec->seldpy_as_printdpy = False;
280 #ifdef OPTIONAL_PXAUTH_PRETEST
282 * Verify connectability to the Print Server.
284 * Note: once beyond the selection phase, all communication
285 * will be by way of the Print Server. If we cannot
286 * connect later, then we will have no way to deliver
287 * EXIT_PXAUTH, EXIT_VXAUTH, EXIT_ERROR, EXIT_OK or
288 * EXIT_CANCEL. Real bad news!
290 * It is better to discover now that we don't have
291 * connection authorization for the print display since
292 * we can still let the user know of PXAUTH problems
293 * via the selection display currently open.
295 * Unfortunately, this pre-test is a performance hit in the
298 if ( ! (testdpy = XOpenDisplay(rec->print_display_str)) ) {
299 rec->pdm_exec_errorcode = g.pdm_start_pxauth;
302 XCloseDisplay( testdpy );
303 #endif /* OPTIONAL_PXAUTH_PRETEST */
306 #ifdef OPTIONAL_VXAUTH_PRETEST
308 * Verify connectability to the Video Server.
310 * It is better to discover now that we don't have
311 * connection authorization for the video display since
312 * we can still let the user know of VXAUTH problems
313 * via the selection display currently open.
315 * Unfortunately, this pre-test is a performance hit in the
318 if ( ! (testdpy = XOpenDisplay(rec->video_display_str)) ) {
319 rec->pdm_exec_errorcode = g.pdm_start_vxauth;
322 XCloseDisplay( testdpy );
323 #endif /* OPTIONAL_VXAUTH_PRETEST */
326 /********************************************************************
328 * fork/exec a child pdm after setting up a message pipe.
330 void mgr_launch_pdm( XpPdmServiceRec *rec )
333 struct sigaction svec;
343 * Setup message pipe.
345 if ( pipe(rec->message_pipe) == -1 ) {
346 rec->pdm_exec_errorcode = g.pdm_start_error;
347 sprintf( buf, PDMD_MSG_8, g.prog_name );
348 rec->pdm_exec_errormessage = xpstrdup( buf );
352 rec->message_xtid = XtAppAddInput( g.context, rec->message_pipe[0],
353 (XtPointer) XtInputReadMask,
354 message_pipe_handler, (XtPointer) NULL );
357 * See if a cookie file is needed.
359 if (rec->cookie_cnt) {
361 * Create new .Xauthority file.
363 original_umask = umask (0077); /* disallow non-owner access */
364 tmpnam( rec->auth_filename );
365 rec->auth_file = fopen( rec->auth_filename, "w" );
367 if (rec->auth_file) {
369 * Copy existing .Xauthority entries.
371 existing_name = XauFileName ();
374 if (access (existing_name, R_OK) == 0) { /* checks REAL id */
375 existing_file = fopen (existing_name, "r");
378 entry = XauReadAuth (existing_file);
382 XauWriteAuth( rec->auth_file, entry );
383 XauDisposeAuth (entry);
385 fclose (existing_file);
391 * Merge in cookies recently sent.
393 for ( i = 0; i < rec->cookie_cnt; i++ ) {
394 XauWriteAuth( rec->auth_file, rec->cookies[i] );
397 fclose( rec->auth_file );
399 original_umask = umask (original_umask);
405 if ( rec->pid < 0 ) {
406 rec->pdm_exec_errorcode = g.pdm_start_error;
407 sprintf( buf, PDMD_MSG_9, g.prog_name );
408 rec->pdm_exec_errormessage = xpstrdup( buf );
411 else if ( rec->pid == 0) {
417 * Hook stderr back to parent via message pipe.
419 dup2(rec->message_pipe[1], 2);
420 close(rec->message_pipe[0]);
423 * The child should have default behavior for all signals.
425 sigemptyset(&svec.sa_mask);
427 svec.sa_handler = SIG_DFL;
428 (void) sigaction(SIGCLD, &svec, (struct sigaction *) NULL);
430 for (i=3; i < FOPEN_MAX; i++) {
431 if ((i != rec->message_pipe[1]) &&
432 (rec->auth_file && (i != fileno(rec->auth_file))))
434 (void) fcntl (i, F_SETFD, 1);
439 * Set the new locale for the child.
441 * note: the locale hint will be of the form:
443 * name_spec[;registry_spec[;ver_spec[;encoding_spec]]]
445 * for now, just pull out the name_spec (e.g. 'C')
446 * and use it. With a little work, a more complex
447 * syntax could be understood and the appropriate
448 * actions taken here rather than just wedging
449 * name_spec into setlocale() and hoping.
451 if ( !(rec->locale_hint) ) {
453 * Leave current locale alone.
456 else if ( strcmp( rec->locale_hint, "" ) ) {
458 * Leave current locale alone. Note that "" into
459 * setlocale says to go with default vs leave it alone.
465 tptr1 = xpstrdup( rec->locale_hint );
466 tptr2 = strchr( tptr1, ';' );
467 if (tptr2) *tptr2 = '\0';
469 setlocale( LC_ALL, tptr1 );
474 * Set XAUTHORITY env var if needed.
476 if ((rec->cookie_cnt) && (rec->auth_filename) && (rec->auth_file)) {
477 envstr = Xmalloc( strlen(rec->auth_filename) + 12 );
478 sprintf( envstr, "XAUTHORITY=%s", rec->auth_filename );
483 * Start the child for real.
485 (void) execvp(rec->pdm_exec_argvs[0], rec->pdm_exec_argvs);
487 (void) fprintf (stderr, PDMD_MSG_10, g.prog_name, rec->pdm_exec_argvs[0]);
490 * tomg - need to deal with failed child start.
492 exit(PDM_EXIT_ERROR);
500 * Close the write end of the pipe - only the child needs it.
502 close(rec->message_pipe[1]);
503 rec->message_pipe[1] = -1;
508 /********************************************************************
510 * Figure out which pdm executable to later fork/exec.
512 void mgr_fetch_pdm( XpPdmServiceRec *rec )
514 char tstr[1024], *tptr1, *tptr2, *tptr3;
520 if ( g.override_pdm ) {
522 * Override all defaults and other possible settings.
524 tptr1 = xpstrdup(g.override_pdm);
528 * See if the print context says which pdm to run.
530 g.xerrno = 0; /* Error Handler */
531 lxerrno = 0; /* XIO Error Handler */
533 if ( setjmp( xio_quickie_jmp_buf ) == 0 ) {
534 XSetIOErrorHandler( xio_quickie_handler );
536 if ( rec->seldpy_as_printdpy ) {
537 tptr1 = XpGetOneAttribute( rec->selection_display,
539 XPPrinterAttr, "dt-pdm-command" );
542 tdpy = XOpenDisplay( rec->print_display_str );
544 tptr1 = XpGetOneAttribute( tdpy,
546 XPPrinterAttr, "dt-pdm-command" );
547 XCloseDisplay( tdpy );
551 XSetIOErrorHandler( NULL );
556 XSetIOErrorHandler( NULL );
560 * See if we got a useful pdm exec string. Use
563 if ( g.xerrno || lxerrno ) {
564 rec->pdm_exec_errorcode = g.pdm_start_error;
568 tptr1 = xpstrdup(g.default_pdm);
570 else if (!tptr1[0]) {
571 tptr1 = xpstrdup(g.default_pdm);
576 * Convert pdm-command into argv[] style array.
578 * Note: this parsing code does NOT respect shell
579 * quotes and other items. --tomg
581 rec->pdm_exec_argvs = (char **) NULL;
583 tptr2 = tptr1; /* retain orig pointer for freeing */
588 tptr3 = xpstrtok( tptr2, " \n\t" );
593 * There were NO useful tokens to begin with, so
594 * we'll have to fall back on the default.
596 xp_add_argv( &(rec->pdm_exec_argvs), xpstrdup( g.default_pdm ));
601 tptr3 = xpstrtok( (char *) NULL, " \n\t" );
605 xp_add_argv( &(rec->pdm_exec_argvs), xpstrdup( tptr3 ) );
614 * Add standard command line parameters.
616 xp_add_argv( &(rec->pdm_exec_argvs), xpstrdup("-display") );
617 xp_add_argv( &(rec->pdm_exec_argvs), xpstrdup(rec->video_display_str) );
619 xp_add_argv( &(rec->pdm_exec_argvs), xpstrdup("-window") );
620 sprintf( tstr, "0x%lx", rec->video_window );
621 xp_add_argv( &(rec->pdm_exec_argvs), xpstrdup(tstr) );
623 xp_add_argv( &(rec->pdm_exec_argvs), xpstrdup("-pdisplay") );
624 xp_add_argv( &(rec->pdm_exec_argvs), xpstrdup(rec->print_display_str) );
626 xp_add_argv( &(rec->pdm_exec_argvs), xpstrdup("-pwindow") );
627 sprintf( tstr, "0x%lx", rec->print_window );
628 xp_add_argv( &(rec->pdm_exec_argvs), xpstrdup(tstr) );
630 xp_add_argv( &(rec->pdm_exec_argvs), xpstrdup("-pcontext") );
631 sprintf( tstr, "0x%lx", rec->print_context );
632 xp_add_argv( &(rec->pdm_exec_argvs), xpstrdup(tstr) );
635 /********************************************************************
637 * Once a pdm has been lauched, reply to the SelectionRequest so that
638 * the requestors knows.
640 void mgr_launch_reply( XpPdmServiceRec *rec )
651 XChangeProperty( rec->selection_display, rec->requestor,
652 rec->prop_atom, XA_ATOM,
654 (unsigned char *) &(rec->pdm_exec_errorcode),
658 * Write optional error message to log file.
660 * Expected errors like PXAUTH and VXAUTH should not have
661 * textual descriptions too - they're obvious as is. Only
662 * real nasty errors should have a message for the log file.
664 if ((rec->pdm_exec_errormessage) && (g.log_file)) {
665 if ((errlog = fopen(g.log_file, "a+")) != NULL) {
666 now = time((time_t)0);
668 if ( rec->pdm_exec_errorcode == g.pdm_start_ok )
669 eec = "PDM_START_OK";
670 else if ( rec->pdm_exec_errorcode == g.pdm_start_vxauth )
671 eec = "PDM_START_VXAUTH";
672 else if ( rec->pdm_exec_errorcode == g.pdm_start_pxauth )
673 eec = "PDM_START_PXAUTH";
674 else if ( rec->pdm_exec_errorcode == g.pdm_start_error )
675 eec = "PDM_START_ERROR";
677 eec = "unknown error";
679 fprintf( errlog, PDMD_MSG_11, g.prog_name, ctime(&now),
680 rec->pdm_exec_errormessage,
682 rec->pdm_exec_argvs[0],
683 rec->print_display_str,
684 rec->video_display_str );
692 * Send a SelectionNotify event, which will conclude the
693 * selection handshake.
695 reply.xselection.type = SelectionNotify;
696 reply.xselection.requestor = rec->requestor;
697 reply.xselection.selection = rec->selection;
698 reply.xselection.target = g.pdm_start;
699 reply.xselection.property = rec->prop_atom;
700 reply.xselection.time = rec->time;
702 status = XSendEvent( rec->selection_display, rec->requestor, True, 0, &reply );
705 /********************************************************************
707 * Send the final OK/CANCEL ClientMessage.
709 void mgr_shutdown_reply( XpPdmServiceRec *rec )
720 * Setup client message event.
722 cme.xclient.type = ClientMessage;
723 cme.xclient.window = rec->print_window;
724 cme.xclient.message_type = g.pdm_reply;
725 cme.xclient.format = 32;
728 * Look at the current exit code. Let 'mess' be both a message
729 * string, and a string we would XInternAtom on if we find out
730 * that the "Print" X-Server is differnent than the "Selection"
731 * X-Server. Up till now, we've been using atom constants
732 * generated via the Selection X-Server.
734 switch (rec->exit_code) {
736 cme.xclient.data.l[0] = (long) g.pdm_exit_ok;
737 mess = "PDM_EXIT_OK";
739 case PDM_EXIT_CANCEL:
740 cme.xclient.data.l[0] = (long) g.pdm_exit_cancel;
741 mess = "PDM_EXIT_CANCEL";
743 case PDM_EXIT_VXAUTH:
744 cme.xclient.data.l[0] = (long) g.pdm_exit_vxauth;
745 mess = "PDM_EXIT_VXAUTH";
747 case PDM_EXIT_PXAUTH:
748 cme.xclient.data.l[0] = (long) g.pdm_exit_pxauth;
749 mess = "PDM_EXIT_PXAUTH";
753 cme.xclient.data.l[0] = (long) g.pdm_exit_error;
754 mess = "PDM_EXIT_ERROR";
759 * Try to send ClientMessage that will carry the reply.
762 g.xerrno = 0; /* Error Handler */
763 lxerrno = 0; /* XIO Error Handler */
765 if ( setjmp( xio_quickie_jmp_buf ) == 0 ) {
766 XSetIOErrorHandler( xio_quickie_handler );
768 if ( rec->seldpy_as_printdpy ) {
770 * Since the "Print" X-Server is the same as the
771 * "Selection" X-Server, against which we have an
772 * active display connection and atom values, go
775 XSendEvent( rec->selection_display, rec->print_window, False, 0L, &cme );
778 pdpy = XOpenDisplay( rec->print_display_str );
781 * The "Print" X-Server is different than the
782 * "Selection" X-Server. Map values over.
784 cme.xclient.message_type =
785 XInternAtom( pdpy, "PDM_REPLY", False );
786 cme.xclient.data.l[0] =
787 (long) XInternAtom( pdpy, mess, False );
788 XSendEvent( pdpy, rec->print_window, False, 0L, &cme );
789 XCloseDisplay( pdpy );
793 XSetIOErrorHandler( NULL );
798 XSetIOErrorHandler( NULL );
801 if ( g.xerrno || lxerrno ) {
803 * Problem - we can't get back to the requestor.
805 * This is really a PANIC situation, since the requesting
806 * client will hang around forever, waiting for this
807 * final reply. The best we can do it log an error for
808 * the sys admin, and hope they can notice.
811 sprintf( buf, PDMD_MSG_12, g.prog_name, mess );
813 sprintf( buf, PDMD_MSG_13, g.prog_name, mess );
815 rec->message_string2 = Xmalloc( strlen( buf ) + 1 );
816 strcpy( rec->message_string2 , buf );
821 Bool has_exec_token( XpPdmServiceRec *rec )
826 s1 = rec->message_string;
830 * Look for "PDM_START_*" tokens burried in the
831 * stderr output of the PDM. If found, react as
832 * required, and eliminate the token from the
835 if ( s2 = strstr( s1, "PDM_START_OK") ) {
836 rec->pdm_exec_errorcode = g.pdm_start_ok;
839 else if ( s2 = strstr( s1, "PDM_START_VXAUTH") ) {
840 rec->pdm_exec_errorcode = g.pdm_start_vxauth;
843 else if ( s2 = strstr( s1, "PDM_START_PXAUTH") ) {
844 rec->pdm_exec_errorcode = g.pdm_start_pxauth;
847 else if ( s2 = strstr( s1, "PDM_START_ERROR") ) {
848 rec->pdm_exec_errorcode = g.pdm_start_error;
854 * Compress out the token.
857 while ( *s2++ = *s3++ );
859 if ( strlen(s1) == 0 ) {
861 * The token was it - free the buffer now so that
862 * it appears no stderr was ever generated.
864 Xfree( rec->message_string );
865 rec->message_string = (char *) NULL;
867 else if ((strlen(s1) == 1) && (s1[0] == '\n')) {
869 * All but a \n remains - free the buffer.
871 Xfree( rec->message_string );
872 rec->message_string = (char *) NULL;
886 /********************************************************************
888 * Search through all the child tracking records and see if
889 * any can be shutdown.
891 void mgr_shutdown_scan()
896 static int errlog_problem_notice = 0;
899 for ( i = 0; i < g.serviceRecNum; i++ ) {
901 shutdown_time = False;
903 if ( (g.serviceRecs[i]->do_launch_reply) &&
904 (has_exec_token(g.serviceRecs[i]) ) ) {
906 * Need to send our PDM_START fork/exec reply still.
908 * Note that if we send pdm_start_ok, we'll need to send
909 * a pdm_exit_* code later. If we send pdm_start_vxauth,
910 * pdm_start_pxauth, or pdm_start_error, it indicates to
911 * the client a fatal condition, and they will NOT receive
912 * any more messages, including any pdm_exit_* codes. The
913 * dtpdmd will hang around however, if for no other reason
914 * than to capture stderr and log it when the child finally
917 mgr_launch_reply( g.serviceRecs[i] );
918 g.serviceRecs[i]->do_launch_reply = False; /* it has been done */
921 if ( (g.serviceRecs[i]->pdm_exec_errorcode == g.pdm_start_ok) &&
922 (g.serviceRecs[i]->exit_received) &&
923 (g.serviceRecs[i]->message_pipe[0] == -1) ) {
925 * Child is down, and since we sent a pdm_start_ok,
926 * we need to send a PDM_REPLY pdm_exit_* code too.
928 mgr_shutdown_reply( g.serviceRecs[i] );
931 if ( (g.serviceRecs[i]->exit_received) &&
932 (g.serviceRecs[i]->message_pipe[0] == -1) ) {
934 * Child is down. Log any final error messages
935 * from the child, and from the dtpdmd on behalf of
938 if ( ((g.serviceRecs[i]->message_string) ||
939 (g.serviceRecs[i]->message_string2)) && (g.log_file) ) {
940 if ((errlog = fopen(g.log_file, "a+")) != NULL) {
941 now = time((time_t)0);
943 fprintf (errlog, PDMD_MSG_14, g.prog_name, ctime(&now),
944 g.serviceRecs[i]->pdm_exec_argvs[0],
945 g.serviceRecs[i]->print_display_str,
946 g.serviceRecs[i]->video_display_str,
947 g.serviceRecs[i]->exit_code,
948 g.serviceRecs[i]->message_string);
950 if (g.serviceRecs[i]->message_string2) {
951 fprintf (errlog, PDMD_MSG_15, g.serviceRecs[i]->message_string2);
954 fprintf (errlog, "\n");
957 else if (!errlog_problem_notice) {
958 fprintf (stderr, PDMD_MSG_16, g.prog_name, g.log_file );
960 errlog_problem_notice = 1;
965 * The child is done and can be cleaned up.
967 delete_rec( g.serviceRecs[i] );