I'm implementing a http server in C. I have a custom function for writing headers. When I call it, it doesn't do anything. I have placed an arbitrary printf inside the function, to make sure that it's called, and it doesn't produce output too. Program compiles with success, and works normally as intended, aside from this issue. I can connect to server, which results in empty response due to this problem. I can easily use fprintf
instead, but I want to understand the problem. The function is declared as follows:
void write_response_ln(FILE *fp, char *format, ...)
{
va_list args;
printf("dsgsfdg");
strcat(format, "\r\n");
va_start(args, format);
vfprintf(fp, format, args);
va_end(args);
}
It is located in it's own file, apart from the file in which the caller is. Even though it is called 4 times, client processes report empty response. Why does this happen? BTW I'm using gcc 4.7 on linux to compile this.
Here is the caller function:
static pid_t handle_connection(size_t bfrsz, int fd_connect, pid_t kid_pid)
{
int c;
char *headers = malloc(bfrsz);
FILE *res = fdopen(fd_connect, "a+");
kid_pid = getpid();
bzero(headers, bfrsz);
fgets(headers, bfrsz, res);
fprintf(stdout, "REQ: %s\n", headers);
write_response_ln(res, "HTTP 200 OK");
write_response_ln(res, "Content-Type:text/html");
write_response_ln(res, "");
write_response_ln(res, "I don't have a parser yet.");
fclose(res);
// Commit suicide.
printf("Transaction: Complete: Kill: [%d]\n", kid_pid);
sleep(1);
kill(kid_pid, SIGINT);
free(headers);
return kid_pid;
}
And a go with the gdb gave me this:
(gdb) break write_response_ln
Breakpoint 1 at 0x400f80: file headers.c, line 8.
(gdb) run
Starting program: /home/goktug/code/server/src/server
Program received signal SIGSEGV, Segmentation fault.
0x0000003491239f24 in ____strtoll_l_internal () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.15-58.fc17.x86_64
As a little note, I haven't done the getopt part yet, so the program segfaults when called without arguments.