This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
forked from julienblitte/tiny-ssdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
99 lines (84 loc) · 1.75 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <syslog.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include "ssdp.h"
#include "logfilter.h"
#define PID_FILE "/var/run/tiny-ssdpd.pid"
#define SIGNAL_CATCH {SIGHUP,SIGTERM}
void sig_handler(int signo)
{
switch(signo)
{
case SIGHUP: /* not implemented */
break;
case SIGTERM:
remove(PID_FILE);
exit(EXIT_SUCCESS);
break;
}
}
void daemonize()
{
FILE *run;
int i, sig[] = SIGNAL_CATCH;
/* fork, detach, reset stdios */
if (daemon(0, 0) == -1)
{
perror("Fatal error unable to become deamon!");
exit(EXIT_FAILURE);
}
/* pid file */
if(run = fopen(PID_FILE, "wt"))
{
fprintf(run, "%d", getpid());
fclose(run);
}
/* signals */
for (i=0; i < sizeof(sig)/sizeof(int); i++)
{
if (signal(i[sig], sig_handler) == SIG_ERR)
{
perror("Error registering signal handler\n");
}
}
}
int main(int argc, char *argv[])
{
pthread_t td_server, td_notify;
void *ret;
if(geteuid() != 0)
{
perror("You must be root to run this program!\n");
exit(EXIT_FAILURE);
}
if (argc == 2 && strcmp(argv[1], "--debug") == 0)
{
openlog(SYSLOG_IDENT, LOG_PERROR, LOG_DAEMON);
}
else
{
daemonize();
syslog_filter = LOG_NOTICE;
openlog(SYSLOG_IDENT, 0, LOG_DAEMON);
}
ssdp_init();
ssdp_notify();
if (pthread_create(&td_server, NULL, ssdp_thread_server, NULL) < 0)
{
writelog(LOG_ERR, "Fatal error unable to create ssdp_server thread!");
exit(EXIT_FAILURE);
}
if (pthread_create(&td_notify, NULL, ssdp_thread_notify, NULL) < 0)
{
writelog(LOG_ERR, "Fatal error unable to create ssdp_notify thread!");
exit(EXIT_FAILURE);
}
(void)pthread_join (td_server, &ret);
(void)pthread_join (td_notify, &ret);
return EXIT_SUCCESS;
}