Formatting

This commit is contained in:
Glenn L McGrath 2004-02-18 13:12:53 +00:00
parent aac2a19e50
commit 82d42dbb57

View File

@ -830,160 +830,164 @@ extern int inetd_main(int argc, char *argv[])
} }
for (;;) { for (;;) {
int n, ctrl; fd_set readable;
fd_set readable; int ctrl;
int n;
if (nsock == 0) { if (nsock == 0) {
sigprocmask(SIG_BLOCK, &blockmask, NULL); sigprocmask(SIG_BLOCK, &blockmask, NULL);
while (nsock == 0) while (nsock == 0) {
sigsuspend(&emptymask); sigsuspend(&emptymask);
sigprocmask(SIG_SETMASK, &emptymask, NULL);
}
readable = allsock;
if ((n = select(maxsock + 1, &readable, (fd_set *)0,
(fd_set *)0, (struct timeval *)0)) <= 0) {
if (n < 0 && errno != EINTR)
syslog(LOG_WARNING, "select: %m");
sleep(1);
continue;
}
for (sep = servtab; n && sep; sep = sep->se_next)
if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
n--;
if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
/* Fixed AGC */
fcntl(sep->se_fd, F_SETFL, O_NDELAY);
/* --------- */
ctrl = accept(sep->se_fd, NULL, NULL);
fcntl(sep->se_fd, F_SETFL, 0);
if (ctrl < 0) {
if (errno == EINTR || errno == EWOULDBLOCK)
continue;
syslog(LOG_WARNING, "accept (for %s): %m",
sep->se_service);
continue;
} }
} else sigprocmask(SIG_SETMASK, &emptymask, NULL);
ctrl = sep->se_fd; }
sigprocmask(SIG_BLOCK, &blockmask, NULL); readable = allsock;
pid = 0; n = select(maxsock + 1, &readable, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
#ifdef INETD_FEATURE_ENABLED if (n <= 0) {
if (sep->se_bi == 0 || sep->se_bi->bi_fork) if (n < 0 && errno != EINTR) {
#endif syslog(LOG_WARNING, "select: %m");
{ }
if (sep->se_count++ == 0) sleep(1);
(void)gettimeofday(&sep->se_time, (struct timezone *)0); continue;
else if (sep->se_count >= sep->se_max) { }
struct timeval now; for (sep = servtab; n && sep; sep = sep->se_next) {
if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
(void)gettimeofday(&now, (struct timezone *)0); n--;
if (now.tv_sec - sep->se_time.tv_sec > if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
CNT_INTVL) { /* Fixed AGC */
sep->se_time = now; fcntl(sep->se_fd, F_SETFL, O_NDELAY);
sep->se_count = 1; /* --------- */
} else { ctrl = accept(sep->se_fd, NULL, NULL);
syslog(LOG_ERR, fcntl(sep->se_fd, F_SETFL, 0);
"%s/%s server failing (looping), service terminated", if (ctrl < 0) {
sep->se_service, sep->se_proto); if (errno == EINTR || errno == EWOULDBLOCK) {
FD_CLR(sep->se_fd, &allsock); continue;
(void) close(sep->se_fd); }
sep->se_fd = -1; syslog(LOG_WARNING, "accept (for %s): %m",
sep->se_count = 0; sep->se_service);
nsock--; continue;
sigprocmask(SIG_SETMASK, &emptymask,
NULL);
if (!timingout) {
timingout = 1;
alarm(RETRYTIME);
} }
continue; } else {
ctrl = sep->se_fd;
} }
} sigprocmask(SIG_BLOCK, &blockmask, NULL);
pid = fork(); pid = 0;
if (pid < 0) {
syslog(LOG_ERR, "fork: %m");
if (sep->se_socktype == SOCK_STREAM)
close(ctrl);
sigprocmask(SIG_SETMASK, &emptymask, NULL);
sleep(1);
continue;
}
if (pid && sep->se_wait) {
sep->se_wait = pid;
FD_CLR(sep->se_fd, &allsock);
nsock--;
}
}
sigprocmask(SIG_SETMASK, &emptymask, NULL);
if (pid == 0) {
#ifdef INETD_FEATURE_ENABLED #ifdef INETD_FEATURE_ENABLED
if (sep->se_bi) if (sep->se_bi == 0 || sep->se_bi->bi_fork)
(*sep->se_bi->bi_fn)(ctrl, sep);
else
#endif #endif
{ {
struct passwd *pwd = getpwnam(sep->se_user); if (sep->se_count++ == 0) {
if (pwd == NULL) { gettimeofday(&sep->se_time, (struct timezone *)0);
syslog_err_and_discard_dg( }
sep->se_socktype, else if (sep->se_count >= sep->se_max) {
"getpwnam: %s: No such user", struct timeval now;
sep->se_user);
gettimeofday(&now, (struct timezone *)0);
if (now.tv_sec - sep->se_time.tv_sec > CNT_INTVL) {
sep->se_time = now;
sep->se_count = 1;
} else {
syslog(LOG_ERR,
"%s/%s server failing (looping), service terminated",
sep->se_service, sep->se_proto);
FD_CLR(sep->se_fd, &allsock);
close(sep->se_fd);
sep->se_fd = -1;
sep->se_count = 0;
nsock--;
sigprocmask(SIG_SETMASK, &emptymask, NULL);
if (!timingout) {
timingout = 1;
alarm(RETRYTIME);
}
continue;
}
}
pid = fork();
if (pid < 0) {
syslog(LOG_ERR, "fork: %m");
if (sep->se_socktype == SOCK_STREAM) {
close(ctrl);
}
sigprocmask(SIG_SETMASK, &emptymask, NULL);
sleep(1);
continue;
}
if (pid && sep->se_wait) {
sep->se_wait = pid;
FD_CLR(sep->se_fd, &allsock);
nsock--;
}
} }
if (sep->se_group && sigprocmask(SIG_SETMASK, &emptymask, NULL);
(grp = getgrnam(sep->se_group)) == NULL) { if (pid == 0) {
syslog_err_and_discard_dg( #ifdef INETD_FEATURE_ENABLED
sep->se_socktype, if (sep->se_bi) {
"getgrnam: %s: No such group", (*sep->se_bi->bi_fn)(ctrl, sep);
sep->se_group); } else
} #endif
/* {
* Ok. There are four cases here: struct passwd *pwd = getpwnam(sep->se_user);
* 1. nonroot user, no group specified if (pwd == NULL) {
* 2. nonroot user, some group specified syslog_err_and_discard_dg(
* 3. root user, no group specified sep->se_socktype,
* 4. root user, some group specified "getpwnam: %s: No such user",
* In cases 2 and 4 we setgid to the specified sep->se_user);
* group. In cases 1 and 2 we run initgroups }
* to run with the groups of the given user. if (sep->se_group && (grp = getgrnam(sep->se_group)) == NULL) {
* In case 4 we do setgroups to run with the syslog_err_and_discard_dg(sep->se_socktype,
* given group. In case 3 we do nothing. "getgrnam: %s: No such group", sep->se_group);
*/ }
if (pwd->pw_uid) { /*
if (sep->se_group) * Ok. There are four cases here:
pwd->pw_gid = grp->gr_gid; * 1. nonroot user, no group specified
setgid((gid_t)pwd->pw_gid); * 2. nonroot user, some group specified
initgroups(pwd->pw_name, pwd->pw_gid); * 3. root user, no group specified
setuid((uid_t)pwd->pw_uid); * 4. root user, some group specified
} else if (sep->se_group) { * In cases 2 and 4 we setgid to the specified
setgid((gid_t)grp->gr_gid); * group. In cases 1 and 2 we run initgroups
setgroups(1, &grp->gr_gid); * to run with the groups of the given user.
} * In case 4 we do setgroups to run with the
dup2(ctrl, 0); * given group. In case 3 we do nothing.
close(ctrl); */
dup2(0, 1); if (pwd->pw_uid) {
dup2(0, 2); if (sep->se_group) {
pwd->pw_gid = grp->gr_gid;
}
setgid((gid_t)pwd->pw_gid);
initgroups(pwd->pw_name, pwd->pw_gid);
setuid((uid_t)pwd->pw_uid);
} else if (sep->se_group) {
setgid((gid_t)grp->gr_gid);
setgroups(1, &grp->gr_gid);
}
dup2(ctrl, 0);
close(ctrl);
dup2(0, 1);
dup2(0, 2);
#ifdef RLIMIT_NOFILE #ifdef RLIMIT_NOFILE
if (rlim_ofile.rlim_cur != rlim_ofile_cur) { if (rlim_ofile.rlim_cur != rlim_ofile_cur) {
if (setrlimit(RLIMIT_NOFILE, if (setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
&rlim_ofile) < 0) syslog(LOG_ERR,"setrlimit: %m");
syslog(LOG_ERR,"setrlimit: %m"); }
} }
#endif #endif
for (ctrl = rlim_ofile_cur-1; --ctrl > 2; ) for (ctrl = rlim_ofile_cur-1; --ctrl > 2; ) {
(void)close(ctrl); (void)close(ctrl);
}
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_DFL;
sigaction(SIGPIPE, &sa, NULL);
memset(&sa, 0, sizeof(sa)); execv(sep->se_server, sep->se_argv);
sa.sa_handler = SIG_DFL; syslog_err_and_discard_dg(sep->se_socktype, "execv %s: %m", sep->se_server);
sigaction(SIGPIPE, &sa, NULL); }
}
execv(sep->se_server, sep->se_argv); if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
syslog_err_and_discard_dg(sep->se_socktype, close(ctrl);
"execv %s: %m", sep->se_server); }
} }
} }
if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
close(ctrl);
}
} }
} }