Esempio n. 1
0
/***************************************************************************
 *                                                                         *
 * Routine:   ConvertDropName                                              *
 *                                                                         *
 * Purpose:  Convert the "object" received from bms to a full path name    *
 *           note:  I am making BIG assumptions about the format of the    *
 *                  file I am getting from dtfile. "<host> - <path>"      *
 * WARNING:  I have used an Xe function directly (XeIsLocalHostP), rather  *
 *           than include Dt/Connect.h, which was causing bad things to    *
 *           happen at build time, probably because dticon is not ansi-   *
 *           clean (it tried to get c++ version of /usr/include/stdlib.h?) *
 *           It's simply too late to clean up the ansi... (the bell tolls) *
 *                                                                         *
 ***************************************************************************/
static char *
ConvertDropName( char *objects)
{
    char *host;
    char *path;
    char *fullName;
    char *tmp;
    char *netfile;

    host = objects;
    tmp = strchr(objects,' ');
    if (tmp==NULL)      /* shouldn't happen */
      return (strdup(strchr(objects, '/')));

    /* check if same host */
    tmp[0] = '\0';
    if ((Boolean)XeIsLocalHostP(host))
    {
        tmp[0] = ' ';
        return (strdup(strchr(objects, '/')));
    }

    /* different host... get full path name */
    path = tmp+3;      /* skip past the " - " */

    /* Convert to a valid name on the local host. */
    netfile = tt_host_file_netfile(host, path);
    fullName = tt_netfile_file(netfile);
    tt_free(netfile);

    tmp[0] = ' ';      /* put back the " " after host name */
    return (fullName);
}
Esempio n. 2
0
int 
_DtIsOpenableDirContext(
        char *path,
        char **ret_path )
{
   char *real_path = NULL;
   char * tmp_real_path;
   DIR *dirp;
   int ret_status;
   Tt_status status;
   char * host;
   char * filename;
   char * netfile;

   if (ret_path)
      *ret_path = NULL;

   host = _DtHostString(path);
   filename = _DtPathname(path);
   if (host)
   {
      netfile = tt_host_file_netfile(host, filename);
      if ((status = tt_ptr_error(netfile)) == TT_OK)
      {
         tmp_real_path = tt_netfile_file(netfile);
         status = tt_ptr_error(real_path);
         tt_free(netfile);
      }

      if (status != TT_OK) {
         real_path = NULL;
      } else {
	 real_path = XtNewString(tmp_real_path);
	 tt_free(tmp_real_path);
      }

      XtFree(filename);
      XtFree(host);
   }
   else
      real_path = filename;

   if (real_path && ((dirp = opendir (real_path)) != NULL)) 
   {
      closedir (dirp);
      ret_status = 1;
      if (ret_path)
         *ret_path = real_path;
   }
   else
   {
      ret_status = 0;
      if (real_path)
         XtFree(real_path);
   }

   return (ret_status);
}
Esempio n. 3
0
File: TTFile.C Progetto: juddy/edcde
TTFile::TTFile
	(
	const CString & host, 
	const CString & path
	) : CString(), status(TT_OK)
{
    char * temp = tt_host_file_netfile(host.data(), path.data());

    if ((status = tt_ptr_error(temp)) != TT_OK)
	Throw (TT_Exception(temp));

    contents = tt_netfile_file(temp);
    tt_free(temp);

    if ((status = tt_ptr_error(contents)) != TT_OK)
	Throw (TT_Exception(contents));
}
Esempio n. 4
0
static char *get_path_from_context (
	char 		*context)
{
   char			*host = NULL;
   char			*file = NULL;
   char			*netfile = NULL;
   char			*path = NULL;
   char			tmp[MAXPATHLEN];
   char			*pch;

   /*
    * Break context into its host and file parts.
    */
   if (context == NULL)
      return (NULL);

   (void) strcpy (tmp, context);
   file = tmp;

   if ((pch = (char *) strchr (tmp, ':')) != NULL) {
      host = tmp;
      file = pch + 1;
      *pch = '\000';
   }

   if (!host)
      return (strdup (file));

   netfile = (char *) tt_host_file_netfile (host, file);
   if (tt_ptr_error (netfile) != TT_OK) {
      SPC_Error (SPC_Cannot_Create_Netfilename, context, host);
      return (NULL);
   }

   path = (char *) tt_netfile_file (netfile);
   tt_free (netfile);
   if (tt_ptr_error (path) != TT_OK) {
      SPC_Error (SPC_Cannot_Create_Netfilename, context, host);
      return (NULL);
   }

   return (path);
}
Esempio n. 5
0
int
_DtCmdCommandInvokerExecute (
    char *errorMessage,		/* MODIFIED */
    DtSvcMsgContext replyContext,	/* OBSOLETE -- always NULL */
    int  winMask,
    char *contextHost,
    char *contextDir,
    char *contextFile,		/* OBSOLETE -- always NULL */
    char *execParms,
    char *execHost,
    char *execString,
    char *procId,
    char *tmpFiles,
    DtCmdInvExecuteProc success_proc,
    void *success_data,
    DtCmdInvExecuteProc failure_proc,
    void *failure_data)

{
    int ioMode, i, index1;
    int windowType;
    pid_t commandPid;
    char context[MAXPATHLEN];
    char tmpDir [MAXPATHLEN];
    char **commandArray;
    SPC_Channel_Ptr cmdChannel;
    char *theCommand = NULL;
    Boolean terminalRequest = False;
    char *commandArray2[MAX_EXEC_ARGS];
    Boolean localExecution = True;
    Boolean xhostError;
    static unsigned long requestNum = 0;
    char *toolRequest = NULL;	/* backward compatibility kludge */


    myassert( !(contextFile && replyContext) );

    /*
     * Check for a valid window-type.
     * This check is probably redundant but it converts the mask bits into
     * small integer values used by the rest of the command invoker code.
     */
    if ((windowType=
                DtCmdGetWindowType(winMask))== -1)
    {
        (void) sprintf (errorMessage, errorRequest, toolRequest,
                        DtTERMINAL, DtPERM_TERMINAL, DtOUTPUT_ONLY,
                        DtSHARED_OUTPUT, "" /* Obsolete shell window */,
                        DtNO_STDIO);
        return (_CMD_EXECUTE_FATAL);
    }

    /*
     * Create the command to be exec'ed.
     */
    if (windowType == PERM_TERMINAL || windowType == TERMINAL)
    {
        _DtCmdCreateTerminalCommand (&theCommand, windowType, execString,
                                     execParms, execHost, procId, tmpFiles);
        terminalRequest = True;
    }
    else
    {
        /*
         * NO-STDIO || START-SESSION request.
         */

        theCommand = XtMalloc(
                         + strlen (cmd_Resources.dtexecPath)
                         + strlen(" -open ") + 4 /* waitTime len */
                         + strlen(" -ttprocid ") + strlen(_DtActNULL_GUARD(procId))
                         + strlen(_DtActNULL_GUARD(tmpFiles))
                         + strlen (execString)
                         + 5 /* for 2 quotes,2 blanks,null */);

        sprintf(theCommand,"%s -open %d -ttprocid '%s' %s %s",
                cmd_Resources.dtexecPath,
                0 /* wait time zero for NO_STDIO */,
                _DtActNULL_GUARD(procId),
                _DtActNULL_GUARD(tmpFiles),
                execString);

    }

    /*
     * See if the request requires Remote Execution.
     */
    localExecution = _DtIsSameHost(execHost,NULL);

    /*
     * If this is a terminalRequest and the Command Invoker subprocess
     * is not executable, return now.
     */
    if (localExecution && terminalRequest && !cmd_Globals.subprocess_ok)
    {
        if (!(_DtCmdCheckForExecutable (cmd_Resources.dtexecPath)))
        {
            (void) sprintf (errorMessage, cmd_Globals.error_subprocess,
                            cmd_Resources.dtexecPath);
            XtFree ((char *) theCommand);
            return (_CMD_EXECUTE_FAILURE);
        }
        else
            cmd_Globals.subprocess_ok = True;
    }

    /*
     * If this is a terminalRequest and the terminal emulator
     * is not executable, return now.
     */
    if (localExecution && terminalRequest && !cmd_Globals.terminal_ok)
    {
        if (!(_DtCmdCheckForExecutable (cmd_Resources.localTerminal)))
        {
            (void) sprintf (errorMessage, cmd_Globals.error_terminal,
                            cmd_Resources.localTerminal);
            XtFree ((char *) theCommand);
            return (_CMD_EXECUTE_FAILURE);
        }
        else
            cmd_Globals.terminal_ok = True;
    }

    /*
     * Break the command into something execvp or SPCSpawn can handle
     * and then free "theCommand" if this is a termianl-based request.
     */
    commandArray = (char **) XtMalloc (MAX_EXEC_ARGS * sizeof (char *));
    _DtCmdStringToArrayOfStrings (theCommand, commandArray);

    XtFree (theCommand);

    if (!localExecution)
    {
        char *netfile;
        char *argv[4];
        char *tmp;

        /* REMOTE Execution */

        ioMode = SPCIO_NOIO | SPCIO_SYNC_TERMINATOR | SPCIO_FORCE_CONTEXT;

        if ((cmdChannel = (_DtSPCOpen(execHost,
                                      ioMode,
                                      errorMessage))) == SPC_ERROR)
        {
            Cmd_FreeAllocatedStringVector (commandArray);
            return (_CMD_EXECUTE_FAILURE);
        }

        /* Old syntax should no longer appear in contextHost/Dir */
        myassert( (contextHost?*contextHost != '*':1) &&
                  (contextDir?*contextDir != '*':1) );
        /*
         * Create a "netfile" for the cwd to be used.
         */
        netfile = (char *) tt_host_file_netfile (
                      ((contextHost == NULL) ? execHost : contextHost),
                      ((contextDir  == NULL) ? (char *) getenv ("HOME") : contextDir));

        if (tt_pointer_error (netfile) != TT_OK) {
            (void) sprintf (errorMessage, cmd_Globals.error_directory_name_map,
                            ((contextDir  == NULL) ? (char *) getenv ("HOME") : contextDir),
                            ((contextHost == NULL) ? execHost : contextHost),
                            tt_status_message (tt_pointer_error(netfile)));
            Cmd_FreeAllocatedStringVector (commandArray);
            return (_CMD_EXECUTE_FAILURE);
        }
        (void) strcpy (context, netfile);
        tt_free (netfile);

        /*
         * First check to see if the "dtexecPath" is executable on
         * the remote execution host by executing it with no
         * options which will cause it to immediately die.
         *
         * There is no need to set up termination handler for this
         * because we don't care when it dies, we only care about
         * whether or not it can be executed.
         */

        argv[0] = cmd_Resources.dtexecPath;
        argv[1] = (char *) NULL;

        if ((_DtSPCSpawn(argv[0], context, argv, NULL, cmdChannel,
                         execHost, contextHost, contextDir, errorMessage))
                == SPC_ERROR)
        {
            if (DtSPCErrorNumber != SPC_cannot_Chdir &&
                    DtSPCErrorNumber != SPC_Cannot_Fork  &&
                    DtSPCErrorNumber != SPC_Env_Too_Big  &&
                    DtSPCErrorNumber != SPC_Arg_Too_Long)
                /*
                 * The Error message must mention that the dtexec
                 * process is not executable so must overwrite the
                 * error message returned by the Spawn function with
                 * an appropriate message.
                 */
                (void) sprintf (errorMessage, errorRemoteSubprocess, execHost,
                                cmd_Resources.dtexecPath);
            DtSPCClose(cmdChannel);
            Cmd_FreeAllocatedStringVector (commandArray);
            return (_CMD_EXECUTE_FAILURE);
        }

        /* The dtexec process is now known to exist on the remote host */

        /*
         *  Now run a test to see if the command is executable
         *  on this exec host.
         */
        _DtCmdStringToArrayOfStrings (execString, commandArray2);

        tmp = (char *) XtMalloc (strlen (commandArray2[0]) +
                                 strlen ("whence ") + 2);
        (void) sprintf (tmp, "whence %s", commandArray2[0]);

        _DtCmdFreeStringVector (commandArray2);

        argv[0] = "ksh";
        argv[1] = "-c";
        argv[2] = tmp;
        argv[3] = (char *) NULL;

        /*
         * Reopen the channel
         */
        if ((cmdChannel = (_DtSPCOpen(execHost,
                                      ioMode,
                                      errorMessage))) == SPC_ERROR)
        {
            Cmd_FreeAllocatedStringVector (commandArray);
            return (_CMD_EXECUTE_FAILURE);
        }

        /*
         * Set up a callback to be invoked when the test command
         * terminates.
         */
        _DtSvcProcessLock();
        if ((DtSPCRegisterTerminator(cmdChannel,
                                     (SPC_TerminateHandlerType) CheckCommandTerminator,
                                     (void *) ++requestNum)) == SPC_ERROR)
        {
            DtSPCClose(cmdChannel);
            Cmd_FreeAllocatedStringVector (commandArray);
            (void) strcpy (errorMessage, errorSpcTerminator);
            XtFree ((char *) tmp);
            _DtSvcProcessUnlock();
            return (_CMD_EXECUTE_FAILURE);
        }

        if ((_DtSPCSpawn(argv[0], context, argv, NULL, cmdChannel,
                         execHost, contextHost, contextDir, errorMessage))
                == SPC_ERROR)
        {
            DtSPCClose(cmdChannel);
            (void) sprintf (errorMessage, errorRemoteSubprocess, execHost,
                            argv[0]);
            Cmd_FreeAllocatedStringVector (commandArray);
            XtFree ((char *) tmp);
            _DtSvcProcessUnlock();
            return (_CMD_EXECUTE_FAILURE);
        }
        /*
         * The command line checking process has been spawned.
         * There is nothing left to do but to queue the request
         * and return to the client's main loop.  The command
         * line will be executed after the above spawned process
         * terminates.
         */
        QueueRequest (cmdChannel, context, execHost, execString,
                      commandArray, windowType, requestNum, replyContext,
                      success_proc, success_data, failure_proc, failure_data);
        _DtSvcProcessUnlock();
        XtFree(tmp);
        return (_CMD_EXECUTE_QUEUED);
    }
    else
    {
        /* LOCAL Execution */

        /*
         * Must first check to see if the execvp will potentially fail.
         *
         * Since the terminal emulator is pre-appended onto the execution
         * string, don't want to check it (should have been done during
         * startup (in _DtInitializeCommandInvoker)) but must check the
         * execution string that was passed in as part of the message.
         */
        /* Break the command into something execvp can handle */
        _DtCmdStringToArrayOfStrings (execString, commandArray2);

        if (!_DtCmdCheckForExecutable (commandArray2[0]))
        {
            (void) sprintf (errorMessage, errorExec, commandArray2[0]);
            Cmd_FreeAllocatedStringVector (commandArray);
            _DtCmdFreeStringVector (commandArray2);
            return (_CMD_EXECUTE_FAILURE);
        }
        _DtCmdFreeStringVector (commandArray2);

        /*
         * Save the current directory and then "chdir" to the directory
         * to do the execution.  If the chdir fails, return.
         */
        (void) getcwd (tmpDir, MAXPATHLEN);

        if (!_DtCmdValidDir (_cmdClientHost, contextDir, contextHost))
        {
            Cmd_FreeAllocatedStringVector (commandArray);
            (void) sprintf (errorMessage, errorChdir, contextDir, execHost);
            (void) chdir (tmpDir);
            return (_CMD_EXECUTE_FAILURE);
        }

        /*
         * Restore the original environment and remove any DT
         * specific environment variables that were added.
         */
        (void) _DtEnvControl (DT_ENV_RESTORE_PRE_DT);

        /* Fork and then execvp the execution string */
        for (index1 = 0; (index1 < 10) &&
                ((commandPid = fork ()) < 0); index1++)
        {
            /* Out of resources ? */
            if (errno != EAGAIN)
                break;
            /* If not out of resources, sleep and try again */
            (void) sleep ((unsigned long) 2);
        }

        if (commandPid < 0)
        {
            Cmd_FreeAllocatedStringVector (commandArray);
            (void) chdir (tmpDir);
            (void) sprintf(errorMessage, errorFork, execHost);
            (void) _DtEnvControl (DT_ENV_RESTORE_POST_DT);
            return (_CMD_EXECUTE_FAILURE);
        }

        if (commandPid == 0)
        {

#if defined(__hp_osf) || defined(__osf__) || defined(CSRG_BASED)
            setsid() ;
#else
            (void) setpgrp ();
#endif

            if (!terminalRequest )
            {
                int fd;

                /*
                 * Close stdout and redirect it to /dev/null.  If this
                 * is not done and the request writes to stdout, the
                 * output will be queued in an "unlinked" file in
                 * /tmp until the client using this code terminates.
                 */
                if ((fd = open ("/dev/null", O_RDWR)) > 0)
                    (void) dup2 (fd, fileno (stdout));
            }

            /*
             * Mark file descriptiors >=3 as "Close on Exec".
             */
            {
                long	open_max;

                open_max = sysconf(_SC_OPEN_MAX);
                if (open_max == -1)
                {
#ifdef _SUN_OS
                    open_max = NOFILE;
#else
#if defined(USL) || defined(__uxp__) || defined(_AIX)
                    open_max = FOPEN_MAX;
#else
                    open_max = FD_SETSIZE;
#endif
#endif /* _SUN_OS */
                }

                for (i=3; i < open_max; i++)
                    (void) fcntl (i, F_SETFD, 1);
            }


            (void) execvp (commandArray[0], commandArray);

            /* Should never get here, but if you do, must exit */

            /*
             * The following message will be written to the errorlog
             * file if the request is not a terminal requests or
             * to the terminal window if the request requires a
             * terminal.
             */
            (void) sprintf (errorMessage, errorExec, commandArray[0]);
            (void) printf ("%s\n", errorMessage);
            (void) _exit (1);
        }

        /*
         * Restore the pre-fork environment.
         */
        (void) chdir (tmpDir);
        (void) _DtEnvControl (DT_ENV_RESTORE_POST_DT);
    }

    Cmd_FreeAllocatedStringVector (commandArray);

    return (_CMD_EXECUTE_SUCCESS);
}