1: #include "global.h"
2:
3:
4: static void *
5: startSession(sched_task_t *task)
6: {
7: close(TASK_FD(task));
8: VERB(1) syslog(LOG_DEBUG, "Close client %s with socket=%d",
9: (char*) TASK_ARG(task), (int) TASK_FD(task));
10: if (TASK_ARG(task))
11: free(TASK_ARG(task));
12: return 0;
13: }
14:
15: /* ----------------------------------------------------------------------- */
16:
17: static void *
18: thrSched(void *arg __unused)
19: {
20: FTRACE(1);
21:
22: schedRun(root, (intptr_t*) &Kill);
23: pthread_exit(NULL);
24: }
25:
26: int
27: Run(int sock)
28: {
29: io_sockaddr_t sa;
30: socklen_t sslen = sizeof sa.ss;
31: int cli;
32: char *str = NULL, szAddr[STRSIZ] = { 0 };
33: pthread_t tid;
34:
35: FTRACE(1);
36:
37: if (pthread_create(&tid, NULL, thrSched, NULL)) {
38: syslog(LOG_ERR, "Error:: thread scheduler #%d - %s", errno, strerror(errno));
39: return -1;
40: } else
41: pthread_detach(tid);
42: VERB(2) syslog(LOG_DEBUG, "Run scheduler management thread");
43:
44: if (listen(sock, SOMAXCONN) == -1) {
45: syslog(LOG_ERR, "Error:: listen(%d) #%d - %s\n", sock, errno, strerror(errno));
46: pthread_cancel(tid);
47: return -1;
48: }
49:
50: while (!Kill) {
51: if ((cli = accept(sock, &sa.sa, &sslen)) == -1) {
52: syslog(LOG_ERR, "Error:: accept() #%d - %s", errno, strerror(errno));
53: continue;
54: } else
55: VERB(1) {
56: switch (sa.sa.sa_family) {
57: case AF_INET:
58: inet_ntop(AF_INET, &sa.sin.sin_addr, szAddr, sslen);
59: snprintf(szAddr, sizeof szAddr, "%s:%d",
60: szAddr, ntohs(sa.sin.sin_port));
61: break;
62: case AF_INET6:
63: inet_ntop(AF_INET6, &sa.sin6.sin6_addr, szAddr, sslen);
64: snprintf(szAddr, sizeof szAddr, "%s:%d",
65: szAddr, ntohs(sa.sin6.sin6_port));
66: break;
67: case AF_LOCAL:
68: strlcpy(szAddr, sa.sun.sun_path, sizeof szAddr);
69: break;
70: default:
71: close(cli);
72: syslog(LOG_ERR, "Error:: unsupported address type %d",
73: sa.sa.sa_family);
74: continue;
75: }
76: str = strdup(szAddr);
77: syslog(LOG_DEBUG, "Connected client with socket=%d from %s", cli, str);
78: }
79:
80: if (!schedRead(root, startSession, str, cli)) {
81: close(cli);
82: VERB(1) syslog(LOG_DEBUG, "Terminated client with socket=%d", cli);
83: if (str)
84: free(str);
85: }
86: }
87:
88: return 0;
89: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>