Exemplo n.º 1
0
int
main(int argc, char **argv)
{
    static char fname[] = "res/main";
    int nready;
    int maxfd;
    int i;
    char *sp;
    char *pathname = NULL;
    int didSomething = 0;
    char exbuf;

    time_t thisPeriodic, lastPeriodic = 0, timeDiff;


    fd_set rm, wm, em;

    int sbdPty = FALSE;
    char *sbdClHost = NULL;
    ushort sbdClPort = 0;
    char **sbdArgv = NULL;
    int selectError = 0;


    _i18n_init(I18N_CAT_RES);

    saveDaemonDir_(argv[0]);



    for (i=1; i<argc; i++) {
      if (strcmp(argv[i], "-d") == 0 && argv[i+1] != NULL) {
         pathname = argv[i+1];
         putEnv("LSF_ENVDIR",pathname);
         break;
      }
    }

    if (pathname == NULL) {
	if ((pathname = getenv("LSF_ENVDIR")) == NULL)
	    pathname = LSETCDIR;
    }


    if (argc > 1) {
        if (!strcmp(argv[1],"-V")) {
            fputs(_LS_VERSION_, stderr);
            exit(0);
        }
    }



    if ((ls_readconfenv(resConfParams, NULL) < 0) ||
        (initenv_(resParams, pathname) < 0) ) {
        if ((sp = getenv("LSF_LOGDIR")) != NULL)
            resParams[LSF_LOGDIR].paramValue = sp;
        ls_openlog("res", resParams[LSF_LOGDIR].paramValue, (debug > 1),
                   resParams[LSF_LOG_MASK].paramValue);
        ls_syslog(LOG_ERR, I18N_FUNC_S_FAIL_MM, fname, "initenv_",
            pathname);
        ls_syslog(LOG_ERR, I18N_Exiting);
        exit(-1);
    }



    restart_argc = argc;
    restart_argv = argv;
    for (i=1; i<argc; i++) {
	if (strcmp(argv[i], "-d") == 0 && argv[i+1] != NULL) {
	    pathname = argv[i+1];
	    i++;
	    continue;
	}

	if (strcmp(argv[i], "-1") == 0) {
	    debug = 1;
	    continue;
	}

	if (strcmp(argv[i], "-2") == 0) {
	    debug = 2;
	    continue;
	}


	if (strcmp(argv[i], "-PTY_FIX") == 0) {
	    printf("PTY_FIX");
	    exit(0);
	}


	if ( (strcmp(argv[i], "-j") == 0) && (argv[i+1] != NULL) ) {
	    lsbJobStarter = argv[++i];
	    continue;
	}

	if (strcmp(argv[i], "-P") == 0) {
	    sbdPty = TRUE;
	    continue;
	}

	if (strcmp(argv[i], "-i") == 0) {
	    sbdFlags |= SBD_FLAG_STDIN;
	    continue;
	}

	if (strcmp(argv[i], "-o") == 0) {
	    sbdFlags |= SBD_FLAG_STDOUT;
	    continue;
	}

	if (strcmp(argv[i], "-e") == 0) {
	    sbdFlags |= SBD_FLAG_STDERR;
	    continue;
	}

	if (strcmp(argv[i], "-m") == 0 && argv[i+1] != NULL) {
	    sbdClHost = argv[i+1];
	    i++;
	    sbdMode = TRUE;
	    continue;
	}

	if (strcmp(argv[i], "-p") == 0 && argv[i+1] != NULL) {
	    sbdClPort = atoi(argv[i+1]);
	    i++;
	    sbdMode = TRUE;
	    continue;
	}


	if (argv[i][0] != '-') {
	    sbdMode = TRUE;
	    sbdArgv = argv + i;
	    break;
	}

        usage(argv[0]);
    }

    if (sbdMode) {

	if (sbdClHost == NULL || sbdArgv == NULL) {
	    usage(argv[0]);
	    exit(-1);
	}
	if (sbdClPort) {
	    sbdFlags |= SBD_FLAG_TERM;
	} else {

	    sbdFlags |= SBD_FLAG_STDIN | SBD_FLAG_STDOUT | SBD_FLAG_STDERR;
	}
    } else {


	if (debug < 2)
	    for (i = sysconf(_SC_OPEN_MAX) ; i >= 0 ; i--)
		close(i);
    }


    if (resParams[LSF_SERVERDIR].paramValue == NULL) {
	ls_openlog("res", resParams[LSF_LOGDIR].paramValue, (debug > 1),
				   resParams[LSF_LOG_MASK].paramValue);
	ls_syslog(LOG_ERR, _i18n_msg_get(ls_catd , NL_SETN, 5001,
	    "LSF_SERVERDIR not defined in %s/lsf.conf: %M; res exiting"), /* catgets 5001 */
	    pathname);
	resExit_(-1);
    }


    if (! debug && resParams[LSF_RES_DEBUG].paramValue != NULL) {
	debug = atoi(resParams[LSF_RES_DEBUG].paramValue);
	if (debug <= 0)
	    debug = 1;
    }


    getLogClass_(resParams[LSF_DEBUG_RES].paramValue,
                 resParams[LSF_TIME_RES].paramValue);


    if (getuid() == 0 && debug) {
        if (sbdMode)  {
	   debug = 0;
	} else {
          ls_openlog("res", resParams[LSF_LOGDIR].paramValue, FALSE,
		   resParams[LSF_LOG_MASK].paramValue);
          ls_syslog(LOG_ERR, I18N(5005,"Root cannot run RES in debug mode ... exiting."));/*catgets 5005 */
	   exit(-1);
	}
    }

    if (debug > 1)
	ls_openlog("res", resParams[LSF_LOGDIR].paramValue, TRUE, "LOG_DEBUG");
    else {
 	ls_openlog("res", resParams[LSF_LOGDIR].paramValue, FALSE,
		   resParams[LSF_LOG_MASK].paramValue);
    }
    if (logclass & (LC_TRACE | LC_HANG))
        ls_syslog(LOG_DEBUG, "%s: logclass=%x", fname, logclass);

    ls_syslog(LOG_DEBUG, "%s: LSF_SERVERDIR=%s", fname, resParams[LSF_SERVERDIR].paramValue);



    init_res();
    initSignals();

    periodic(0);



    if (sbdMode) {
	lsbJobStart(sbdArgv, sbdClPort, sbdClHost, sbdPty);
    }

    maxfd = FD_SETSIZE;


    for (;;) {
        struct timeval *timep;
        struct timeval timeout;
loop:
        didSomething = 0;

        for (i = 0; i < child_cnt; i++) {
            if (children[i]->backClnPtr == NULL
                 && !FD_IS_VALID(conn2NIOS.sock.fd)
                 && children[i]->running == 0) {
                delete_child (children[i]);
            }
        }

	if (logclass & LC_TRACE) {
	    ls_syslog(LOG_DEBUG,"\
%s: %s Res child_res=<%d> child_go=<%d> child_cnt=<%d> client_cnt=<%d>",
		      fname, ((child_res) ? "Application" : "Root") ,
		      child_res, child_go, child_cnt, client_cnt);
            if (child_cnt == 1 && children != NULL && children[0] != NULL) {
                dumpChild(children[0], 1, "in main()");
            }
	}

        if (child_res && child_go && child_cnt == 0 && client_cnt == 0)  {


            if (debug > 1)
		printf (" \n Child <%d> Retired! \n", (int)getpid());

	    if (logclass & LC_TRACE) {
		ls_syslog(LOG_DEBUG,"\
%s: Application Res is exiting.....", fname);
	    }


	    millisleep_(5000);


	    if (sbdMode) {

		close(1);
		close(2);
		exit(lastChildExitStatus);
            }
	    resExit_(EXIT_NO_ERROR);
        }
Exemplo n.º 2
0
/**
 * @brief Function for accepting password input from users
 *
 * The functions reads chars from a buffered stream and store them in a buffer of
 * chars. If a file descriptor is supplied then, the password is read from
 * the associated stream, otherwise a new buffered stream is created and a
 * prompt is displayed to the user.
 *
 * @param prompt String displayed on the terminal to prompt the user for a
 *               password or an encryption key
 * @param fd     File descriptor to use to read the pasword from. If fd is set
 *               to FD_INVALID, then a new stream is opened.
 *
 * @return NULL if a problem occured or the user killed the terminal (Ctrl-C)\n
 *         otherwise the password - empty password is accepted.
 */
char*
getpasswd(const char *prompt, int fd)
{
    char *ptr;
    
#ifndef WIN32
    sigset_t        sig, old_sig;
    struct termios  ts, old_ts;
    FILE           *fp;

    /* If a valid file descriptor is supplied, we try to open a stream from it */
    if (FD_IS_VALID(fd))
    {
        fp = fdopen(fd, "r");
        if (fp == NULL)
        {
            log_msg(LOG_VERBOSITY_ERROR, "getpasswd() - "
                "Unable to create a stream from the file descriptor : %s",
                strerror(errno));
            exit(EXIT_FAILURE);
        }
    }

    /* Otherwise we are going to open a new stream */
    else
    {
        if((fp = fopen(ctermid(NULL), "r+")) == NULL)
            return(NULL);

        setbuf(fp, NULL);

        /* Setup blocks for SIGINT and SIGTSTP and save the original signal
        * mask.
        */
        sigemptyset(&sig);
        sigaddset(&sig, SIGINT);
        sigaddset(&sig, SIGTSTP);
        sigprocmask(SIG_BLOCK, &sig, &old_sig);

        /*
        * Save current tty state for later restoration after we :
        *   - disable echo of characters to the tty
        *   - disable signal generation
        *   - disable cannonical mode (input read line by line mode)
        */
        tcgetattr(fileno(fp), &ts);
        old_ts = ts;
        ts.c_lflag &= ~(ECHO | ICANON | ISIG);
        tcsetattr(fileno(fp), TCSAFLUSH, &ts);

        fputs(prompt, fp);
    }
#else
    _cputs(prompt);
#endif

    /* Read the password */
    ptr = read_passwd_from_stream(fp);

#ifndef WIN32

    /* If we used a new buffered stream */
    if (FD_IS_VALID(fd) == 0)
    {
        /* we can go ahead and echo out a newline.
        */
        putc(PW_LF_CHAR, fp);

        /* Restore our tty state and signal handlers.
        */
        tcsetattr(fileno(fp), TCSAFLUSH, &old_ts);
        sigprocmask(SIG_BLOCK, &old_sig, NULL);
    }

    fclose(fp);
#else
    /* In Windows, it would be a CR-LF
     */
    _putch(PW_CR_CHAR);
    _putch(PW_LF_CHAR);
#endif

    return (ptr);
}