syslog_in_unix.c
author viric@llimona
Thu, 06 Apr 2006 01:47:35 +0200
changeset 18 84fa30ea0b0d
parent 15 fea6e87812f0
child 26 7227789ca718
permissions -rw-r--r--
Ara sí - versió 1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
     1
#include <unistd.h> // Per les crides a fitxer
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
     2
#include <stdio.h>  // Per l'I/O de stdin/stdout
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
     3
#include <errno.h>  // Pels errors de les crides a fitxer
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
     4
#include <string.h> // Per strerror()
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
     5
#include <stdlib.h> // Per abort()
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
     6
#include <sys/types.h>  // Per crides de fitxer
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
     7
#include <sys/socket.h>  // Per les macros bool,true,false
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
     8
#include <sys/un.h>  // Per les estructures dels unix sockets
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
     9
#include <assert.h> // Per assert()
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    10
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    11
#include "rfc3164.h"
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    12
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    13
void show_help(const char * restrict program)
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    14
{
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    15
	printf("Usage: %s <unix_socket>\n", program);
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    16
}
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    17
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    18
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    19
int listen_unix(const char * restrict socketname)
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    20
{
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    21
	int socket_unix;
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    22
	struct sockaddr_un source_unix;
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    23
	int result;
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 11
diff changeset
    24
	int sockopt;
9
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    25
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    26
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    27
	socket_unix = socket(PF_UNIX, SOCK_DGRAM, 0 );
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    28
	if (socket_unix == -1)
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    29
	{
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    30
		printf("Unix socket creation error: %s\n", strerror(errno));
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    31
		abort();
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    32
	}
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    33
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 11
diff changeset
    34
	/* Allow socket reuse */
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 11
diff changeset
    35
	sockopt = 1;
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 11
diff changeset
    36
	result = setsockopt(socket_unix, SOL_SOCKET, SO_REUSEADDR, &sockopt,
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 11
diff changeset
    37
		sizeof(sockopt));
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 11
diff changeset
    38
	assert(result == 0);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 11
diff changeset
    39
9
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    40
	/* Unix socket listen address */
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    41
	memset(&source_unix, 0, sizeof(source_unix));
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    42
	source_unix.sun_family = AF_UNIX;
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    43
	strcpy(source_unix.sun_path, socketname);
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    44
	result = bind(socket_unix, (struct sockaddr *) &source_unix,
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    45
		sizeof(source_unix));
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    46
	assert(result == 0);
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    47
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    48
	/* Here we should take care of the socket permissions */
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    49
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    50
	return socket_unix;
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    51
}
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    52
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 11
diff changeset
    53
void server_loop(const char * restrict socketname)
9
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    54
{
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    55
	int socket_unix;
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    56
	int result;
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    57
	char message[MESSAGE_LENGTH+1];
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    58
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    59
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    60
	socket_unix = listen_unix(socketname);
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    61
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    62
	result = 0;
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    63
	while ((result = recv(socket_unix, message, MESSAGE_LENGTH, 0)) >=0 )
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    64
	{
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    65
		process_message(message);
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    66
		/* Debug */
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    67
		if(!strcmp(message,"close"))
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    68
			break;
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    69
	}
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    70
	result = close(socket_unix);
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    71
	if (result == -1)
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    72
	{
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    73
		printf("Unix socket close error: %s\n", strerror(errno));
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    74
		abort();
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    75
	}
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    76
	result = unlink(socketname);
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    77
	if (result == -1)
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    78
	{
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    79
		printf("Unix socket unlink error: %s\n", strerror(errno));
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    80
		abort();
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    81
	}
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    82
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    83
}
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    84
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    85
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    86
int main(int argn, char **argv)
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    87
{
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    88
	/* Processem els paràmetres d'entrada */
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    89
	if (argn != 2) {
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    90
		show_help(argv[0]);
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    91
		return 2;
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    92
	}
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    93
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    94
	server_loop(argv[1]);
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    95
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    96
	return 0;
3fd9ecdc4299 First implementation of unix sockets
viric@mandarina
parents:
diff changeset
    97
}