diff options
| author | Luke Bratch <luke@bratch.co.uk> | 2019-05-30 22:02:07 +0100 | 
|---|---|---|
| committer | Luke Bratch <luke@bratch.co.uk> | 2019-05-30 22:02:07 +0100 | 
| commit | fd33289346881b96841a598a1f8980212b0bab62 (patch) | |
| tree | 2de9e0c65af5c2d138e3141c88fb826c0ea133dc | |
| parent | 6d183c02a50a42743c3031532f458ab5cea0685d (diff) | |
Remove most (all?) exit()s with error handling without exiting where possible.  Convert most remaining printing to stdout/stderr to debugprint() instead.
| -rw-r--r-- | TODO | 4 | ||||
| -rw-r--r-- | blabouncer.c | 89 | ||||
| -rw-r--r-- | config.c | 11 | ||||
| -rw-r--r-- | functions.c | 4 | ||||
| -rw-r--r-- | logging.c | 22 | ||||
| -rw-r--r-- | sockets.c | 9 | 
6 files changed, 92 insertions, 47 deletions
@@ -10,7 +10,3 @@ Might need to #include <limits.h> in blabouncer.c to make some operating systems  Send a PING to the server before assuming a timeout is definite.  Implement daemon (background) mode with optional foreground mode. - -Replace exit(1)s with error handling without exiting where possible. - -Remove all possible stderr/stdout/perror after startup, everything goes to printdebug() unless critical. diff --git a/blabouncer.c b/blabouncer.c index 1ca16ea..f7e38a1 100644 --- a/blabouncer.c +++ b/blabouncer.c @@ -292,7 +292,8 @@ int disconnectclient(int fd, struct client *clients, struct ircdstrings *ircdstr    char alertmsg[MAXDATASIZE];    if (!snprintf(alertmsg, MAXDATASIZE, "NOTICE %s :blabouncer: client with fd %d has disconnected.", ircdstrings->ircnick, fd)) {      fprintf(stderr, "Error while preparing authentication failure NOTICE!\n"); -    exit(1); +    debugprint(DEBUG_CRIT, "Error while preparing authentication failure NOTICE!\n"); +    alertmsg[0] = '\0';    }    // Remove the client from the clients array    for (int i = 0; i < MAXCLIENTS; i++) { @@ -545,7 +546,7 @@ int doreplay(int sourcefd, int replayseconds, struct client *clients, struct set      for (int j = 0; j < 3; j++) {        // Try to split        if ((token = strsep(&strcopy, " ")) == NULL) { -        debugprint(DEBUG_CRIT, "doreplay(): error splitting string on iteration '%d', exiting!\n", j); +        debugprint(DEBUG_CRIT, "doreplay(): error splitting string on iteration '%d', returning!\n", j);          return 0;        }        // Copy into the token array (strlen + 1 to get the NULL terminator) @@ -629,6 +630,7 @@ int joinautochannels(SSL *server_ssl, struct client *clients, struct settings *s      strncpy(tokens[counter], token, strlen(token) + 1);      if (strlen(tokens[counter]) > MAXCHANLENGTH) {        printf("error: channel name '%s' from configuration file too long, max length is '%d'.\n", tokens[counter], MAXCHANLENGTH); +      debugprint(DEBUG_CRIT, "error: channel name '%s' from configuration file too long, max length is '%d'.\n", tokens[counter], MAXCHANLENGTH);        exit(1);      }      counter++; @@ -657,6 +659,7 @@ void tryautonick(struct ircdstrings *ircdstrings) {    if (ircdstrings->autonicknum == 10) {      // We've already tried 9 nicks and failed, give up      printf("tryautonick(): Tried 9 automatic nicks and the server didn't like any, giving up.\n"); +    debugprint(DEBUG_CRIT, "tryautonick(): Tried 9 automatic nicks and the server didn't like any, giving up.\n");      exit(1);    } @@ -812,7 +815,8 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli          char outgoingmsg[MAXDATASIZE]; // String to send to server          if (!snprintf(outgoingmsg, MAXDATASIZE, "PONG %s", tokens[1])) { // TODO - Make sure tokens[1] actually has a token            fprintf(stderr, "Error while preparing PONG response!\n"); -          exit(1); +          debugprint(DEBUG_CRIT, "Error while preparing PONG response!\n"); +          outgoingmsg[0] = '\0';          }          // sourcefd = 0 as this is a trusted response          sendtoserver(server_ssl, outgoingmsg, strlen(outgoingmsg), 0, clients, settings); @@ -837,6 +841,7 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli            // Prepend a colon (:) first since everything (so far) needs one            if (!snprintf(ircdstrings->nickuserhost, MAXDATASIZE, ":%s", tokens[counter - 1])) {              fprintf(stderr, "Error while preparing nickuserhost for storage!\n"); +            debugprint(DEBUG_CRIT, "Error while preparing nickuserhost for storage!\n");              exit(1);            }            // Null the end of the new string @@ -1545,7 +1550,8 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli                char alertmsg[MAXDATASIZE];                if (!snprintf(alertmsg, MAXDATASIZE, "NOTICE %s :blabouncer: new client with fd %d has successfully authenticated.", ircdstrings->ircnick, sourcefd)) {                  fprintf(stderr, "Error while preparing authentication success NOTICE!\n"); -                exit(1); +                debugprint(DEBUG_CRIT, "Error while preparing authentication success NOTICE!\n"); +                alertmsg[0] = '\0';                }                // "except" the current fd - we can use this as "except/sourcefd" since we set them as authed just above                sendtoallclients(clients, alertmsg, sourcefd, settings); @@ -1558,7 +1564,8 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli            char alertmsg[MAXDATASIZE];            if (!snprintf(alertmsg, MAXDATASIZE, "NOTICE %s :blabouncer: new client with fd %d has failed to authenticate.", ircdstrings->ircnick, sourcefd)) {              fprintf(stderr, "Error while preparing authentication failure NOTICE!\n"); -            exit(1); +            debugprint(DEBUG_CRIT, "Error while preparing authentication failure NOTICE!\n"); +            alertmsg[0] = '\0';            }            // "except" 0 since we trust this message            sendtoallclients(clients, alertmsg, 0, settings); @@ -1586,7 +1593,8 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli          if (strncasecmp(tokens[1], "LS", strlen(tokens[1])) == 0) {            if (!snprintf(outgoingmsg, MAXDATASIZE, ":%s CAP * LS :multi-prefix", ircdstrings->ircdname)) {              fprintf(stderr, "Error while preparing CAP LS response!\n"); -            exit(1); +            debugprint(DEBUG_CRIT, "Error while preparing CAP LS response!\n"); +            outgoingmsg[0] = '\0';            }            // ...even if unauthenticated            sendtoclient(sourcefd, outgoingmsg, clients, settings, 1); @@ -1598,7 +1606,8 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli            if (strncasecmp(tokens[2], ":multi-prefix", strlen(tokens[2])) == 0) {              if (!snprintf(outgoingmsg, MAXDATASIZE, ":%s CAP %s ACK :multi-prefix ", ircdstrings->ircdname, ircdstrings->ircnick)) {                fprintf(stderr, "Error while preparing CAP ACK response!\n"); -              exit(1); +              debugprint(DEBUG_CRIT, "Error while preparing CAP ACK response!\n"); +              outgoingmsg[0] = '\0';              }              // ...even if unauthenticated              sendtoclient(sourcefd, outgoingmsg, clients, settings, 1); @@ -1691,7 +1700,9 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli            // Get client to join channels            if (!snprintf(outgoingmsg, MAXDATASIZE, "%s JOIN :%s", ircdstrings->nickuserhost, channels[i].name)) {              fprintf(stderr, "Error while preparing USER just connected, channel JOIN responses!\n"); -            exit(1); +            debugprint(DEBUG_CRIT, "Error while preparing USER just connected, channel JOIN responses!\n"); +            free(strcopyPtr); +            return 0;            }            sendtoclient(sourcefd, outgoingmsg, clients, settings, 0); @@ -1701,7 +1712,9 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli              // Prepare the no topic message...              if (!snprintf(outgoingmsg, MAXDATASIZE, ":%s 331 %s %s :No topic is set.", ircdstrings->ircdname, ircdstrings->ircnick, channels[i].name)) {                fprintf(stderr, "Error while preparing USER just connected, channel JOIN responses, 331 RPL_NOTOPIC!\n"); -              exit(1); +              debugprint(DEBUG_CRIT, "Error while preparing USER just connected, channel JOIN responses, 331 RPL_NOTOPIC!\n"); +              free(strcopyPtr); +              return 0;              }              // ..and send it to the client              sendtoclient(sourcefd, outgoingmsg, clients, settings, 0); @@ -1710,7 +1723,9 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli              // Prepare the topic message...              if (!snprintf(outgoingmsg, MAXDATASIZE, ":%s 332 %s %s :%s", ircdstrings->ircdname, ircdstrings->ircnick, channels[i].name, channels[i].topic)) {                fprintf(stderr, "Error while preparing USER just connected, channel JOIN responses, 332 RPL_TOPIC!\n"); -              exit(1); +              debugprint(DEBUG_CRIT, "Error while preparing USER just connected, channel JOIN responses, 332 RPL_TOPIC!\n"); +              free(strcopyPtr); +              return 0;              }              // ..and send it to the client              sendtoclient(sourcefd, outgoingmsg, clients, settings, 0); @@ -1718,7 +1733,9 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli              // Next prepare the topic who/when message...              if (!snprintf(outgoingmsg, MAXDATASIZE, ":%s 333 %s %s %s %s", ircdstrings->ircdname, ircdstrings->ircnick, channels[i].name, channels[i].topicwho, channels[i].topicwhen)) {                fprintf(stderr, "Error while preparing USER just connected, channel JOIN responses, 333 RPL_TOPICWHOTIME!\n"); -              exit(1); +              debugprint(DEBUG_CRIT, "Error while preparing USER just connected, channel JOIN responses, 333 RPL_TOPICWHOTIME!\n"); +              free(strcopyPtr); +              return 0;              }              // ..and send it to the client              sendtoclient(sourcefd, outgoingmsg, clients, settings, 0); @@ -1734,7 +1751,9 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli          if (strlen(ircdstrings->mode) > 0) {            if (!snprintf(outgoingmsg, MAXDATASIZE, ":%s MODE %s %s", ircdstrings->ircnick, ircdstrings->ircnick, ircdstrings->mode)) {              fprintf(stderr, "Error while preparing USER just connected, MODE response!\n"); -            exit(1); +            debugprint(DEBUG_CRIT, "Error while preparing USER just connected, MODE response!\n"); +            free(strcopyPtr); +            return 0;            }            sendtoclient(sourcefd, outgoingmsg, clients, settings, 0);          } @@ -1767,7 +1786,8 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli          char outgoingmsg[MAXDATASIZE]; // String to send to client          if (!snprintf(outgoingmsg, MAXDATASIZE, "PONG %s", tokens[1])) { // TODO - Make sure tokens[1] actually has a token            fprintf(stderr, "Error while preparing PONG response!\n"); -          exit(1); +          debugprint(DEBUG_CRIT, "Error while preparing PONG response!\n"); +          outgoingmsg[0] = '\0';          }          sendtoclient(sourcefd, outgoingmsg, clients, settings, 0); @@ -1809,7 +1829,9 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli          if (!snprintf(outgoingmsg, MAXDATASIZE, "%s %s", ircdstrings->nickuserhost, str)) {            fprintf(stderr, "Error while preparing PRIVMSG relay from another bouncer client.\n"); -          exit(1); +          debugprint(DEBUG_CRIT, "Error while preparing PRIVMSG relay from another bouncer client.\n"); +          free(strcopyPtr); +          return 0;          }          // Send to all except source client          sendtoallclients(clients, outgoingmsg, sourcefd, settings); @@ -1929,7 +1951,9 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli          if (!snprintf(fullmsg, MAXDATASIZE, "%s %s", ircdstrings->nickuserhost, str)) {            fprintf(stderr, "Error while preparing NOTICE string for logging.\n"); -          exit(1); +          debugprint(DEBUG_CRIT, "Error while preparing NOTICE string for logging.\n"); +          free(strcopyPtr); +          return 0;          }          // Write to replay log if replay logging enabled @@ -2005,7 +2029,8 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli                debugprint(DEBUG_SOME, "Invalid number '%s' requested by REPLAY command.  Telling client.\n", timetokens[i]);                if (!snprintf(outgoingmsg, MAXDATASIZE, "NOTICE %s :Invalid number '%s' requested by REPLAY command.", ircdstrings->ircnick, timetokens[i])) {                  fprintf(stderr, "Error while preparing REPLAY invalid number response!\n"); -                exit(1); +                debugprint(DEBUG_CRIT, "Error while preparing REPLAY invalid number response!\n"); +                outgoingmsg[0] = '\0';                }                sendtoclient(sourcefd, outgoingmsg, clients, settings, 0);                free(timestrcopyPtr); @@ -2083,7 +2108,9 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli        break;      default:        fprintf(stderr, "Unexpected raw IRC string source!\n"); -      exit(1); +      debugprint(DEBUG_CRIT, "Unexpected raw IRC string source!\n"); +      free(strcopyPtr); +      return 0;    }    // =============================================> @@ -2198,7 +2225,8 @@ int processrawstring(SSL *server_ssl, char *str, int source, struct client *clie            break;          default:            fprintf(stderr, "Unexpected raw IRC string source for unprocessed message \"%s\", length %zd.!\n", messages[i], strlen(messages[i])); -          exit(1); +          debugprint(DEBUG_CRIT, "Unexpected raw IRC string source for unprocessed message \"%s\", length %zd.!\n", messages[i], strlen(messages[i])); +          return 0;        }      }    } @@ -2326,8 +2354,9 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {      debugprint(DEBUG_FULL, "select()ing...\n");      // check to see if anything in the fd_set is waiting - waits here until one of the fds in the set does something      if (select(fdmax + 1, &rfds, NULL, NULL, NULL) < 0) { // network socket + 1, rfds, no writes, no exceptions/errors, no timeout -      printf("receive error, exiting!?\n");        perror("select"); +      debugprint(DEBUG_CRIT, "select() error, errno '%d'.\n", errno); +      continue;      }      // TODO - switch around the serversockfd and STDIN FD_ISSET if-statements?  They feel the wrong way round.  Are they like this on purpose?  I can't remember. @@ -2338,13 +2367,14 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {        debugprint(DEBUG_FULL, "reading server socket!\n");        if ((servernumbytes = sockread(server_ssl, serverbuf, MAXRCVSIZE - 1, settings->servertls)) == -1) { -        printf("receive error (-1), exiting...\n");          perror("recv"); -        debugprint(DEBUG_CRIT, "serversockfd receive error (-1), errno '%d'.\n"); +        printf("receive error (-1), skipping loop iteration...\n"); +        debugprint(DEBUG_CRIT, "serversockfd receive error (-1), skipping loop iteration, errno '%d'.\n"); +        continue;        } else if (servernumbytes == 0) { -        printf("socket closed (or no data received) (0), exiting...\n");          perror("recv"); -        debugprint(DEBUG_CRIT, "serversockfd socket closed (or no data received) (0), errno '%d'.\n"); +        printf("socket closed (or no data received) (0), skipping loop iteration...\n"); +        debugprint(DEBUG_CRIT, "serversockfd socket closed (or no data received) (0), skipping loop iteration, errno '%d'.\n");        }        // If there was a socket error (receive error or socket closed) @@ -2382,7 +2412,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {        // TODO - What if there were two server respones/commands and only one didn't need relaying?        if (!processrawstring(server_ssl, serverbuf, SOURCE_SERVER, clients, EXCEPT_NONE, &ircdstrings, channels, settings)) {          fprintf(stderr, "Error: bouncer-server failed to process raw string.\n"); -        exit(1); +        debugprint(DEBUG_CRIT, "Error: bouncer-server failed to process raw string.\n");        }      } @@ -2443,8 +2473,10 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {            debugprint(DEBUG_SOME, "...new connection!\n");            // handle new connections            if (numclients(clients) >= MAXCLIENTS) { -            fprintf(stderr, "too many clients!\n"); -            exit(1); // TODO - handle cleanly instead of exiting! +            fprintf(stderr, "too many clients, disconnecting and skipping loop iteration!\n"); +            debugprint(DEBUG_CRIT, "too many clients, disconnecting and skipping loop iteration!\n"); +            disconnectclient(i, clients, &ircdstrings, settings); +            continue;            }            addrlen = sizeof remoteaddr;            newfd = accept(*clientsockfd, (struct sockaddr *)&remoteaddr, &addrlen); @@ -2489,7 +2521,8 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {              if (!snprintf(alertmsg, MAXDATASIZE, "NOTICE %s :blabouncer: new client connected from %s with fd %d.", ircdstrings.ircnick,                            inet_ntop(remoteaddr.ss_family, get_in_addr((struct sockaddr*)&remoteaddr), remoteIP, INET6_ADDRSTRLEN), newfd)) {                fprintf(stderr, "Error while preparing new client connection NOTICE!\n"); -              exit(1); +              debugprint(DEBUG_CRIT, "Error while preparing new client connection NOTICE!\n"); +              alertmsg[0] = '\0';              }              // "except" 0 since we trust this message              sendtoallclients(clients, alertmsg, 0, settings); @@ -2526,7 +2559,7 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {              // TODO - What if there were two server respones/commands and only one didn't need relaying?              if (!processrawstring(server_ssl, clientbuf, SOURCE_CLIENT, clients, i, &ircdstrings, channels, settings)) {                fprintf(stderr, "Error: bouncer-client failed to process raw string.\n"); -              exit(1); +              debugprint(DEBUG_CRIT, "Error: bouncer-client failed to process raw string.\n");              }            }          } @@ -19,6 +19,7 @@ int getconfstr(char *confname, char *filename, char* dest) {    if (fp == NULL) {      printf("error: could not open configuration file '%s'.\n", filename); +    debugprint(DEBUG_CRIT, "error: could not open configuration file '%s'.\n", filename);      exit(1);    }    // Loop through the whole file, looking for the requested configuration option @@ -74,6 +75,7 @@ int getconfstr(char *confname, char *filename, char* dest) {     // Check for start and end quotes    if (conf[0] != '"' || conf[strlen(conf) - 1] != '"') {      printf("Error reading configuration option '%s', not enclosed in double quotes in configuration file '%s'!\n", confname, filename); +    debugprint(DEBUG_CRIT, "Error reading configuration option '%s', not enclosed in double quotes in configuration file '%s'!\n", confname, filename);      exit(1);    } @@ -93,8 +95,7 @@ int getconfint(char *confname, char *filename) {    char result[MAXCHAR];    if (!getconfstr(confname, filename, result)) {      printf("getconfint(): error getting configuration option '%s' from configuration file '%s'.\n", confname, filename); -    // TODO - Do something useful here instead of exiting -    exit(1); +    debugprint(DEBUG_CRIT, "getconfint(): error getting configuration option '%s' from configuration file '%s'.\n", confname, filename);    }    return strtol(result, NULL, 10); // Convert resulting string to an integer, base 10 @@ -126,6 +127,7 @@ int checkpassword(char *password, char *filename) {    }    printf("checkpassword(): unexpectedly got to end of function, quitting.\n"); +  debugprint(DEBUG_CRIT, "checkpassword(): unexpectedly got to end of function, quitting.\n");    exit(1);  } @@ -142,6 +144,7 @@ int createconfigfile(char *filename) {    if (stat(dir, &st) == -1) {      if (mkdir(dir, 0700)) {        printf("Error creating config directory '%s'.\n", dir); +      debugprint(DEBUG_CRIT, "Error creating config directory '%s'.\n", dir);        exit(1);      } else {        printf("Created config directory '%s'.\n", dir); @@ -154,6 +157,7 @@ int createconfigfile(char *filename) {    if (fp == NULL) {      printf("error: could not open default configuration file '%s' for writing.\n", filename); +    debugprint(DEBUG_CRIT, "error: could not open default configuration file '%s' for writing.\n", filename);      exit(1);    } @@ -233,8 +237,7 @@ int createconfigfile(char *filename) {    // Write complete string to file    if ((fprintf(fp, "%s", string)) < 0) { -    printf("error: could not write to replay log file.\n"); -    exit(1); +    debugprint(DEBUG_CRIT, "error: could not write to replay log file.\n");    }    fclose(fp); diff --git a/functions.c b/functions.c index 13b910b..dbcc54e 100644 --- a/functions.c +++ b/functions.c @@ -52,6 +52,7 @@ void updategreetingnick(char *greeting, char *greetingnum, char *newnick, char *    char greetingtmp2[MAXDATASIZE];    if (!snprintf(greetingtmp2, MAXDATASIZE, "%s%s %s", greetingtmp, newnick, greeting + pos + strlen(oldnick) + 1)) {      fprintf(stderr, "Error while preparing new greeting string!\n"); +    debugprint(DEBUG_CRIT, "Error while preparing new greeting string!\n");      exit(1);    } @@ -80,8 +81,7 @@ void debugprint(int level, char *format, ...) {    int bytes = 0;    fp = fopen(debugpath, "a");    if ((bytes = vfprintf(fp, format, args)) < 0) { -    printf("error: could not write to debug file.\n"); -    exit(1); +    debugprint(DEBUG_CRIT, "error: could not write to debug file.\n");    }    fclose(fp); @@ -52,8 +52,8 @@ int logline(char *str, char *ournick, char *basedir, int type) {    for (int i = 0; i < 3; i++) {      // Try to split      if ((token = strsep(&str, " ")) == NULL) { -      printf("Error splitting string for logging, exiting!\n"); -      exit(1); +      debugprint(DEBUG_CRIT, "Error splitting string for logging, returning!\n"); +      return 0;      }      // Copy into the token array (strlen + 1 to get the NULL terminator)      strncpy(tokens[i], token, strlen(token) + 1); @@ -156,7 +156,7 @@ int logline(char *str, char *ournick, char *basedir, int type) {      if (mkdir(logdir, 0700)) {        debugprint(DEBUG_CRIT, "Error creating log directory '%s.\n", logdir);        printf("Error creating log directory '%s'.\n", logdir); -      exit(1); +      return 0;      } else {        debugprint(DEBUG_FULL, "logline(): log directory '%s'.\n", logdir);      } @@ -171,7 +171,8 @@ int logline(char *str, char *ournick, char *basedir, int type) {    if (fp == NULL) {      debugprint(DEBUG_CRIT, "error: could not open log file '%s' for writing.\n", filename);      printf("error: could not open log file '%s' for writing.\n", filename); -    exit(1); +    fclose(fp); +    return 0;    }    // Get a current time string to prepend - TODO - Make this customisable. @@ -188,14 +189,18 @@ int logline(char *str, char *ournick, char *basedir, int type) {      // Prepend the time string and "from" nick      if (!snprintf(line, MAXCHAR, "%s <%s> %s", timestr, tokens[0], str)) {        fprintf(stderr, "Error while preparing log string to write!\n"); -      exit(1); +      debugprint(DEBUG_CRIT, "Error while preparing log string to write!\n"); +      fclose(fp); +      return 0;      }    } else if (type == LOG_JOINPART || type == LOG_TOPIC) {      // Prepend the time string      char line2[MAXCHAR];      if (!snprintf(line2, MAXCHAR, "%s %s", timestr, line)) {        fprintf(stderr, "Error while preparing log string to write!\n"); -      exit(1); +      debugprint(DEBUG_CRIT, "Error while preparing log string to write!\n"); +      fclose(fp); +      return 0;      }      // Copy back to line to write      snprintf(line, MAXCHAR, "%s", line2); @@ -208,9 +213,10 @@ int logline(char *str, char *ournick, char *basedir, int type) {    // Write complete line to file    if ((bytes = fprintf(fp, "%s", line)) < 0) { -    debugprint(DEBUG_CRIT, "error: could not write to log file.\n");      printf("error: could not write to log file.\n"); -    exit(1); +    debugprint(DEBUG_CRIT, "error: could not write to log file.\n"); +    fclose(fp); +    return 0;    }    fclose(fp); @@ -69,6 +69,7 @@ int createclientsocket(char *listenport) {    if ((rv = getaddrinfo(NULL, listenport, &hints, &ai)) != 0) {      fprintf(stderr, "bouncer-client: %s\n", gai_strerror(rv)); +    debugprint(DEBUG_CRIT, "bouncer-client: %s\n", gai_strerror(rv));      exit(1);    } @@ -105,13 +106,15 @@ int createclientsocket(char *listenport) {      // failed to bind      close(listener);      printf("bouncer-client: failed to bind, exiting...\n"); +    debugprint(DEBUG_CRIT, "bouncer-client: failed to bind, exiting...\n");      exit(1);    }    // if we got here, it means we didn't get bound    if (p == NULL) {      fprintf(stderr, "bouncer-client: failed to bind\n"); -    exit(2); +    debugprint(DEBUG_CRIT, "bouncer-client: failed to bind\n"); +    exit(1);    }    freeaddrinfo(ai); // all done with this @@ -119,6 +122,7 @@ int createclientsocket(char *listenport) {      // listen      if (listen(listener, BACKLOG) == -1) {          perror("listen"); +        debugprint(DEBUG_CRIT, "bouncer-client failed to listen(), errno '%d'.\n", errno);          exit(1);      } @@ -151,6 +155,7 @@ SSL_CTX *create_openssl_context(int type) {    if (!ctx) {      perror("Unable to create SSL context");      ERR_print_errors_fp(stderr); +    debugprint(DEBUG_CRIT, "Unable to create SSL context, errno '%d'.\n", errno);      exit(EXIT_FAILURE);    } @@ -171,12 +176,14 @@ void configure_openssl_context(SSL_CTX *ctx, char *certfile, char *keyfile) {    if (SSL_CTX_use_certificate_file(ctx, certfile, SSL_FILETYPE_PEM) <= 0) {      ERR_print_errors_fp(stderr);      printf("Couldn't load certificate file '%s'.  Hint: You can generate your own with OpenSSL.  Once created, set its location in blabouncer.conf which by default is in ~/.blabouncer/.\n", certfile); +    debugprint(DEBUG_CRIT, "Couldn't load certificate file '%s'.  Hint: You can generate your own with OpenSSL.  Once created, set its location in blabouncer.conf which by default is in ~/.blabouncer/.\n", certfile);      exit(EXIT_FAILURE);    }    if (SSL_CTX_use_PrivateKey_file(ctx, keyfile, SSL_FILETYPE_PEM) <= 0 ) {      ERR_print_errors_fp(stderr);      printf("Couldn't load key file '%s'.  Hint: You can generate your own with OpenSSL.  Once created, set its location in blabouncer.conf which by default is in ~/.blabouncer/.\n", keyfile); +    debugprint(DEBUG_CRIT, "Couldn't load key file '%s'.  Hint: You can generate your own with OpenSSL.  Once created, set its location in blabouncer.conf which by default is in ~/.blabouncer/.\n", keyfile);      exit(EXIT_FAILURE);    }  }  | 
