listen: added struct listen_socket
Make the listen socket an object, allowing us to add more fields later. Convert listenSockets into a simple linked list.
This commit is contained in:
35
src/listen.c
35
src/listen.c
@@ -54,8 +54,13 @@
|
|||||||
port, strerror(errno)); \
|
port, strerror(errno)); \
|
||||||
} while (0);
|
} while (0);
|
||||||
|
|
||||||
static int *listenSockets;
|
struct listen_socket {
|
||||||
static int numberOfListenSockets;
|
struct listen_socket *next;
|
||||||
|
|
||||||
|
int fd;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct listen_socket *listen_sockets;
|
||||||
int boundPort;
|
int boundPort;
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
@@ -69,6 +74,7 @@ static int establishListen(int pf, const struct sockaddr *addrp,
|
|||||||
#ifdef HAVE_STRUCT_UCRED
|
#ifdef HAVE_STRUCT_UCRED
|
||||||
int passcred = 1;
|
int passcred = 1;
|
||||||
#endif
|
#endif
|
||||||
|
struct listen_socket *ls;
|
||||||
GIOChannel *channel;
|
GIOChannel *channel;
|
||||||
|
|
||||||
if ((sock = socket(pf, SOCK_STREAM, 0)) < 0)
|
if ((sock = socket(pf, SOCK_STREAM, 0)) < 0)
|
||||||
@@ -91,17 +97,17 @@ static int establishListen(int pf, const struct sockaddr *addrp,
|
|||||||
setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
|
setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
numberOfListenSockets++;
|
ls = g_new(struct listen_socket, 1);
|
||||||
listenSockets = g_realloc(listenSockets, sizeof(listenSockets[0]) *
|
ls->fd = sock;
|
||||||
numberOfListenSockets);
|
|
||||||
|
|
||||||
listenSockets[numberOfListenSockets - 1] = sock;
|
|
||||||
|
|
||||||
channel = g_io_channel_unix_new(sock);
|
channel = g_io_channel_unix_new(sock);
|
||||||
g_io_add_watch(channel, G_IO_IN,
|
g_io_add_watch(channel, G_IO_IN,
|
||||||
listen_in_event, GINT_TO_POINTER(sock));
|
listen_in_event, GINT_TO_POINTER(sock));
|
||||||
g_io_channel_unref(channel);
|
g_io_channel_unref(channel);
|
||||||
|
|
||||||
|
ls->next = listen_sockets;
|
||||||
|
listen_sockets = ls;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,18 +275,15 @@ void listenOnPort(void)
|
|||||||
|
|
||||||
void closeAllListenSockets(void)
|
void closeAllListenSockets(void)
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
|
|
||||||
g_debug("closeAllListenSockets called");
|
g_debug("closeAllListenSockets called");
|
||||||
|
|
||||||
for (i = 0; i < numberOfListenSockets; i++) {
|
while (listen_sockets != NULL) {
|
||||||
g_debug("closing listen socket %i", i);
|
struct listen_socket *ls = listen_sockets;
|
||||||
while (close(listenSockets[i]) < 0 && errno == EINTR) ;
|
listen_sockets = ls->next;
|
||||||
}
|
|
||||||
|
|
||||||
numberOfListenSockets = 0;
|
close(ls->fd);
|
||||||
g_free(listenSockets);
|
g_free(ls);
|
||||||
listenSockets = NULL;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_remote_uid(int fd)
|
static int get_remote_uid(int fd)
|
||||||
|
Reference in New Issue
Block a user