anon
anon

Reputation:

Why rsyslog logs the same log multiple times

SOLVED: Changed c == accept(... to c = accept(... One should use -Wall to see warning when == is used improperly.

I configure syslog connection opening

openlog("program", LOG_PID, LOG_USER);

Server code:

for (;;) {
    syslog(LOG_NOTICE, "before accept");
    if ((c == accept(s, (struct sockaddr *)&sa, &b)) == -1) {
        syslog(LOG_ERR, "could not accept on socket");
        exit(1);
    }
    syslog(LOG_NOTICE, "after accept");
    pid = fork();
    if (pid < 0) {
        syslog(LOG_ERR, "could not fork to handle client session");
        exit(1);
    } else if (0 == pid) {
        close(s);
        handle_client(c);
        exit(0);
        syslog(LOG_ERR, "exit after");
    } else
        close(c);
}

Then I conenct using telnet

telnet localhost 2345
...
Connection closed by foreign host.

Log /var/log/user.log shows

...some_info...: client was handled
...some_info...: client was handled
...some_info...: before accept
...some_info...: after accept
...some_info...: before accept
...some_info...: after accept
...some_info...: client was handled

It looks like that rsyslog logged message multiple times in random order, although I know that I called telnet only once. I clearly understand how the program executed, but do not understand why rsyslog logged multiples times.

Could you please suggest how to fix that "bug"?

Upvotes: 0

Views: 608

Answers (1)

Weather Vane
Weather Vane

Reputation: 34585

Here is one bug:

if ((c == accept(s, (struct sockaddr *)&sa, &b)) == -1) {

where c will never equal -1. It should be

if ((c = accept(s, (struct sockaddr *)&sa, &b)) == -1) {

Edit

This will have a knock-on effect to the subsequent code using c, which can only have the values 0 or 1.

handle_client(c);

and

close(c);

Upvotes: 3

Related Questions