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>
Junio C Hamano committed
Aug 24, 2008 at 13:27 UTC
460c201039471d22194ca871290c098bfe6ce6a3
1 file changed
+34
-42
daemon.c
+34
-42
@@ -81,9 +81,9 @@ static void logreport(int priority, const char *err, va_list params)
81
char buf[1024];
82
vsnprintf(buf, sizeof(buf), err, params);
83
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
87
* logging of different processes will not overlap
88
*/
89
fprintf(stderr, "[%d] ", (int)getpid());
@@ -596,31 +596,24 @@ static struct child {
596
597
static void add_child(pid_t pid, struct sockaddr *addr, int addrlen)
598
{
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;
615
}
616
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
- */
617
static void remove_child(pid_t pid)
618
{
619
struct child **cradle, *blanket;
@@ -642,18 +635,17 @@ static void remove_child(pid_t pid)
635
*/
636
static void kill_some_child(void)
637
{
645
- const struct child *blanket;
638
+ const struct child *blanket, *next;
639
647
- if ((blanket = firstborn)) {
648
- const struct child *next;
640
+ if (!(blanket = firstborn))
641
+ return;
642
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
+ }
649
}
650
651
static void check_dead_children(void)
@@ -661,10 +653,10 @@ static void check_dead_children(void)
653
int status;
654
pid_t pid;
655
664
- while ((pid = waitpid(-1, &status, WNOHANG))>0) {
656
+ while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
657
const char *dead = "";
658
remove_child(pid);
667
- if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
659
+ if (!WIFEXITED(status) || (WEXITSTATUS(status) > 0))
660
dead = " (with error)";
661
loginfo("[%d] Disconnected%s", (int)pid, dead);
662
}
@@ -676,7 +668,7 @@ static void handle(int incoming, struct sockaddr *addr, int addrlen)
668
669
if (max_connections && live_children >= max_connections) {
670
kill_some_child();
679
- sleep(1); /* give it some time to die */
671
+ sleep(1); /* give it some time to die */
672
check_dead_children();
673
if (live_children >= max_connections) {
674
close(incoming);
@@ -705,7 +697,8 @@ static void handle(int incoming, struct sockaddr *addr, int addrlen)
697
698
static void child_handler(int signo)
699
{
708
- /* Otherwise empty handler because systemcalls will get interrupted
700
+ /*
701
+ * Otherwise empty handler because systemcalls will get interrupted
702
* upon signal receipt
703
* SysV needs the handler to be rearmed
704
*/
@@ -1089,8 +1082,7 @@ int main(int argc, char **argv)
1082
if (log_syslog) {
1083
openlog("git-daemon", LOG_PID, LOG_DAEMON);
1084
set_die_routine(daemon_die);
1092
- }
1093
- else
1085
+ } else
1086
setlinebuf(stderr); /* avoid splitting a message in the middle */
1087
1088
if (inetd_mode && (group_name || user_name))