* Main idle loop of postmaster * * NB: Needs to be called with signals blocked */
| 1946 | * NB: Needs to be called with signals blocked |
| 1947 | */ |
| 1948 | static int |
| 1949 | ServerLoop(void) |
| 1950 | { |
| 1951 | fd_set readmask; |
| 1952 | int nSockets; |
| 1953 | time_t last_lockfile_recheck_time, |
| 1954 | last_touch_time; |
| 1955 | |
| 1956 | last_lockfile_recheck_time = last_touch_time = time(NULL); |
| 1957 | |
| 1958 | nSockets = initMasks(&readmask); |
| 1959 | |
| 1960 | for (;;) |
| 1961 | { |
| 1962 | fd_set rmask; |
| 1963 | int selres; |
| 1964 | time_t now; |
| 1965 | |
| 1966 | /* |
| 1967 | * Wait for a connection request to arrive. |
| 1968 | * |
| 1969 | * We block all signals except while sleeping. That makes it safe for |
| 1970 | * signal handlers, which again block all signals while executing, to |
| 1971 | * do nontrivial work. |
| 1972 | * |
| 1973 | * If we are in PM_WAIT_DEAD_END state, then we don't want to accept |
| 1974 | * any new connections, so we don't call select(), and just sleep. |
| 1975 | */ |
| 1976 | memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set)); |
| 1977 | |
| 1978 | if (pmState == PM_WAIT_DEAD_END) |
| 1979 | { |
| 1980 | PG_SETMASK(&UnBlockSig); |
| 1981 | |
| 1982 | pg_usleep(100000L); /* 100 msec seems reasonable */ |
| 1983 | selres = 0; |
| 1984 | |
| 1985 | PG_SETMASK(&BlockSig); |
| 1986 | } |
| 1987 | else |
| 1988 | { |
| 1989 | /* must set timeout each time; some OSes change it! */ |
| 1990 | struct timeval timeout; |
| 1991 | |
| 1992 | /* Needs to run with blocked signals! */ |
| 1993 | DetermineSleepTime(&timeout); |
| 1994 | |
| 1995 | PG_SETMASK(&UnBlockSig); |
| 1996 | |
| 1997 | selres = select(nSockets, &rmask, NULL, NULL, &timeout); |
| 1998 | |
| 1999 | PG_SETMASK(&BlockSig); |
| 2000 | } |
| 2001 | |
| 2002 | /* Now check the select() result */ |
| 2003 | if (selres < 0) |
| 2004 | { |
| 2005 | if (errno != EINTR && errno != EWOULDBLOCK) |
no test coverage detected