inetd: deindent main loop, other readability enhancements

This commit is contained in:
Denis Vlasenko 2006-09-23 15:58:01 +00:00
parent 4c196a80fc
commit c1876d7364

View File

@ -183,8 +183,7 @@ static struct rlimit rlim_ofile;
# define INETD_SETPROCTITLE # define INETD_SETPROCTITLE
#endif #endif
typedef struct servtab typedef struct servtab {
{
char *se_hostaddr; /* host address to listen on */ char *se_hostaddr; /* host address to listen on */
char *se_service; /* name of service */ char *se_service; /* name of service */
int se_socktype; /* type of socket to use */ int se_socktype; /* type of socket to use */
@ -209,8 +208,7 @@ typedef struct servtab
#define MAXARGV 20 #define MAXARGV 20
char *se_argv[MAXARGV + 1]; /* program arguments */ char *se_argv[MAXARGV + 1]; /* program arguments */
int se_fd; /* open descriptor */ int se_fd; /* open descriptor */
union union {
{
struct sockaddr se_un_ctrladdr; struct sockaddr se_un_ctrladdr;
struct sockaddr_in se_un_ctrladdr_in; struct sockaddr_in se_un_ctrladdr_in;
#ifdef CONFIG_FEATURE_IPV6 #ifdef CONFIG_FEATURE_IPV6
@ -232,8 +230,7 @@ typedef struct servtab
static servtab_t *servtab; static servtab_t *servtab;
#ifdef INETD_FEATURE_ENABLED #ifdef INETD_FEATURE_ENABLED
struct builtin struct builtin {
{
const char *bi_service; /* internally provided service name */ const char *bi_service; /* internally provided service name */
int bi_socktype; /* type of socket supported */ int bi_socktype; /* type of socket supported */
short bi_fork; /* 1 if should fork before call */ short bi_fork; /* 1 if should fork before call */
@ -427,7 +424,8 @@ static void setup(servtab_t *sep)
int on = 1; int on = 1;
int r; int r;
if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) { sep->se_fd = socket(sep->se_family, sep->se_socktype, 0);
if (sep->se_fd < 0) {
bb_perror_msg("%s/%s: socket", sep->se_service, sep->se_proto); bb_perror_msg("%s/%s: socket", sep->se_service, sep->se_proto);
return; return;
} }
@ -1355,7 +1353,7 @@ inetd_main(int argc, char *argv[])
} }
readable = allsock; readable = allsock;
n = select(maxsock + 1, &readable, NULL, NULL, NULL) n = select(maxsock + 1, &readable, NULL, NULL, NULL);
if (n <= 0) { if (n <= 0) {
if (n < 0 && errno != EINTR) { if (n < 0 && errno != EINTR) {
bb_perror_msg("select"); bb_perror_msg("select");
@ -1363,9 +1361,11 @@ inetd_main(int argc, char *argv[])
} }
continue; continue;
} }
for (sep = servtab; n && sep; sep = sep->se_next) { for (sep = servtab; n && sep; sep = sep->se_next) {
// TODO: undo this unholy mess if (sep->se_fd == -1 || !FD_ISSET(sep->se_fd, &readable))
if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) { continue;
n--; n--;
if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) { if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
ctrl = accept(sep->se_fd, NULL, NULL); ctrl = accept(sep->se_fd, NULL, NULL);
@ -1392,6 +1392,7 @@ inetd_main(int argc, char *argv[])
} }
} else } else
ctrl = sep->se_fd; ctrl = sep->se_fd;
Block_Using_Signals(omask); Block_Using_Signals(omask);
pid = 0; pid = 0;
#ifdef INETD_FEATURE_ENABLED #ifdef INETD_FEATURE_ENABLED
@ -1460,19 +1461,16 @@ inetd_main(int argc, char *argv[])
} else } else
#endif #endif
{ {
if ((pwd = getpwnam(sep->se_user)) == NULL) { pwd = getpwnam(sep->se_user);
if (pwd == NULL) {
bb_error_msg("getpwnam: %s: no such user", sep->se_user); bb_error_msg("getpwnam: %s: no such user", sep->se_user);
if (sep->se_socktype != SOCK_STREAM) goto do_exit1;
recv(0, buf, sizeof(buf), 0);
_exit(1);
} }
if (setsid() < 0) if (setsid() < 0)
bb_perror_msg("%s: setsid", sep->se_service); bb_perror_msg("%s: setsid", sep->se_service);
if (sep->se_group && (grp = getgrnam(sep->se_group)) == NULL) { if (sep->se_group && (grp = getgrnam(sep->se_group)) == NULL) {
bb_error_msg("getgrnam: %s: no such group", sep->se_group); bb_error_msg("getgrnam: %s: no such group", sep->se_group);
if (sep->se_socktype != SOCK_STREAM) goto do_exit1;
recv(0, buf, sizeof(buf), 0);
_exit(1);
} }
if (uid != 0) { if (uid != 0) {
/* a user running private inetd */ /* a user running private inetd */
@ -1489,7 +1487,7 @@ inetd_main(int argc, char *argv[])
setgroups(1, &grp->gr_gid); setgroups(1, &grp->gr_gid);
} }
dup2(ctrl, 0); dup2(ctrl, 0);
close(ctrl); if (ctrl) close(ctrl);
dup2(0, 1); dup2(0, 1);
dup2(0, 2); dup2(0, 2);
if (rlim_ofile.rlim_cur != rlim_ofile_cur) if (rlim_ofile.rlim_cur != rlim_ofile_cur)
@ -1500,17 +1498,17 @@ inetd_main(int argc, char *argv[])
(void) close(tmpint); (void) close(tmpint);
sigaction(SIGPIPE, &sapipe, NULL); sigaction(SIGPIPE, &sapipe, NULL);
execv(sep->se_server, sep->se_argv); execv(sep->se_server, sep->se_argv);
bb_perror_msg("execv %s", sep->se_server);
do_exit1:
if (sep->se_socktype != SOCK_STREAM) if (sep->se_socktype != SOCK_STREAM)
recv(0, buf, sizeof(buf), 0); recv(0, buf, sizeof(buf), 0);
bb_perror_msg("execv %s", sep->se_server);
_exit(1); _exit(1);
} }
} }
if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
close(ctrl); close(ctrl);
} } /* for (sep = servtab...) */
} } /* for(;;) */
}
} }
/* /*
@ -1542,8 +1540,12 @@ echo_stream(int s, servtab_t *sep)
int i; int i;
inetd_setproctitle(sep->se_service, s); inetd_setproctitle(sep->se_service, s);
while ((i = read(s, buffer, sizeof(buffer))) > 0 && while (1) {
write(s, buffer, i) > 0); i = read(s, buffer, sizeof(buffer));
if (i <= 0) break;
/* FIXME: this isnt correct - safe_write()? */
if (write(s, buffer, i) <= 0) break;
}
exit(0); exit(0);
} }
@ -1577,9 +1579,11 @@ discard_stream(int s, servtab_t *sep)
char buffer[BUFSIZE]; char buffer[BUFSIZE];
inetd_setproctitle(sep->se_service, s); inetd_setproctitle(sep->se_service, s);
while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) || while (1) {
errno == EINTR); errno = 0;
if (read(s, buffer, sizeof(buffer)) <= 0 && errno != EINTR)
exit(0); exit(0);
}
} }
/* Discard service -- ignore data */ /* Discard service -- ignore data */
@ -1629,8 +1633,10 @@ chargen_stream(int s, servtab_t *sep)
text[LINESIZ] = '\r'; text[LINESIZ] = '\r';
text[LINESIZ + 1] = '\n'; text[LINESIZ + 1] = '\n';
for (rs = ring;;) { rs = ring;
if ((len = endring - rs) >= LINESIZ) for (;;) {
len = endring - rs;
if (len >= LINESIZ)
memmove(text, rs, LINESIZ); memmove(text, rs, LINESIZ);
else { else {
memmove(text, rs, len); memmove(text, rs, len);