Exemple #1
0
int
main_loop (void)
{
    fd_set readfds;
    int rc, numfds, sock, kkk;

    rl_callback_handler_install (get_prompt (), rl_read_cli);

    sock = GET_FILE_DESCRIPTOR (&main_sock);

    do {
        numfds = -1;
        FD_ZERO (&readfds);

        kkk = 0;			/* stdin for commands */
        FD_SET (kkk, &readfds);
        if (kkk > numfds)
            numfds = kkk;

        FD_SET (sock, &readfds);
        if (sock > numfds)
            numfds = sock;

        if (numfds < 0)
            numfds = 0;
        else
            numfds++;

        rc = select (numfds, &readfds, NULL, NULL, NULL);
        if (rc < 0) {		// Error
            if (EINTR == errno)
                continue;		// don't break
            printf ("FATAL_MODE:select failed: %s\n", strerror (errno));
            return -2;
        }

        if (FD_ISSET (0, &readfds)) {
            rl_callback_read_char ();
        }

        if (FD_ISSET (sock, &readfds)) {
            shutdown_flag |= read_uid ();
        }

    } while (!shutdown_flag);
    return 0;
}
Exemple #2
0
int main_loop(void)
{
	fd_set readfds;
	struct timeval tv;
	struct timeval now, earliest;
	int rc, numfds, sock, kkk;

	sock = GET_FILE_DESCRIPTOR(&uid_socket);

	gettimeofday(&earliest, NULL);
	earliest.tv_sec++;

	do {
		numfds = -1;
		FD_ZERO(&readfds);

		kkk = 0; /* stdin for commands */
		FD_SET(kkk, &readfds);
		if (kkk > numfds) {
			numfds = kkk;
		}

		FD_SET(sock, &readfds);
		if (sock > numfds) {
			numfds = sock;
		}

		if (numfds < 0) {
			numfds = 0;
		} else {
			numfds++;
		}

		gettimeofday(&now, 0);
		tv.tv_usec = 0;
		tv.tv_sec = 0;

		if (now.tv_sec < earliest.tv_sec) { /* we must wait more than 1 sec. */
			tv.tv_sec = 1;
			tv.tv_usec = 0;
		} else if (now.tv_sec == earliest.tv_sec) {
			if (now.tv_usec < earliest.tv_usec) {
				if (earliest.tv_usec < now.tv_usec)
					tv.tv_usec = 0;
				else
					tv.tv_usec = earliest.tv_usec
					                - now.tv_usec;
			}
		}

		//printf("wait %ld-%ld\n", (long)tv.tv_sec, (long)tv.tv_usec);
		rc = select(numfds, &readfds, NULL, NULL, &tv);
		if (rc < 0) { // Error
			if (EINTR == errno)
				continue; // don't break 
			printf("FATAL_MODE:select failed: %s\n",
			       strerror(errno));
			return -2;
		}

		if (!rc) { // Timeout expired
			STP_IN_one_second();
			gettimeofday(&earliest, NULL);
			//printf("tick %ld-%ld\n", (long)earliest.tv_sec - 1005042800L, (long)earliest.tv_usec);

			earliest.tv_sec++;
			continue;
		}

		if (FD_ISSET(0, &readfds)) {
			rl_callback_read_char();
		}

		if (FD_ISSET(sock, &readfds)) {
			shutdown_flag |= read_uid(&uid_socket);
		}

	} while (!shutdown_flag);
	
	return 0;
}