summaryrefslogtreecommitdiff
path: root/package/uvd/src/uvd.c
blob: 72d7980c152a44f9a7391ed4950e71be4210501f (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * daemon for version information of embedded systems
 *
 * Copyright (C) 2010 Waldemar Brodkorb <wbx@openadk.org>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h> 
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#define VERSION		"0.1"
#define LOGFILE		"/tmp/uvd.log"
#define LOCKFILE	"/tmp/uvd.lock"
#define BUFSIZE		1024
#define VERFILE		"/etc/adkversion"

void version() {
	fprintf(stdout, "uvd version %s", VERSION);
	exit(EXIT_SUCCESS);
}

static const char *log_date(void) {
	static char buf[18];
	time_t t = time(NULL);	
	strftime(buf, 18, "%b %d %H:%M:%S", localtime(&t));
	return buf;
}

void die(const char *msg) {
	fprintf(stderr, "%s\n", msg);
	exit(EXIT_FAILURE);
}

void die_log(FILE *lf, const char *msg) {
	fprintf(lf, "%s: %s\n", log_date(), msg);
	exit(EXIT_FAILURE);
}

void log_msg(FILE *lf, const char *msg) {
	fprintf(lf, "%s: %s\n", log_date(), msg);
	fflush(lf);
}


void signal_handler(int sig) {

	switch(sig) {
		case SIGHUP:
			break;
		case SIGTERM:
		case SIGINT:
		case SIGQUIT:
			unlink(LOCKFILE);
			exit(EXIT_SUCCESS);
			break;
		default:
			break;
	}
}

void usage(int argc, char *argv[]) {

	if (argc >=1) {
		fprintf(stdout, "Usage: %s [ --help | --debug | --version ]\n", argv[0]);
		fprintf(stdout, "  Options:\n");
		fprintf(stdout, "    --debug	| -d start in debug mode, don't fork\n");
		fprintf(stdout, "    --version	| -v show version\n");
		fprintf(stdout, "    --help	| -h show help\n");
		fprintf(stdout, "\n");
	}
	exit(EXIT_SUCCESS);
}


int main(int argc, char **argv) {

	pid_t pid, sid;
	int lfd, c, ss, n, res;
	FILE *lf = NULL;
	int vf;
	struct sigaction sigact;
	int daemonize = 1;
	int optval = 1;
	char buf[BUFSIZE];
	struct sockaddr_in server = { 0 };
	struct sockaddr_in clientaddr; /* client addr */
	socklen_t clientlen;

	/* options descriptor */
	static struct option longopts[] = {
		{ "debug",	no_argument,	0,	'd' },
		{ "help",	no_argument,	0,	'h' },
		{ "version",	no_argument,	0,	'v' },
		{ NULL,		0,		NULL,	0 }
	};

	while((c = getopt_long(argc, argv, "dhv", longopts, NULL)) != -1) {
		switch(c) {
			case 'h':
				usage(argc, argv);
				break;
			case 'v':
				version();
				break;
			case 'd':
				daemonize = 0;
				break;
			default:
				usage(argc, argv);
			}
	}

	if (daemonize) {
		/* Fork off the parent process */
		pid = fork();
		if (pid < 0)
			die("Can't fork process");

		/* If we got a pid, we exit the parent process */	
		if (pid > 0)
			exit(EXIT_SUCCESS);

		/* Change the file mode mask */
		umask(0);
	}

	/* Open a logfile */
	lf = fopen(LOGFILE, "a");
	if (lf == NULL)
		die("Can't open logfile");
	
	if (daemonize) {
		/* Create a new session for child process */
		if ((sid = setsid()) < 0)
			die_log(lf, "Can't create a new session for child process");

		/* Change the current working directory */
		if ((chdir("/")) < 0)
			die_log(lf, "Can't change working directory to /");
	}

	/* check if daemon already running */
	if ((lfd = open(LOCKFILE, O_RDWR | O_CREAT, 0640) < 0))
		die("Can't open or create lock file");

	if (lockf(lfd, F_TLOCK, 0) == -1)
		die("uvd already running!");

	if (daemonize) {
		/* Close out the standard file descriptors */
       		close(STDIN_FILENO);
        	close(STDOUT_FILENO);
        	close(STDERR_FILENO);
	}

	/* Handle some signals */
	sigact.sa_handler = signal_handler;
	sigact.sa_flags = 0;

	sigaction(SIGHUP, &sigact, NULL);
	sigaction(SIGTERM, &sigact, NULL);
	sigaction(SIGINT, &sigact, NULL);
	sigaction(SIGQUIT, &sigact, NULL);

	log_msg(lf, "uvd started successfully");

	/* create network socket */
	if ((ss = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
		die("can not create socket");	

	server.sin_addr.s_addr=htonl(INADDR_ANY);
	server.sin_family = AF_INET;
	server.sin_port = htons(4242);

	if (setsockopt (ss, SOL_SOCKET, SO_BROADCAST, (caddr_t) &optval, sizeof (optval)) < 0)
		die("can not set socket option");

	if (setsockopt (ss, SOL_SOCKET, SO_REUSEADDR, (caddr_t) &optval, sizeof (optval)) < 0)
		die("can not set socket option");

	if (bind(ss, (struct sockaddr *) &server, sizeof(server)) < 0)
		die("can not bind socket");

	/* loop forever */
	while(1) {
		clientlen = sizeof(clientaddr);
		n = recvfrom(ss, buf, BUFSIZE , 0, (struct sockaddr *)&clientaddr, &clientlen);
		if (n < 0) 
			die_log(lf, "error reading from client");

		buf[n] = 0;
		res = strncmp(buf, "version", n);
		if (res > 0) {
			log_msg(lf, "been asked for version information");
			if ((vf = open(VERFILE, O_RDONLY)) < 0)
				die_log(lf, "unable to open version file");

			ssize_t num;
			do {
				num = read(vf, &buf, sizeof(buf));
				buf[num] = '\0';
				if (sendto(ss, buf, num, 0, (struct sockaddr *) &clientaddr, sizeof(clientaddr)) < 0)
					die_log(lf, "can not send data");
			} while (num > 0);

			close(vf);
		}
	}
	close(ss);
	return(0);
}