forked from Exhar/otpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accept_thread.c
75 lines (65 loc) · 1.98 KB
/
accept_thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* For alternative licensing terms, contact licensing@tri-dsystems.com.
*
* Copyright 2005,2006 TRI-D Systems, Inc.
*/
#include "ident.h"
RCSID("$Id$")
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h> /* sockaddr_un */
#include <unistd.h>
#include "extern.h"
/*
* The accept thread's job is easy.
* Accept a new connection and pass it off to a work thread. Repeat.
*/
void *
accept_thread(void *arg)
{
config_t *config = (config_t *) arg;
int s;
struct sockaddr_un sa;
socklen_t salen = sizeof(sa);
mlog(LOG_DEBUG2, "%s: tid=%lu", __func__, (unsigned long) pthread_self());
for (;;) {
s = accept(config->s, (struct sockaddr *) &sa, &salen);
if (s == -1) {
mlog(LOG_ERR, "%s: plugin accept: %s", __func__, strerror(errno));
/* not much else we can do */
continue;
}
mlog(LOG_DEBUG, "%s: plugin accept fd=%d", __func__, s);
/*
* start a work thread (1:1 model)
*/
{
pthread_t tid;
config_t *inst;
inst = xmalloc(sizeof(*config));
(void) memcpy(inst, config, sizeof(*config));
inst->s = s;
xpthread_create(&tid, &attr_detached, work_thread, inst);
}
} /* for (;;) */
}