35
35
#include <errno.h>
36
36
#include <syslog.h>
37
37
38
- #include "httpd.h"
39
- #include "gateway.h"
40
38
#include "safe.h"
41
39
#include "debug.h"
42
40
41
+ /** @brief Clean up all the registered fds. */
42
+ static void cleanup_fds (void );
43
+
44
+ /** List of fd's to close on fork. */
45
+ typedef struct _fd_list {
46
+ int fd ; /**< @brief file descriptor */
47
+ struct _fd_list * next ; /**< @brief linked list pointer */
48
+ } fd_list_t ;
49
+
50
+ static fd_list_t * fd_list = NULL ;
51
+
52
+ /** Clean up all the registered fds. Frees the list as it goes.
53
+ * XXX This should only be run by CHILD processes.
54
+ */
55
+ static void
56
+ cleanup_fds (void )
57
+ {
58
+ fd_list_t * entry ;
59
+
60
+ while (NULL != (entry = fd_list )) {
61
+ close (entry -> fd );
62
+ fd_list = entry -> next ;
63
+ free (entry );
64
+ }
65
+ }
66
+
67
+ /** Register an fd for auto-cleanup on fork()
68
+ * @param fd A file descriptor.
69
+ */
70
+ void
71
+ register_fd_cleanup_on_fork (const int fd )
72
+ {
73
+ fd_list_t * entry = safe_malloc (sizeof (fd_list_t ));
74
+
75
+ entry -> fd = fd ;
76
+ entry -> next = fd_list ;
77
+ fd_list = entry ;
78
+ }
79
+
80
+ /** Allocate zero-filled ram or die.
81
+ * @param size Number of bytes to allocate
82
+ * @return void * pointer to the zero'd bytes.
83
+ */
43
84
void *
44
85
safe_malloc (size_t size )
45
86
{
@@ -53,6 +94,14 @@ safe_malloc(size_t size)
53
94
return (retval );
54
95
}
55
96
97
+ /** Re-allocates memory to a new larger allocation.
98
+ * DOES NOT ZERO the added RAM
99
+ * Original pointer is INVALID after call
100
+ * Dies on allocation failures.
101
+ * @param ptr A pointer to a current allocation from safe_malloc()
102
+ * @param newsize What size it should now be in bytes
103
+ * @return pointer to newly allocation ram
104
+ */
56
105
void *
57
106
safe_realloc (void * ptr , size_t newsize )
58
107
{
@@ -65,6 +114,10 @@ safe_realloc(void *ptr, size_t newsize)
65
114
return retval ;
66
115
}
67
116
117
+ /** Duplicates a string or die if memory cannot be allocated
118
+ * @param s String to duplicate
119
+ * @return A string in a newly allocated chunk of heap.
120
+ */
68
121
char *
69
122
safe_strdup (const char * s )
70
123
{
@@ -81,6 +134,13 @@ safe_strdup(const char *s)
81
134
return (retval );
82
135
}
83
136
137
+ /** Sprintf into a newly allocated buffer
138
+ * Memory MUST be freed. Dies if memory cannot be allocated.
139
+ * @param strp Pointer to a pointer that will be set to the newly allocated string
140
+ * @param fmt Format string like sprintf
141
+ * @param ... Variable number of arguments for format string
142
+ * @return int Size of allocated string.
143
+ */
84
144
int
85
145
safe_asprintf (char * * strp , const char * fmt , ...)
86
146
{
@@ -94,6 +154,13 @@ safe_asprintf(char **strp, const char *fmt, ...)
94
154
return (retval );
95
155
}
96
156
157
+ /** Sprintf into a newly allocated buffer
158
+ * Memory MUST be freed. Dies if memory cannot be allocted.
159
+ * @param strp Pointer to a pointer that will be set to the newly allocated string
160
+ * @param fmt Format string like sprintf
161
+ * @param ap pre-digested va_list of arguments.
162
+ * @return int Size of allocated string.
163
+ */
97
164
int
98
165
safe_vasprintf (char * * strp , const char * fmt , va_list ap )
99
166
{
@@ -108,6 +175,10 @@ safe_vasprintf(char **strp, const char *fmt, va_list ap)
108
175
return (retval );
109
176
}
110
177
178
+ /** Fork and then close any registered fille descriptors.
179
+ * If fork() fails, we die.
180
+ * @return pid_t 0 for child, pid of child for parent
181
+ */
111
182
pid_t
112
183
safe_fork (void )
113
184
{
@@ -119,10 +190,7 @@ safe_fork(void)
119
190
exit (1 );
120
191
} else if (result == 0 ) {
121
192
/* I'm the child - do some cleanup */
122
- if (webserver ) {
123
- close (webserver -> serverSock );
124
- webserver = NULL ;
125
- }
193
+ cleanup_fds ();
126
194
}
127
195
128
196
return result ;
0 commit comments