Annotation of mqtt/src/daemon.c, revision 1.1.2.5
1.1.2.1 misho 1: #include "global.h"
2:
3:
1.1.2.5 ! misho 4: static void *
! 5: startSession(sched_task_t *task)
1.1.2.4 misho 6: {
1.1.2.5 ! misho 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));
1.1.2.4 misho 12: return 0;
13: }
14:
15: /* ----------------------------------------------------------------------- */
16:
1.1.2.5 ! misho 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:
1.1.2.2 misho 26: int
1.1.2.3 misho 27: Run(int sock)
1.1.2.2 misho 28: {
1.1.2.4 misho 29: io_sockaddr_t sa;
30: socklen_t sslen = sizeof sa.ss;
31: int cli;
1.1.2.5 ! misho 32: char *str = NULL, szAddr[STRSIZ] = { 0 };
! 33: pthread_t tid;
1.1.2.4 misho 34:
1.1.2.2 misho 35: FTRACE(1);
36:
1.1.2.5 ! misho 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:
1.1.2.3 misho 44: if (listen(sock, SOMAXCONN) == -1) {
1.1.2.4 misho 45: syslog(LOG_ERR, "Error:: listen(%d) #%d - %s\n", sock, errno, strerror(errno));
1.1.2.5 ! misho 46: pthread_cancel(tid);
1.1.2.3 misho 47: return -1;
48: }
49:
1.1.2.4 misho 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: }
1.1.2.5 ! misho 76: str = strdup(szAddr);
! 77: syslog(LOG_DEBUG, "Connected client with socket=%d from %s", cli, str);
1.1.2.4 misho 78: }
79:
1.1.2.5 ! misho 80: if (!schedRead(root, startSession, str, cli)) {
1.1.2.4 misho 81: close(cli);
82: VERB(1) syslog(LOG_DEBUG, "Terminated client with socket=%d", cli);
1.1.2.5 ! misho 83: if (str)
! 84: free(str);
1.1.2.4 misho 85: }
86: }
87:
1.1.2.2 misho 88: return 0;
89: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>