コード例 #1
0
ファイル: qrsh_starter.c プロジェクト: HPCKP/gridengine
/****** Interactive/qrsh/startJob() ***************************************
*
*  NAME
*     startJob() -- start a shell with commands to execute
*
*  SYNOPSIS
*     static int startJob(char *command, char *wrapper, int noshell);
*
*  FUNCTION
*     Starts the commands and arguments to be executed as 
*     specified in parameter <command>. 
*     If the parameter noshell is set to 1, the command is directly called
*     by exec.
*     If a wrapper is specified (parameter wrapper, set by environment
*     variable QRSH_WRAPPER), this wrapper is called and is passed the 
*     command to execute as commandline parameters.
*     If neither noshell nor wrapper is set, a users login shell is called
*     with the parameters -c <command>.
*     The child process creates an own process group.
*     The pid of the child process is written to a pid file in $TMPDIR.
*
*  INPUTS
*     command - commandline to be executed
*     wrapper - name and path of a wrapper script
*     noshell - if != 0, call the command directly without shell
*
*  RESULT
*     status of the child process after it terminated
*     or EXIT_FAILURE, if the process of starting the child 
*     failed because of one of the following error situations:
*        - fork failed
*        - the pid of the child process cannot be written to pid file
*        - the name of actual user cannot be determined
*        - info about the actual user cannot be determined (getpwnam)
*        - necessary memory cannot be allocated
*        - executing the shell failed
*
*  SEE ALSO
*     Interactive/qrsh/write_pid_file()
*     Interactive/qrsh/split_command()
*     Interactive/qrsh/join_command()
*
****************************************************************************
*/
static int startJob(char *command, char *wrapper, int noshell)
{

   child_pid = fork();
   if(child_pid == -1) {
      qrsh_error(MSG_QRSH_STARTER_CANNOTFORKCHILD_S, strerror(errno));
      return EXIT_FAILURE;
   }

   if(child_pid) {
      /* parent */
      int status;

#if defined(LINUX)
      int ttyfd;
#endif

      signal(SIGINT,  forward_signal);
      signal(SIGQUIT, forward_signal);
      signal(SIGTERM, forward_signal);

      /* preserve pseudo terminal */
#if defined(LINUX)
      ttyfd = open("/dev/tty", O_RDWR);
      if (ttyfd != -1) {
         tcsetpgrp(ttyfd, child_pid);
         close(ttyfd); 
      }
#endif

      while(waitpid(child_pid, &status, 0) != child_pid && errno == EINTR);
      return(status);
   } else {
      /* child */
      char *buffer = NULL;
      int size;
      struct passwd pw_struct;
      char *shell    = NULL;
      char *userName = NULL;
      int    argc = 0;
      const char **args = NULL;
      char *cmd = NULL;
      int cmdargc;
      char **cmdargs = NULL;
      int i;

      if(!write_pid_file(getpid())) {
         exit(EXIT_FAILURE);
      }

      cmdargc = split_command(command, &cmdargs);

      if(cmdargc == 0) {
         qrsh_error(MSG_QRSH_STARTER_INVALIDCOMMAND);
         exit(EXIT_FAILURE);
      }

      if(!noshell) {
         struct passwd *pw = NULL;

         if((userName = search_conf_val("job_owner")) == NULL) {
            qrsh_error(MSG_QRSH_STARTER_CANNOTGETLOGIN_S, strerror(errno));
            exit(EXIT_FAILURE);
         }

         size = get_pw_buffer_size();
         buffer = sge_malloc(size);

         if ((pw = sge_getpwnam_r(userName, &pw_struct, buffer, size)) == NULL) {
            qrsh_error(MSG_QRSH_STARTER_CANNOTGETUSERINFO_S, strerror(errno));
            exit(EXIT_FAILURE);
         }
         
         shell = pw->pw_shell;
         
         if(shell == NULL) { 
            qrsh_error(MSG_QRSH_STARTER_CANNOTDETERMSHELL_S, "/bin/sh");
            shell = "/bin/sh";
         } 
      }
     
      if((args = malloc((cmdargc + 3) * sizeof(char *))) == NULL) {
         qrsh_error(MSG_QRSH_STARTER_MALLOCFAILED_S, strerror(errno));
         exit(EXIT_FAILURE);
      }         
    
      if(wrapper == NULL) {
         if(noshell) {
            cmd = cmdargs[0];
            for(i = 0; i < cmdargc; i++) {
               args[argc++] = cmdargs[i];
            }
         } else {
            cmd = shell;
            args[argc++] = sge_basename(shell, '/');
            args[argc++] = "-c";
            args[argc++] = join_command(cmdargc, cmdargs);
         }
      } else {
         cmd = wrapper;
         args[argc++] = sge_basename(wrapper, '/');
         for(i = 0; i < cmdargc; i++) {
            args[argc++] = cmdargs[i];
         }
      }

      args[argc++] = NULL;

#if 0
{
   /* debug code */
   int i;
   
   fflush(stdout) ; fflush(stderr);
   printf("qrsh_starter: executing %s\n", cmd);
   for(i = 1; args[i] != NULL; i++) {
      printf("args[%d] = %s\n", i, args[i]);
   }
   printf("\n");
   fflush(stdout) ; fflush(stderr); 
} 
#endif

      SETPGRP;
      execvp(cmd, (char *const *)args);
      /* exec failed */
      fprintf(stderr, MSG_QRSH_STARTER_EXECCHILDFAILED_S, args[0], strerror(errno));
      fprintf(stderr, "\n");
      exit(EXIT_FAILURE);
   }

   /* will never be reached */
   return EXIT_FAILURE; 
}
コード例 #2
0
ファイル: sircd.c プロジェクト: dawnwords/irc_daemon
void request_check(int index, char* buf){
    char tokens[MAX_MSG_TOKENS][MAX_MSG_LEN+1];
    int arg_num = tokenize(buf,tokens);
    int type,connfd;

    connfd = p.clientfd[index];

    if (!strcmp(tokens[0],"USER"))
        type = USER_CMD;
    else if(!strcmp(tokens[0],"NICK"))
        type = NICK_CMD;
    else if(!strcmp(tokens[0],"JOIN"))
        type = JOIN_CMD;
    else if(!strcmp(tokens[0],"QUIT"))
        type = QUIT_CMD;
    else if(!strcmp(tokens[0],"PART"))
        type = PART_CMD;
    else if(!strcmp(tokens[0],"LIST"))
        type = LIST_CMD;
    else if(!strcmp(tokens[0],"WHO"))
        type = WHO_CMD;
    else if(!strcmp(tokens[0],"PRIVMSG"))
        type = PRIVMSG_CMD;
    else
        type = UNKONWN_CMD;

    //DEBUG
    int i;
    printf(" # CMD:%s\t ARG_NUM:%d\t",tokens[0],arg_num);
    for(i=0;i<4;i++)
        printf(" ARG%d:%s\t",i,tokens[i+1]);
    printf("\n");

    if(type == UNKONWN_CMD)
        unknown_command(connfd,tokens[0]);
    else if(arg_num < ARG_NUM[type]){
        char msg[MAX_MSG_LEN];
        if(type == NICK_CMD)
            /* NICK ERRRO TYPE: ERR_NOERR_NONICKNAMEGIVEN */
            snprintf(msg,MAX_MSG_LEN,":No nickname given\n");
        else if(type == PRIVMSG_CMD){
            /* PRIVMSG ERRRO TYPE: ERR_NORECIPIENT */
            if(!arg_num)
                snprintf(msg,MAX_MSG_LEN,":No recipient given PRIVMSG\n");
            /* PRIVMSG ERRRO TYPE: ERR_NOTEXTTOSEND */
            else
                snprintf(msg,MAX_MSG_LEN,":No text to send\n");
        }else
            snprintf(msg,MAX_MSG_LEN,"%s:Not enough parameters\n", tokens[0]);
        send_msg_back(connfd,msg);
    }
    else
        switch(type){
            case QUIT_CMD:
                quit_command(index);
                break;
            case USER_CMD:
                user_command(connfd,tokens[1],tokens[4]);               
                break;
            case NICK_CMD:
                nick_command(connfd,tokens[1]);
                break;
            default:
                if(check_register(connfd)){
                    switch(type){
                        case JOIN_CMD:
                            join_command(connfd,tokens[1]);
                            break;
                        case PART_CMD:
                            part_command(connfd,tokens[1],1);
                            break;
                        case LIST_CMD:
                            list_command(connfd);
                            break;
                        case WHO_CMD:
                            who_command(connfd,tokens[1]);
                            break;
                        case PRIVMSG_CMD:
                            privmsg_command(connfd,tokens[1],tokens[2]);
                    }
                }else
                    send_msg_back(connfd,":You have not registered\n");
        }
}