[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [Ovirt-devel] [PATCH-node 1/2] Add ovirt-listen-awake daemon. This is a very small daemon that oVirt



Chris Lalancette <clalance redhat com> wrote:
...
> diff --git a/ovirt-listen-awake/ovirt-listen-awake.c b/ovirt-listen-awake/ovirt-listen-awake.c
...
> +static void usage(int exitcode)
> +{
> +  printf("Usage: ovirt-listen-awake [OPTIONS]\n");
> +  printf("OPTIONS:\n");
> +  printf(" -h\t\tShow this help message\n");
> +  printf(" -n\t\tDo not daemonize (useful for debugging)\n");
> +  exit(exitcode);
> +}
> +
> +int main(int argc, char *argv[])
> +{
> +  int listen_socket, conn;
> +  struct sockaddr_in client_address;
> +  unsigned int addrlen;
> +  FILE *conn_stream;
> +  FILE *logfile;
> +  char buffer[BUFLEN];
> +  int c;
> +  int do_daemon;
> +  int i;
> +
> +  do_daemon = 1;
> +
> +  while ((c=getopt(argc, argv, ":hn")) != -1) {
> +    switch(c) {
> +    case 'h':
> +      usage(0);
> +      break;
> +    case 'n':
> +      do_daemon = 0;
> +      break;
> +    default:
> +      usage(1);
> +    }
> +  }
> +
> +  if ((argc-optind) != 0) {
> +    for (i = optind; i < argc ; i ++) {
> +      fprintf(stderr, "Extra operand %s\n", argv[i]);
> +    }
> +    error(1, 0, "Try --help for more information");

s/--help/-h/
(or overkill: switch to getopt_long and accept --help)

> +  }
> +
> +  listen_socket = listenSocket(7777);
> +  if (listen_socket < 0)
> +    return 2;
> +
> +  if (do_daemon) {
> +    logfile = fopen(LOGFILE,"a+");
> +    if (logfile == NULL)
> +      error(3, errno, "Error opening logfile %s", LOGFILE);
> +
> +    // NOTE: this closes stdout and stderr
> +    if (daemon(0, 0) < 0)
> +      error(4, errno, "Error daemonizing");
> +
> +    // so re-open them to the logfile here
> +    dup2(fileno(logfile), STDOUT_FILENO);
> +    dup2(fileno(logfile), STDERR_FILENO);

Oops.  I missed this the first time around.
Each of these dup2 calls can fail, so best to check for that.

> +  }
> +
> +  while (1) {
> +    addrlen = sizeof(client_address);
> +    memset(&client_address, 0, addrlen);
> +    memset(buffer, 0, BUFLEN);

This use of memset still looks unnecessary.

...
> +  }
> +
> +  close(listen_socket);
> +  fclose(logfile);

How about detecting write failure (e.g., disk full):

     if (fclose (logfile) != 0)
       return 1;

> +  return 0;
> +}
> diff --git a/ovirt-node.spec b/ovirt-node.spec
...


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]