Main Page | Data Structures | Directories | File List | Data Fields

src/safe.c

00001 /********************************************************************\
00002  * This program is free software; you can redistribute it and/or    *
00003  * modify it under the terms of the GNU General Public License as   *
00004  * published by the Free Software Foundation; either version 2 of   *
00005  * the License, or (at your option) any later version.              *
00006  *                                                                  *
00007  * This program is distributed in the hope that it will be useful,  *
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00010  * GNU General Public License for more details.                     *
00011  *                                                                  *
00012  * You should have received a copy of the GNU General Public License*
00013  * along with this program; if not, contact:                        *
00014  *                                                                  *
00015  * Free Software Foundation           Voice:  +1-617-542-5942       *
00016  * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
00017  * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
00018  *                                                                  *
00019  \********************************************************************/
00020 
00021 /*
00022  * $Id: safe.c 901 2006-01-17 18:58:13Z mina $
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 /* From gateway.c */
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                 /* I'm the child - do some cleanup */
00102                 if (webserver) {
00103                         close(webserver->serverSock);
00104                         webserver = NULL;
00105                 }
00106         }
00107 
00108         return result;
00109 }
00110 

Generated on Tue Jan 31 23:13:16 2006 for WifiDog by  doxygen 1.4.4