00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00031 #include <stdarg.h>
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <errno.h>
00036
00037 #include "httpd.h"
00038 #include "safe.h"
00039 #include "debug.h"
00040 #include <syslog.h>
00041
00042
00043 extern httpd * webserver;
00044
00045 void * safe_malloc (size_t size) {
00046 void * retval = NULL;
00047 retval = malloc(size);
00048 if (!retval) {
00049 debug(LOG_CRIT, "Failed to malloc %d bytes of memory: %s. Bailing out", size, strerror(errno));
00050 exit(1);
00051 }
00052 return (retval);
00053 }
00054
00055 char * safe_strdup(const char *s) {
00056 char * retval = NULL;
00057 if (!s) {
00058 debug(LOG_CRIT, "safe_strdup called with NULL which would have crashed strdup. Bailing out");
00059 exit(1);
00060 }
00061 retval = strdup(s);
00062 if (!retval) {
00063 debug(LOG_CRIT, "Failed to duplicate a string: %s. Bailing out", strerror(errno));
00064 exit(1);
00065 }
00066 return (retval);
00067 }
00068
00069 int safe_asprintf(char **strp, const char *fmt, ...) {
00070 va_list ap;
00071 int retval;
00072
00073 va_start(ap, fmt);
00074 retval = safe_vasprintf(strp, fmt, ap);
00075 va_end(ap);
00076
00077 return (retval);
00078 }
00079
00080 int safe_vasprintf(char **strp, const char *fmt, va_list ap) {
00081 int retval;
00082
00083 retval = vasprintf(strp, fmt, ap);
00084
00085 if (retval == -1) {
00086 debug(LOG_CRIT, "Failed to vasprintf: %s. Bailing out", strerror(errno));
00087 exit (1);
00088 }
00089 return (retval);
00090 }
00091
00092 pid_t safe_fork(void) {
00093 pid_t result;
00094 result = fork();
00095
00096 if (result == -1) {
00097 debug(LOG_CRIT, "Failed to fork: %s. Bailing out", strerror(errno));
00098 exit (1);
00099 }
00100 else if (result == 0) {
00101
00102 if (webserver) {
00103 close(webserver->serverSock);
00104 webserver = NULL;
00105 }
00106 }
00107
00108 return result;
00109 }
00110