File tree Expand file tree Collapse file tree 2 files changed +22
-16
lines changed Expand file tree Collapse file tree 2 files changed +22
-16
lines changed Original file line number Diff line number Diff line change 42
42
static void cleanup_fds (void );
43
43
44
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 ;
45
+ typedef int fd_list_t ;
46
+ static fd_list_t fd_list [MAX_FD_CLEANUP ];
51
47
52
48
/** Clean up all the registered fds. Frees the list as it goes.
53
49
* XXX This should only be run by CHILD processes.
54
50
*/
55
51
static void
56
52
cleanup_fds (void )
57
53
{
58
- fd_list_t * entry ;
54
+ unsigned int i ;
59
55
60
- while (NULL != (entry = fd_list )) {
61
- close (entry -> fd );
62
- fd_list = entry -> next ;
63
- free (entry );
56
+ for (i = 0 ; i < sizeof (fd_list ) / sizeof (int ); i ++ ) {
57
+ if (fd_list [i ]) {
58
+ close (fd_list [i ]);
59
+ fd_list [i ] = 0 ;
60
+ }
64
61
}
65
62
}
66
63
@@ -70,11 +67,18 @@ cleanup_fds(void)
70
67
void
71
68
register_fd_cleanup_on_fork (const int fd )
72
69
{
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 ;
70
+ unsigned int i ;
71
+ for (i = 0 ; i < sizeof (fd_list ) / sizeof (int ); i ++ ) {
72
+ if (!fd_list [i ]) {
73
+ break ;
74
+ }
75
+ }
76
+ if (MAX_FD_CLEANUP == i ) {
77
+ debug (LOG_CRIT , "Trying to register more than %d fds for cleanup on fork" ,
78
+ MAX_FD_CLEANUP );
79
+ exit (1 );
80
+ }
81
+ fd_list [i ] = fd ;
78
82
}
79
83
80
84
/** Allocate zero-filled ram or die.
Original file line number Diff line number Diff line change 32
32
#include <sys/types.h> /* For fork */
33
33
#include <unistd.h> /* For fork */
34
34
35
+ #define MAX_FD_CLEANUP 16
36
+
35
37
/** Register an fd for auto-cleanup on fork() */
36
38
void register_fd_cleanup_on_fork (const int );
37
39
You can’t perform that action at this time.
0 commit comments