summaryrefslogtreecommitdiffstats
path: root/tests/uxsockrcvr.c
blob: 551f0ef3384ff719f5256c27513679fc52d0e810 (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
/* receives messages from a specified unix sockets and writes
 * output to specfied file.
 *
 * Command line options:
 * -s name of socket (required)
 * -o name of output file (stdout if not given)
 * -l add newline after each message received (default: do not add anything)
 *
 * Part of the testbench for rsyslog.
 *
 * Copyright 2010 Rainer Gerhards and Adiscon GmbH.
 *
 * This file is part of rsyslog.
 *
 * Rsyslog 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 3 of the License, or
 * (at your option) any later version.
 *
 * Rsyslog 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 Rsyslog.  If not, see <http://www.gnu.org/licenses/>.
 *
 * A copy of the GPL can be found in the file "COPYING" in this distribution.
 */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <getopt.h>
#include <sys/un.h>
#include <netdb.h>
 
char *sockName = NULL;
int sock;
int addNL = 0;


/* called to clean up on exit
 */
void
cleanup(void)
{
        unlink(sockName);
	close(sock);
}


void
doTerm(int __attribute__((unused)) signum)
{
	exit(1);
}


void
usage(void)
{
	fprintf(stderr, "usage: uxsockrcvr -s /socket/name -o /output/file -l\n"
			"-l adds newline after each message received\n"
			"-s MUST be specified\n"
			"if -o ist not specified, stdout is used\n");
	exit(1);
}


int
main(int argc, char *argv[])
{
	int opt;
        int rlen;
	FILE *fp = stdout;
        unsigned char data[128*1024];
        struct  sockaddr_un addr; /* address of server */
        struct  sockaddr from;
        socklen_t fromlen;

	if(argc < 2) {
		fprintf(stderr, "error: too few arguments!\n");
		usage();
	}

	while((opt = getopt(argc, argv, "s:o:l")) != EOF) {
		switch((char)opt) {
                case 'l':
			addNL = 1;
			break;
                case 's':
			sockName = optarg;
			break;
                case 'o':
			if((fp = fopen(optarg, "w")) == NULL) {
				perror(optarg);
				exit(1);
			}
			break;
		default:usage();
		}
	}

	if(sockName == NULL) {
		fprintf(stderr, "error: -s /socket/name must be specified!\n");
		exit(1);
	}

        if(signal(SIGTERM, doTerm) == SIG_ERR) {
		perror("signal(SIGTERM, ...)");
		exit(1);
	}
        if(signal(SIGINT, doTerm) == SIG_ERR) {
		perror("signal(SIGINT, ...)");
		exit(1);
	}

        /*      Create a UNIX datagram socket for server        */
        if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
                perror("server: socket");
                exit(1);
        }

	atexit(cleanup);

        /*      Set up address structure for server socket      */
        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_UNIX;
        strcpy(addr.sun_path, sockName);
        
        if (bind(sock, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
                close(sock);
                perror("server: bind");
                exit(1);
        }        

	/* we now run in an endless loop. We do not check who sends us
	 * data. This should be no problem for our testbench use.
	 */
	while(1) {
		fromlen = sizeof(from);
		rlen = recvfrom(sock, data, 2000, 0, &from, &fromlen);
		if(rlen == -1) {
		       perror("uxsockrcvr : recv\n");
		       exit(1);
		} else {
		      fwrite(data, 1, rlen, fp);
			if(addNL)
				fputc('\n', fp);
		}
        }

        return 0;
}