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: CmdMain.c /main/15 1998/04/20 12:46:37 mgreess $ */
25 * (c) Copyright 1997, The Open Group
27 /***************************************************************************
30 * Description: Command execution system
33 ** (c) Copyright 1993, 1994 Hewlett-Packard Company
34 ** (c) Copyright 1993, 1994 International Business Machines Corp.
35 ** (c) Copyright 1993, 1994 Sun Microsystems, Inc.
36 ** (c) Copyright 1993, 1994 Novell, Inc.
37 ***************************************************************************/
40 #include <Dt/CmdInv.h>
44 #include "/sys5/usr/include/sys/termio.h"
51 #ifdef _SUN_OS /* to get the define for NOFILE */
52 #include <sys/param.h>
54 #define X_INCLUDE_PWD_H
55 #define XOS_USE_XT_LOCKING
56 #include <X11/Xos_r.h>
58 #include <Dt/CommandM.h>
59 #include <Dt/EnvControlP.h>
60 #include <Dt/DtNlUtils.h>
62 #include <Dt/ActionDb.h>
63 #include <Dt/ActionUtilP.h>
64 #include "myassertP.h"
65 #include "DtSvcLock.h"
69 #include <SPC/spc-proto.h>
72 #define MAX_EXEC_ARGS 1000 /* Maximum number of arguments for */
75 * Dtexec return status:
77 #define COMMAND_CHECK_FAILURE 1
81 #define INETD_SECURITY_FILE "/var/adm/inetd.sec"
83 #define INETD_SECURITY_FILE "/usr/adm/inetd.sec"
87 #define Cmd_FreeAllocatedStringVector(sv) \
88 _DtCmdFreeStringVector(sv);\
92 * Global variables for the Command Invoker.
94 static char _cmdClientHost[MAXHOSTNAMELEN];
97 * Static variables for the Command Invoker.
99 static Cmd_RequestQueue *requestQueue = NULL;
102 * Static function declarations:.
105 static void QueueRequest (
106 SPC_Channel_Ptr channel,
112 unsigned long requestNum,
113 DtSvcMsgContext replyContext,
114 DtCmdInvExecuteProc success_proc,
116 DtCmdInvExecuteProc failure_proc,
118 static void ExecuteQueuedRequest (
119 unsigned long requestNum);
121 static void FreeRequest (Cmd_RequestQueue *pNode);
123 static void DtexecTerminator (
124 SPC_Channel_Ptr cmdChannel,
129 static void CheckCommandTerminator (
130 SPC_Channel_Ptr cmdChannel,
135 static int DtCmdGetWindowType(
136 unsigned long windowTypeMask);
137 static void _DtCmdInitializeErrorMessages(void);
140 * Command invocatin error messages.
142 static char *errorExec,
149 *errorRemoteSubprocess,
153 *errorRegisterHandshake,
154 *errorRegisterUsername,
161 /*******************************************************************************
164 * This is a wrapper around DtSPCSpawn (i.e. XeSPCSPawn) that makes sure
165 * the original environment is restored before the spawn and the DT
166 * environment is reinstated after the spawn. It returns the value
167 * originally returned by DtSPCSpawn.
169 ******************************************************************************/
177 SPC_Channel_Ptr chan,
186 * Restore the original environment
188 (void) _DtEnvControl (DT_ENV_RESTORE_PRE_DT);
191 * Map some env var paths to execHost.
193 (void) _DtEnvMapForRemote(execHost);
195 if ((retVal = XeSPCSpawn(path,
201 switch (DtSPCErrorNumber)
203 case SPC_cannot_Chdir:
204 (void) sprintf (errorMessage,
209 case SPC_Cannot_Fork:
210 (void) sprintf (errorMessage,
214 case SPC_Env_Too_Big:
215 case SPC_Arg_Too_Long:
216 (void) sprintf (errorMessage,
224 (void) sprintf (errorMessage,
232 * Restore some env var paths.
234 (void) _DtEnvRestoreLocal();
237 * Restore the DT environment
239 (void) _DtEnvControl (DT_ENV_RESTORE_POST_DT);
242 * Return the result of DtSPCSpawn
247 /*******************************************************************************
250 * This is a wrapper around DtSPCOpen (i.e. XeSPCOpen) that makes sure
251 * the original environment is restored before the spawn and the DT
252 * environment is reinstated after the spawn. It returns the value
253 * originally returned by DtSPCOpen.
255 ******************************************************************************/
262 SPC_Channel_Ptr chan;
263 _Xgetpwparams pwd_buf;
264 struct passwd * pwd_ret;
267 * Restore the original environment
269 (void) _DtEnvControl (DT_ENV_RESTORE_PRE_DT);
272 * Map some env var paths to execHost.
274 (void) _DtEnvMapForRemote(hostname);
276 if ((chan = XeSPCOpen(hostname, iomode)) == SPC_ERROR)
281 switch (DtSPCErrorNumber)
283 case SPC_Unknown_Host:
284 (void) sprintf (errorMessage,
288 case SPC_Bad_Connect:
289 (void) sprintf (errorMessage,
295 case SPC_Bad_Service:
296 (void) sprintf (errorMessage,
301 case SPC_Register_Handshake:
303 if((pwd_ret = _XGetpwuid(this_uid, pwd_buf)) ==
307 username = pwd_ret->pw_name;
308 (void) sprintf (errorMessage,
309 errorRegisterHandshake,
316 case SPC_Register_Username:
318 if((pwd_ret = _XGetpwuid(this_uid, pwd_buf)) ==
322 username = pwd_ret->pw_name;
323 (void) sprintf (errorMessage,
324 errorRegisterUsername,
328 case SPC_Register_Netrc:
329 (void) sprintf (errorMessage,
333 case SPC_Env_Too_Big:
334 (void) sprintf (errorMessage,
339 case SPC_Connection_EOF:
341 (void) sprintf (errorMessage,
352 (void) sprintf (errorMessage,
363 (void) sprintf (errorMessage,
370 * Restore some env var paths.
372 (void) _DtEnvRestoreLocal();
375 * Restore the DT environment
377 (void) _DtEnvControl (DT_ENV_RESTORE_POST_DT);
383 /******************************************************************************
385 * QueueRequest - takes the "state" from a request and puts it on the
390 * SPC_Channel_Ptr channel; - Spcd channel id.
391 * char *context; - Context for SPCSpawn.
392 * char *execHost; - The execution host.
393 * char *execString; - The execution string.
394 * char **argv; - Arg vector for SPCSpawn.
395 * (Arg vector is XtFree'd)
396 * int windowType; - window type of queued command.
397 * unsigned long requestNum; - Id number into the queue.
398 * DtSvcMsgContext replyContext; - Reply info.
399 * DtCmdInvExecuteProc success_proc; - Success callback.
400 * void *success_data; - Success client_data.
401 * DtCmdInvExecuteProc failure_proc; - Failure callback.
402 * void *failure_data; - Failure client_data.
406 * Cmd_RequestQueue *requestQueue; - This request to added.
408 *****************************************************************************/
412 SPC_Channel_Ptr channel,
418 unsigned long requestNum,
419 DtSvcMsgContext replyContext,
420 DtCmdInvExecuteProc success_proc,
422 DtCmdInvExecuteProc failure_proc,
426 Cmd_RequestQueue *pNode;
427 Cmd_RequestQueue *pNewNode;
429 pNewNode = (Cmd_RequestQueue *) XtMalloc (sizeof (Cmd_RequestQueue));
431 pNewNode->next = (Cmd_RequestQueue *) NULL;
432 pNewNode->channel = channel;
433 pNewNode->context = XtNewString (context);
434 pNewNode->exec_host = XtNewString (execHost);
435 pNewNode->exec_string = XtNewString (execString);
436 pNewNode->argv = argv;
437 pNewNode->winType = winType;
438 pNewNode->request_num = requestNum;
439 if (replyContext == NULL)
440 pNewNode->replyContext = NULL;
442 pNewNode->replyContext = replyContext;
443 pNewNode->success_proc = success_proc;
444 pNewNode->success_data = success_data;
445 pNewNode->failure_proc = failure_proc;
446 pNewNode->failure_data = failure_data;
448 if (requestQueue == NULL)
450 requestQueue = pNewNode;
455 * Find the End Of the Queue and link in the NewNode.
457 for (pNode = requestQueue; pNode->next != NULL; pNode = pNode->next);
459 pNode->next = pNewNode;
463 /******************************************************************************
465 * ExecuteQueuedRequest - given a key into the requestQueue (requestNum)
466 * find the appropriate request and execute it.
470 * unsigned long requestNum; - Key into the requestQueue.
474 * Cmd_RequestQueue *requestQueue; - The executed request gets
477 *****************************************************************************/
480 ExecuteQueuedRequest (
481 unsigned long requestNum)
484 Boolean success = True;
485 Cmd_RequestQueue *prev = NULL;
486 Cmd_RequestQueue *pNode = requestQueue;
487 unsigned long iomode;
489 for (; pNode != NULL; pNode = pNode->next)
491 if ( pNode->request_num == requestNum )
494 * Pluck pNode out of the queue
497 prev->next = pNode->next;
499 requestQueue = pNode->next;
505 * Move alone to the next node
513 errorMessage = XtMalloc (MAX_BUF_SIZE * sizeof (char));
520 iomode = ( SPCIO_NOIO
521 | SPCIO_SYNC_TERMINATOR
522 | SPCIO_FORCE_CONTEXT );
525 if ((pNode->channel = (_DtSPCOpen(pNode->exec_host,
527 errorMessage))) == SPC_ERROR)
533 if ((_DtSPCSpawn(pNode->argv[0], pNode->context, pNode->argv, NULL,
534 pNode->channel, pNode->exec_host, NULL, NULL,
535 errorMessage)) == SPC_ERROR)
538 if (DtSPCErrorNumber != SPC_Arg_Too_Long)
539 DtSPCClose(pNode->channel);
542 if (success && pNode->success_proc != NULL)
544 if (cmd_Resources.executionHostLogging && pNode->success_data != NULL)
547 data = (CallbackData *) pNode->success_data;
548 (void) sprintf (errorMessage, successHost,
549 data->actionLabel, pNode->exec_host);
550 _DtCmdLogErrorMessage (errorMessage);
553 (*pNode->success_proc) (NULL, pNode->success_data);
557 if (cmd_Resources.executionHostLogging)
559 if (DtSPCErrorNumber == SPC_Arg_Too_Long)
562 char *cmdp; /* pointer to complete command string */
566 * The message should include all of the command because
567 * the problem may be with some on the internally generated
568 * parts of the command (e.g. the terminal emulator and args).
569 * This means going through all of argv to determine the
570 * length of the string.
572 for (cmdlen = 0, i = 0; pNode->argv[i]; i++) {
573 cmdlen+=strlen(pNode->argv[i]);
574 cmdlen+=2; /* make room for a space + string terminator */
577 tmp_message = (char *) XtMalloc (strlen (errorSpawn) +
578 strlen (pNode->exec_host) +
580 cmdp = (char *) XtMalloc(cmdlen + 1);
582 for (i = 0; pNode->argv[i]; i++) {
583 strcat(cmdp,pNode->argv[i]);
586 (void) sprintf (tmp_message, errorSpawn, pNode->exec_host, cmdp);
587 _DtCmdLogErrorMessage (tmp_message);
592 if (pNode->failure_proc != NULL)
593 (*pNode->failure_proc) (errorMessage, pNode->failure_data);
596 XtFree ((char *)errorMessage);
601 _DtCmdGetQueueHead(void)
606 /******************************************************************************
608 * FreeRequest - Frees the malloced data associated with the node.
609 * and frees the node.
613 * Cmd_RequestQueue *pNode
616 *****************************************************************************/
619 FreeRequest (Cmd_RequestQueue *pNode)
625 XtFree (pNode->context);
626 XtFree (pNode->exec_host);
627 XtFree (pNode->exec_string);
628 Cmd_FreeAllocatedStringVector (pNode->argv);
630 XtFree ((char *) pNode);
633 /******************************************************************************
635 * _DtCmdCommandInvokerExecute - executes a request on the specified host.
639 * _CMD_EXECUTE_SUCCESS - successful execution
640 * _CMD_EXECUTE_FAILURE - execution failed
641 * _CMD_EXECUTE_QUEUED - the request was queued
642 * _CMD_EXECUTE_FATAL - the request contains invalid information
645 *****************************************************************************/
648 _DtCmdCommandInvokerExecute (
649 char *errorMessage, /* MODIFIED */
650 DtSvcMsgContext replyContext, /* OBSOLETE -- always NULL */
654 char *contextFile, /* OBSOLETE -- always NULL */
660 DtCmdInvExecuteProc success_proc,
662 DtCmdInvExecuteProc failure_proc,
666 int ioMode, i, index1;
669 char context[MAXPATHLEN];
670 char tmpDir [MAXPATHLEN];
672 SPC_Channel_Ptr cmdChannel;
673 char *theCommand = NULL;
674 Boolean terminalRequest = False;
675 char *commandArray2[MAX_EXEC_ARGS];
676 Boolean localExecution = True;
678 static unsigned long requestNum = 0;
679 char *toolRequest = NULL; /* backward compatibility kludge */
682 myassert( !(contextFile && replyContext) );
685 * Check for a valid window-type.
686 * This check is probably redundant but it converts the mask bits into
687 * small integer values used by the rest of the command invoker code.
690 DtCmdGetWindowType(winMask))== -1)
692 (void) sprintf (errorMessage, errorRequest, toolRequest,
693 DtTERMINAL, DtPERM_TERMINAL, DtOUTPUT_ONLY,
694 DtSHARED_OUTPUT, "" /* Obsolete shell window */,
696 return (_CMD_EXECUTE_FATAL);
700 * Create the command to be exec'ed.
702 if (windowType == PERM_TERMINAL || windowType == TERMINAL)
704 _DtCmdCreateTerminalCommand (&theCommand, windowType, execString,
705 execParms, execHost, procId, tmpFiles);
706 terminalRequest = True;
711 * NO-STDIO || START-SESSION request.
714 theCommand = XtMalloc(
715 + strlen (cmd_Resources.dtexecPath)
716 + strlen(" -open ") + 4 /* waitTime len */
717 + strlen(" -ttprocid ") + strlen(_DtActNULL_GUARD(procId))
718 + strlen(_DtActNULL_GUARD(tmpFiles))
719 + strlen (execString)
720 + 5 /* for 2 quotes,2 blanks,null */);
722 sprintf(theCommand,"%s -open %d -ttprocid '%s' %s %s",
723 cmd_Resources.dtexecPath,
724 0 /* wait time zero for NO_STDIO */,
725 _DtActNULL_GUARD(procId),
726 _DtActNULL_GUARD(tmpFiles),
732 * See if the request requires Remote Execution.
734 localExecution = _DtIsSameHost(execHost,NULL);
737 * If this is a terminalRequest and the Command Invoker subprocess
738 * is not executable, return now.
740 if (localExecution && terminalRequest && !cmd_Globals.subprocess_ok)
742 if (!(_DtCmdCheckForExecutable (cmd_Resources.dtexecPath)))
744 (void) sprintf (errorMessage, cmd_Globals.error_subprocess,
745 cmd_Resources.dtexecPath);
746 XtFree ((char *) theCommand);
747 return (_CMD_EXECUTE_FAILURE);
750 cmd_Globals.subprocess_ok = True;
754 * If this is a terminalRequest and the terminal emulator
755 * is not executable, return now.
757 if (localExecution && terminalRequest && !cmd_Globals.terminal_ok)
759 if (!(_DtCmdCheckForExecutable (cmd_Resources.localTerminal)))
761 (void) sprintf (errorMessage, cmd_Globals.error_terminal,
762 cmd_Resources.localTerminal);
763 XtFree ((char *) theCommand);
764 return (_CMD_EXECUTE_FAILURE);
767 cmd_Globals.terminal_ok = True;
771 * Break the command into something execvp or SPCSpawn can handle
772 * and then free "theCommand" if this is a termianl-based request.
774 commandArray = (char **) XtMalloc (MAX_EXEC_ARGS * sizeof (char *));
775 _DtCmdStringToArrayOfStrings (theCommand, commandArray);
785 /* REMOTE Execution */
787 ioMode = SPCIO_NOIO | SPCIO_SYNC_TERMINATOR | SPCIO_FORCE_CONTEXT;
789 if ((cmdChannel = (_DtSPCOpen(execHost,
791 errorMessage))) == SPC_ERROR)
793 Cmd_FreeAllocatedStringVector (commandArray);
794 return (_CMD_EXECUTE_FAILURE);
797 /* Old syntax should no longer appear in contextHost/Dir */
798 myassert( (contextHost?*contextHost != '*':1) &&
799 (contextDir?*contextDir != '*':1) );
801 * Create a "netfile" for the cwd to be used.
803 netfile = (char *) tt_host_file_netfile (
804 ((contextHost == NULL) ? execHost : contextHost),
805 ((contextDir == NULL) ? (char *) getenv ("HOME") : contextDir));
807 if (tt_pointer_error (netfile) != TT_OK) {
808 (void) sprintf (errorMessage, cmd_Globals.error_directory_name_map,
809 ((contextDir == NULL) ? (char *) getenv ("HOME") : contextDir),
810 ((contextHost == NULL) ? execHost : contextHost),
811 tt_status_message (tt_pointer_error(netfile)));
812 Cmd_FreeAllocatedStringVector (commandArray);
813 return (_CMD_EXECUTE_FAILURE);
815 (void) strcpy (context, netfile);
819 * First check to see if the "dtexecPath" is executable on
820 * the remote execution host by executing it with no
821 * options which will cause it to immediately die.
823 * There is no need to set up termination handler for this
824 * because we don't care when it dies, we only care about
825 * whether or not it can be executed.
828 argv[0] = cmd_Resources.dtexecPath;
829 argv[1] = (char *) NULL;
831 if ((_DtSPCSpawn(argv[0], context, argv, NULL, cmdChannel,
832 execHost, contextHost, contextDir, errorMessage))
835 if (DtSPCErrorNumber != SPC_cannot_Chdir &&
836 DtSPCErrorNumber != SPC_Cannot_Fork &&
837 DtSPCErrorNumber != SPC_Env_Too_Big &&
838 DtSPCErrorNumber != SPC_Arg_Too_Long)
840 * The Error message must mention that the dtexec
841 * process is not executable so must overwrite the
842 * error message returned by the Spawn function with
843 * an appropriate message.
845 (void) sprintf (errorMessage, errorRemoteSubprocess, execHost,
846 cmd_Resources.dtexecPath);
847 DtSPCClose(cmdChannel);
848 Cmd_FreeAllocatedStringVector (commandArray);
849 return (_CMD_EXECUTE_FAILURE);
852 /* The dtexec process is now known to exist on the remote host */
855 * Now run a test to see if the command is executable
858 _DtCmdStringToArrayOfStrings (execString, commandArray2);
860 tmp = (char *) XtMalloc (strlen (commandArray2[0]) +
861 strlen ("whence ") + 2);
862 (void) sprintf (tmp, "whence %s", commandArray2[0]);
864 _DtCmdFreeStringVector (commandArray2);
869 argv[3] = (char *) NULL;
874 if ((cmdChannel = (_DtSPCOpen(execHost,
876 errorMessage))) == SPC_ERROR)
878 Cmd_FreeAllocatedStringVector (commandArray);
879 return (_CMD_EXECUTE_FAILURE);
883 * Set up a callback to be invoked when the test command
887 if ((DtSPCRegisterTerminator(cmdChannel,
888 (SPC_TerminateHandlerType) CheckCommandTerminator,
889 (void *) ++requestNum)) == SPC_ERROR)
891 DtSPCClose(cmdChannel);
892 Cmd_FreeAllocatedStringVector (commandArray);
893 (void) strcpy (errorMessage, errorSpcTerminator);
894 XtFree ((char *) tmp);
895 _DtSvcProcessUnlock();
896 return (_CMD_EXECUTE_FAILURE);
899 if ((_DtSPCSpawn(argv[0], context, argv, NULL, cmdChannel,
900 execHost, contextHost, contextDir, errorMessage))
903 DtSPCClose(cmdChannel);
904 (void) sprintf (errorMessage, errorRemoteSubprocess, execHost,
906 Cmd_FreeAllocatedStringVector (commandArray);
907 XtFree ((char *) tmp);
908 _DtSvcProcessUnlock();
909 return (_CMD_EXECUTE_FAILURE);
912 * The command line checking process has been spawned.
913 * There is nothing left to do but to queue the request
914 * and return to the client's main loop. The command
915 * line will be executed after the above spawned process
918 QueueRequest (cmdChannel, context, execHost, execString,
919 commandArray, windowType, requestNum, replyContext,
920 success_proc, success_data, failure_proc, failure_data);
921 _DtSvcProcessUnlock();
923 return (_CMD_EXECUTE_QUEUED);
927 /* LOCAL Execution */
930 * Must first check to see if the execvp will potentially fail.
932 * Since the terminal emulator is pre-appended onto the execution
933 * string, don't want to check it (should have been done during
934 * startup (in _DtInitializeCommandInvoker)) but must check the
935 * execution string that was passed in as part of the message.
937 /* Break the command into something execvp can handle */
938 _DtCmdStringToArrayOfStrings (execString, commandArray2);
940 if (!_DtCmdCheckForExecutable (commandArray2[0]))
942 (void) sprintf (errorMessage, errorExec, commandArray2[0]);
943 Cmd_FreeAllocatedStringVector (commandArray);
944 _DtCmdFreeStringVector (commandArray2);
945 return (_CMD_EXECUTE_FAILURE);
947 _DtCmdFreeStringVector (commandArray2);
950 * Save the current directory and then "chdir" to the directory
951 * to do the execution. If the chdir fails, return.
953 (void) getcwd (tmpDir, MAXPATHLEN);
955 if (!_DtCmdValidDir (_cmdClientHost, contextDir, contextHost))
957 Cmd_FreeAllocatedStringVector (commandArray);
958 (void) sprintf (errorMessage, errorChdir, contextDir, execHost);
959 (void) chdir (tmpDir);
960 return (_CMD_EXECUTE_FAILURE);
964 * Restore the original environment and remove any DT
965 * specific environment variables that were added.
967 (void) _DtEnvControl (DT_ENV_RESTORE_PRE_DT);
969 /* Fork and then execvp the execution string */
970 for (index1 = 0; (index1 < 10) &&
971 ((commandPid = fork ()) < 0); index1++)
973 /* Out of resources ? */
976 /* If not out of resources, sleep and try again */
977 (void) sleep ((unsigned long) 2);
982 Cmd_FreeAllocatedStringVector (commandArray);
983 (void) chdir (tmpDir);
984 (void) sprintf(errorMessage, errorFork, execHost);
985 (void) _DtEnvControl (DT_ENV_RESTORE_POST_DT);
986 return (_CMD_EXECUTE_FAILURE);
992 #if defined(__hp_osf) || defined(__osf__)
998 if (!terminalRequest )
1003 * Close stdout and redirect it to /dev/null. If this
1004 * is not done and the request writes to stdout, the
1005 * output will be queued in an "unlinked" file in
1006 * /tmp until the client using this code terminates.
1008 if ((fd = open ("/dev/null", O_RDWR)) > 0)
1009 (void) dup2 (fd, fileno (stdout));
1013 * Mark file descriptiors >=3 as "Close on Exec".
1018 open_max = sysconf(_SC_OPEN_MAX);
1024 #if defined(USL) || defined(__uxp__) || defined(_AIX)
1025 open_max = FOPEN_MAX;
1027 open_max = FD_SETSIZE;
1029 #endif /* _SUN_OS */
1032 for (i=3; i < open_max; i++)
1033 (void) fcntl (i, F_SETFD, 1);
1037 (void) execvp (commandArray[0], commandArray);
1039 /* Should never get here, but if you do, must exit */
1042 * The following message will be written to the errorlog
1043 * file if the request is not a terminal requests or
1044 * to the terminal window if the request requires a
1047 (void) sprintf (errorMessage, errorExec, commandArray[0]);
1048 (void) printf ("%s\n", errorMessage);
1053 * Restore the pre-fork environment.
1055 (void) chdir (tmpDir);
1056 (void) _DtEnvControl (DT_ENV_RESTORE_POST_DT);
1059 Cmd_FreeAllocatedStringVector (commandArray);
1061 return (_CMD_EXECUTE_SUCCESS);
1064 /******************************************************************************
1066 * CheckCommandTerminator - this is the SPC termination callback
1067 * that is invoked when the command line checking process terminates.
1069 * When this callback is invoked, the next step is to check the
1070 * exit status of the remote checking process and if the "command_
1071 * line" is executable, execute the QueuedRequest. Otherwise,
1074 * PARAMETERS: This parameters for this callback are those defined by
1075 * the type "SPC_TerminateHandlerType". Most are not used.
1077 *****************************************************************************/
1080 CheckCommandTerminator(
1081 SPC_Channel_Ptr cmdChannel,
1082 int pid, /* NOT USED */
1083 int type, /* NOT USED */
1084 int cause, /* Exit value of the remote process. */
1085 unsigned long requestNum) /* Specifies the request number. */
1088 char errorMessage[MAX_BUF_SIZE];
1089 Cmd_RequestQueue *prev = NULL;
1090 Cmd_RequestQueue *pNode = requestQueue;
1092 DtSPCClose(cmdChannel);
1095 * Must find the node in the queue.
1097 for (; pNode != NULL; pNode = pNode->next)
1099 if ( pNode->request_num == requestNum )
1102 * Pluck pNode out of the queue
1105 prev->next = pNode->next;
1107 requestQueue = pNode->next;
1113 * Move alone to the next node
1122 * Check the exit status of the remote process.
1124 if (cause == COMMAND_CHECK_FAILURE)
1126 if (pNode->failure_proc != NULL)
1128 (void) sprintf (errorMessage, errorSpawn, pNode->exec_host,
1129 pNode->exec_string);
1130 if (cmd_Resources.executionHostLogging)
1131 _DtCmdLogErrorMessage (errorMessage);
1132 (*pNode->failure_proc) (errorMessage, pNode->failure_data);
1135 FreeRequest (pNode);
1141 * Re-queue this node -- we will execute the command
1143 pNode->next = requestQueue;
1144 requestQueue = pNode;
1146 ExecuteQueuedRequest (requestNum);
1149 /******************************************************************************
1151 * DtCmdGetWindowType - given a window-type mask, determine its'
1152 * internal window type number.
1154 * It should not be possible for this to be called with a bogus mask,
1158 * unsigned long mask; - The request window type mask
1160 * RETURNS: The window type if one if found, otherwise "-1".
1162 *****************************************************************************/
1166 unsigned long windowTypeMask)
1168 int winTypeNum = -1;
1171 * Determine the winType number recognized by dtexec
1172 * The expected wintype input here is the value of the
1173 * wintype bits in the action mask. Convert this to the
1174 * simple integers expected by dtexec.
1177 switch ( windowTypeMask )
1179 case _DtAct_NO_STDIO_BIT:
1182 case _DtAct_TERMINAL_BIT:
1183 winTypeNum = TERMINAL;
1185 case _DtAct_PERM_TERM_BIT:
1186 winTypeNum = PERM_TERMINAL;
1189 myassert(0); /* should never get here */
1190 winTypeNum = PERM_TERMINAL;
1198 /******************************************************************************
1200 * _DtActionCommandInvoke
1201 * ----------------------
1202 * This is the primary entry point into the command invoker portion
1203 * of the Dt Services library. Command Actions are routed to this
1204 * entry point. This function is used to invoke both local and remote
1207 * If logging is turned on, the success or failure message is logged.
1208 ******************************************************************************/
1210 _DtActionCommandInvoke(
1219 void (*success_proc)(),
1221 void (*failure_proc)(),
1225 char errorMessage[MAX_BUF_SIZE * 2];
1227 status = _DtCmdCommandInvokerExecute (errorMessage, NULL,
1229 cwdHost, cwdDir, NULL,
1230 termOpts, execHost, execString,
1232 success_proc, success_data,
1233 failure_proc, failure_data);
1236 case _CMD_EXECUTE_SUCCESS :
1237 if (cmd_Resources.executionHostLogging && success_data != NULL)
1240 data = (CallbackData *) success_data;
1241 (void) sprintf (errorMessage, successHost,
1242 data->actionLabel, execHost);
1243 _DtCmdLogErrorMessage (errorMessage);
1245 if (success_proc != NULL)
1246 (*success_proc) (NULL, success_data);
1248 case _CMD_EXECUTE_QUEUED :
1250 * Return for now and when the termination handler
1251 * gets hit, the queued request will be executed.
1256 * _CMD_EXECUTE_FAILURE or _CMD_EXECUTE_FATAL
1258 if (cmd_Resources.executionHostLogging)
1259 _DtCmdLogErrorMessage (errorMessage);
1260 if (failure_proc != NULL)
1261 (*failure_proc) (errorMessage, failure_data);
1265 return (status == _CMD_EXECUTE_QUEUED) ? 1 : 0;
1268 /******************************************************************************
1270 * _DtCommandInvokerExecute -
1272 * This function allows a client to use the DT "Command Invoker"
1273 * Library for its' process execution. This function is intended
1274 * for processes which do not use the Action Library.
1276 * For local execution, the "fork" and "execvp" system calls are
1277 * used. For remote execution, the "SPCD" is used.
1279 * RETURNS: Nothing directly. However, if the command is successfully
1280 * executed, the "success_proc" function is executed; otherwise,
1281 * the "failure_proc" function is executed.
1283 * NOTE: This API is NOT public it is only here for use by the Session
1284 * manager which uses to start up remote clients at session restore
1285 * time (see SmRestore.c).
1287 *****************************************************************************/
1290 _DtCommandInvokerExecute(
1295 char *exec_parameters,
1298 DtCmdInvExecuteProc success_proc,
1300 DtCmdInvExecuteProc failure_proc,
1304 _DtSvcAppLock(cmd_Globals.app_context);
1306 _DtActionCommandInvoke(_DtAct_NO_STDIO_BIT,context_host,
1307 context_dir, exec_string, exec_parameters, exec_host,
1309 success_proc,success_data, failure_proc,failure_data);
1310 _DtSvcAppUnlock(cmd_Globals.app_context);
1314 /******************************************************************************
1316 * _DtInitializeCommandInvoker - initialize some global variables and
1317 * and call the appropriate initialization routines.
1321 * Display *display; - The X server connection.
1323 * char *toolClass; - The client's tool class.
1325 * char *appClass; - The client's application class.
1327 * DtSvcMessageProc reloadDBHandler; - The callback function for handling
1328 * a "RELOAD-TYPES-DB" request. (OBSOLETE)
1330 * XtAppContext appContext; - The client's application context.
1334 * SbInputId (*SbAddInput_hookfn); - Set to _DtCmdSPCAddInputHandler
1336 * SbInputId (*SbRemoveInput_hookfn); - Set to XtRemoveInput
1338 * SbInputId (*SbAddException_hookfn); - Set to _DtCmdSPCAddExceptionHandler
1339 * SbInputId (*SbRemoveException_hookfn); - Set to XtRemoveInput
1341 *****************************************************************************/
1344 _DtInitializeCommandInvoker(
1346 char *toolClass, /* ignored */
1347 char *appClass, /* ignored */
1348 DtSvcMessageProc reloadDBHandler, /* OBSOLETE -- ignored */
1349 XtAppContext appContext)
1351 static int beenCalled = 0;
1353 _DtSvcAppLock(appContext);
1356 * Prevent repeat calls
1358 _DtSvcProcessLock();
1360 _DtSvcProcessUnlock();
1366 cmd_Globals.app_context = appContext;
1368 SbAddInput_hookfn = _DtCmdSPCAddInputHandler;
1370 SbRemoveInput_hookfn = XtRemoveInput;
1372 SbAddException_hookfn = _DtCmdSPCAddExceptionHandler;
1374 SbRemoveException_hookfn = XtRemoveInput;
1376 _DtCmdBuildPathList ();
1378 _DtCmdInitializeErrorMessages ();
1381 * Must get the name of the invoking host, so that requests
1382 * can be checked for remote execution.
1384 if ((DtGetShortHostname(_cmdClientHost, MAXHOSTNAMELEN)) == -1) {
1385 _DtCmdLogErrorMessage ("Cannot determine the local host name.\n");
1388 _DtCmdGetResources (display);
1390 _DtSvcProcessUnlock();
1391 _DtSvcAppUnlock(appContext);
1394 /*****************************************************************************
1396 * _DtCmdInitializeErrorMessages - initializes all of the command invoker's
1401 * MODIFIED: all of the Command Invoker's error messages are initialized.
1403 *****************************************************************************/
1406 _DtCmdInitializeErrorMessages( void )
1410 * Non-Fatal -> Abort the request
1412 errorChdir = strdup (((char *)Dt11GETMESSAGE(3, 2, "An attempt to change to the following directory:\n\n %s\n\nfrom host \"%s\" failed.\n\nCheck the spelling and permissions and make sure the directory exists.")));
1414 errorSpawn = strdup (((char *)Dt11GETMESSAGE(3, 5, "An attempt to execute the following command on host\n\"%s\" failed:\n\n %s\n\nCheck that the program exists, has the correct\npermissions and is executable.")));
1416 errorExec = strdup (((char *)Dt11GETMESSAGE(3, 6, "An attempt to execute the following command failed:\n\n %s\n\nCheck that the program exists, has the correct\npermissions and is executable.")));
1418 cmd_Globals.error_terminal = strdup (((char *)Dt11GETMESSAGE(3, 7, "This action cannot be started because the following\nterminal emulator cannot be executed:\n\n %s\n\nCheck that the program exists, has the correct permissions\nand is executable. This problem may have occurred because the\nprogram is not in your \"PATH\".")));
1420 errorLength = strdup (((char *)Dt11GETMESSAGE(3, 9, "The total number of characters in this action exceeds the limit of \"%d\".\n\nYou may need to break the action into more than one action.")));
1422 errorFork = strdup (((char *)Dt11GETMESSAGE(3, 11, "An attempt to start a new process on host \"%s\" failed.\n\nTo continue, you may need to stop an unneeded process on this host.")));
1424 errorRequest = strdup (((char *)Dt11GETMESSAGE(3, 17, "This action's WINDOW_TYPE \"%s\" is un-recognized.\n\nThe WINDOW_TYPE should be one of the following:\n\n %s, %s, %s,\n %s, %s, or %s\n")));
1426 cmd_Globals.error_subprocess = strdup (((char *)Dt11GETMESSAGE(3, 18, "This action cannot be started because the DT subprocess program:\n\n %s\n\ncannot be executed. Check that the program has the correct\npermissions and is executable.")));
1428 errorRemoteSubprocess = strdup (((char *)Dt11GETMESSAGE(3, 20, "This action cannot be executed on host \"%s\"\nbecause the following required program either\ndoes not exist or it is not executable:\n\n %s\n")));
1431 * The following errors may occur when a SPC
1432 * channel is opened.
1434 errorUnknownHost = XtNewString (((char *)Dt11GETMESSAGE(3, 24, "This action cannot be executed because\nhost \"%s\" cannot be reached.")));
1436 errorBadConnect = XtNewString (((char *)Dt11GETMESSAGE(3, 25, "This action cannot be executed on host \"%s\" because the\n\"%s\" service is not properly configured on this host.")));
1438 errorBadService = XtNewString (((char *)Dt11GETMESSAGE(3, 26, "This action cannot be executed because the \"%s\"\nservice is not configured on host \"%s\".")));
1440 errorRegisterHandshake = XtNewString (((char *)Dt11GETMESSAGE(3, 27, "This action cannot be executed on host \"%s\" because user\n\"%s\" has a user id of \"%d\" on host \"%s\" and this does\nnot match the username and user id on the action\ninvocation host \"%s\".")));
1442 errorRegisterUsername = XtNewString (((char *)Dt11GETMESSAGE(3, 28, "This action cannot be executed on host \"%s\" because\nuser \"%s\" does not have an account on this host.")));
1444 errorRegisterNetrc = XtNewString (((char *)Dt11GETMESSAGE(3, 29, "This action cannot be executed on host \"%s\" because\na pathname to the authentication file cannot be created.")));
1446 errorRegisterOpen = XtNewString (((char *)Dt11GETMESSAGE(3, 30, "This action cannot be executed on host \"%s\" because\nthe authentication file on this host cannot be opened.\n\nThis may be caused by your network home not being\nproperly configured.")));
1448 errorEnvTooBig = XtNewString (((char *)Dt11GETMESSAGE(3, 31, "This action cannot be executed on host \"%s\" because\nthe environment exceeds \"%d\" bytes.")));
1450 errorInetSecurity = XtNewString (((char *)Dt11GETMESSAGE(3, 32, "This action cannot be executed on host \"%s\" because\nhost \"%s\" is not authorized to use the \"%s\" service.\n\nTo fix this, add host \"%s\" to the \"%s\" service\nentry in file \"%s\" on host \"%s\".")));
1453 * Do not post a dialog, write to the error log file only.
1456 errorSpcTerminator = strdup (((char *)Dt11GETMESSAGE(3, 15, "An attempt to register the output log from a remote host failed.\n\nTo continue, you may need to stop an existing process.")));
1458 successHost = strdup (((char *)Dt11GETMESSAGE(3, 21, "The action \"%s\" was successfully executed on host \"%s\".")));
1460 cmd_Globals.error_directory_name_map = strdup (((char *)Dt11GETMESSAGE(3, 22, "The directory \"%s\" on host \"%s\"\ncould not be converted to a network path.\n(%s)")));