File: getstream.c

package info (click to toggle)
getstream 20081204-1.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 364 kB
  • ctags: 927
  • sloc: ansic: 4,913; sh: 76; makefile: 62
file content (180 lines) | stat: -rw-r--r-- 3,219 bytes parent folder | download | duplicates (3)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

#include <sys/poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>

#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <event.h>

#include "getstream.h"
#include "config.h"
#include "libhttp.h"

#ifdef DEBUG
static int	guardcounter;

void *guard_thread(void *mtp) {
	pthread_t	mt=(pthread_t) mtp;
	struct timeval	tv;

	guardcounter=0;

	while(1) {
		tv.tv_sec=1;
		tv.tv_usec=0;
		select(0, NULL, NULL, NULL, &tv);

		if (guardcounter==0) {
			pthread_kill(mt, SIGABRT);
			pthread_exit(NULL);
		}
		guardcounter=0;
	}
}

static void init_guard_evtimer(void );

static void guard_evtimer(int fd, short event, void *arg) {
	guardcounter++;
	init_guard_evtimer();
}

static void init_guard_evtimer(void ) {
	static struct event	gevent;
	static struct timeval	tv;

	tv.tv_usec=50000;
	tv.tv_sec=00;

	evtimer_set(&gevent, guard_evtimer, &gevent);
	evtimer_add(&gevent, &tv);
}

void init_guard_thread(void ) {
	pthread_t	mt, gt;

	init_guard_evtimer();
	mt=pthread_self();
	pthread_create(&gt, NULL, guard_thread, (void *) mt);
}
#endif


/*
 * When profiling one needs to call "exit" or return to main.
 * As we are running in a wheel like a Hamster there is no way
 * returning to main so we unconditionally call exit after a timeout
 *
 */
static void terminate_timeout(int fd, short event, void *arg) {
	exit(0);
}

static void terminate_init(int timeout) {
	static struct event	event;
	static struct timeval	tv;

	tv.tv_usec=0;
	tv.tv_sec=timeout;

	evtimer_set(&event, terminate_timeout, &event);
	evtimer_add(&event, &tv);

}


static void usage(void ) {
	fprintf(stderr, "-c <config file> -d -t <timeout>\n");
	exit(-1);
}

struct http_server	*hserver;

int main(int argc, char **argv) {
	extern char		*optarg;
	char			ch;
	int			timeout=0;
	GList			*al;
	struct config_s		*config=NULL;

	while((ch=getopt(argc, argv, "c:dt:")) != -1) {
		switch(ch) {
			case 'c':
				config=readconfig(optarg);
				if (!config)
					exit(1);
				break;
			case 'd':
				logwrite_inc_level();
				break;
			case 't':
				timeout=strtol(optarg, NULL, 10);
				break;
			default:
				usage();
				break;
		}
	}
	if (!config)
		exit(-1);

	/* Initialize libevent */
	event_init();

	if (config->http_port) {
		hserver=http_init(config->http_port);
		if (!hserver) {
			fprintf(stderr, "Could not create http server on port %d\n", config->http_port);
			exit(-1);
		}
	}

	/* In case of profiling we want to call exit after a timeout */
	if (timeout)
		terminate_init(timeout);

	al=g_list_first(config->adapter);
	while(al) {
		struct adapter_s	*a=al->data;
		GList			*sl=g_list_first(a->streams);

		/* Tune to the one and only transponder */
		fe_tune_init(a);

		/* Initialize demux0 dvr0 and co */
		dmx_init(a);
		dvr_init(a);

		/* Init all streams */
		while(sl) {
			struct stream_s	*stream=sl->data;
			stream_init(stream);
			sl=g_list_next(sl);
		}

		al=g_list_next(al);
	}

#ifdef DEBUG
	init_guard_thread();
#endif

	/* Pigs can fly */
	event_dispatch();

	return 0;
}