Skip to content

Commit 460c201

Browse files
committed
daemon.c: minor style fixup
* "else" on the same line as "}" that closes corresponding "if (...) {"; * multi-line comments begin with "/*\n"; * sizeof, even it is not a function, is written as "sizeof(...)"; * no need to check x?alloc() return value -- it would have died; * "if (...) { ... }" that covers the whole function body can be dedented by returning from the function early with "if (!...) return;"; * SP on each side of an operator, i.e. "a > 0", not "a>0"; Also removes stale comment describing how remove_child() used to do its thing. Signed-off-by: Junio C Hamano <gitster@pobox.com>: Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3bd62c2 commit 460c201

File tree

1 file changed

+34
-42
lines changed

1 file changed

+34
-42
lines changed

daemon.c

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ static void logreport(int priority, const char *err, va_list params)
8181
char buf[1024];
8282
vsnprintf(buf, sizeof(buf), err, params);
8383
syslog(priority, "%s", buf);
84-
}
85-
else {
86-
/* Since stderr is set to linebuffered mode, the
84+
} else {
85+
/*
86+
* Since stderr is set to linebuffered mode, the
8787
* logging of different processes will not overlap
8888
*/
8989
fprintf(stderr, "[%d] ", (int)getpid());
@@ -596,31 +596,24 @@ static struct child {
596596

597597
static void add_child(pid_t pid, struct sockaddr *addr, int addrlen)
598598
{
599-
struct child *newborn;
600-
newborn = xcalloc(1, sizeof *newborn);
601-
if (newborn) {
602-
struct child **cradle;
603-
604-
live_children++;
605-
newborn->pid = pid;
606-
memcpy(&newborn->address, addr, addrlen);
607-
for (cradle = &firstborn; *cradle; cradle = &(*cradle)->next)
608-
if (!memcmp(&(*cradle)->address, &newborn->address,
609-
sizeof newborn->address))
610-
break;
611-
newborn->next = *cradle;
612-
*cradle = newborn;
613-
}
614-
else
615-
logerror("Out of memory spawning new child");
599+
struct child *newborn, **cradle;
600+
601+
/*
602+
* This must be xcalloc() -- we'll compare the whole sockaddr_storage
603+
* but individual address may be shorter.
604+
*/
605+
newborn = xcalloc(1, sizeof(*newborn));
606+
live_children++;
607+
newborn->pid = pid;
608+
memcpy(&newborn->address, addr, addrlen);
609+
for (cradle = &firstborn; *cradle; cradle = &(*cradle)->next)
610+
if (!memcmp(&(*cradle)->address, &newborn->address,
611+
sizeof(newborn->address)))
612+
break;
613+
newborn->next = *cradle;
614+
*cradle = newborn;
616615
}
617616

618-
/*
619-
* Walk from "deleted" to "spawned", and remove child "pid".
620-
*
621-
* We move everything up by one, since the new "deleted" will
622-
* be one higher.
623-
*/
624617
static void remove_child(pid_t pid)
625618
{
626619
struct child **cradle, *blanket;
@@ -642,29 +635,28 @@ static void remove_child(pid_t pid)
642635
*/
643636
static void kill_some_child(void)
644637
{
645-
const struct child *blanket;
638+
const struct child *blanket, *next;
646639

647-
if ((blanket = firstborn)) {
648-
const struct child *next;
640+
if (!(blanket = firstborn))
641+
return;
649642

650-
for (; (next = blanket->next); blanket = next)
651-
if (!memcmp(&blanket->address, &next->address,
652-
sizeof next->address)) {
653-
kill(blanket->pid, SIGTERM);
654-
break;
655-
}
656-
}
643+
for (; (next = blanket->next); blanket = next)
644+
if (!memcmp(&blanket->address, &next->address,
645+
sizeof(next->address))) {
646+
kill(blanket->pid, SIGTERM);
647+
break;
648+
}
657649
}
658650

659651
static void check_dead_children(void)
660652
{
661653
int status;
662654
pid_t pid;
663655

664-
while ((pid = waitpid(-1, &status, WNOHANG))>0) {
656+
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
665657
const char *dead = "";
666658
remove_child(pid);
667-
if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
659+
if (!WIFEXITED(status) || (WEXITSTATUS(status) > 0))
668660
dead = " (with error)";
669661
loginfo("[%d] Disconnected%s", (int)pid, dead);
670662
}
@@ -676,7 +668,7 @@ static void handle(int incoming, struct sockaddr *addr, int addrlen)
676668

677669
if (max_connections && live_children >= max_connections) {
678670
kill_some_child();
679-
sleep(1); /* give it some time to die */
671+
sleep(1); /* give it some time to die */
680672
check_dead_children();
681673
if (live_children >= max_connections) {
682674
close(incoming);
@@ -705,7 +697,8 @@ static void handle(int incoming, struct sockaddr *addr, int addrlen)
705697

706698
static void child_handler(int signo)
707699
{
708-
/* Otherwise empty handler because systemcalls will get interrupted
700+
/*
701+
* Otherwise empty handler because systemcalls will get interrupted
709702
* upon signal receipt
710703
* SysV needs the handler to be rearmed
711704
*/
@@ -1089,8 +1082,7 @@ int main(int argc, char **argv)
10891082
if (log_syslog) {
10901083
openlog("git-daemon", LOG_PID, LOG_DAEMON);
10911084
set_die_routine(daemon_die);
1092-
}
1093-
else
1085+
} else
10941086
setlinebuf(stderr); /* avoid splitting a message in the middle */
10951087

10961088
if (inetd_mode && (group_name || user_name))

0 commit comments

Comments
 (0)