unix_writer.c
author viric@llimona
Tue, 30 May 2006 01:10:00 +0200
changeset 53 667cd5966695
parent 15 fea6e87812f0
permissions -rw-r--r--
Finer message processing - now only at file and screen output appear '\n'.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
     1
#include <stdio.h>  // Per l'I/O de stdin/stdout
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
     2
#include <sys/socket.h>  // Per les macros bool,true,false
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
     3
#include <sys/un.h>  // Per les estructures dels unix sockets
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
     4
#include <errno.h>  // Pels errors de les crides a fitxer
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
     5
#include <string.h> // Per strerror()
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
     6
#include <stdlib.h> // Per abort()
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
     7
#include <assert.h> // Per assert()
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
     8
#include <unistd.h> // Per les crides a fitxer
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
     9
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    10
#include "rfc3164.h" // Per MESSAGE_LENGTH
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    11
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    12
void show_help(const char * restrict program)
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    13
{
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    14
	printf("Usage: %s <unix_socket>\n", program);
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    15
}
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    16
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    17
int connect_unix(const char * restrict socketname)
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    18
{
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    19
	int socket_unix;
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    20
	struct sockaddr_un source_unix;
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    21
	int result;
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    22
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    23
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    24
	socket_unix = socket(PF_UNIX, SOCK_DGRAM, 0 );
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    25
	if (socket_unix == -1)
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    26
	{
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    27
		printf("Unix socket creation error: %s\n", strerror(errno));
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    28
		abort();
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 12
diff changeset
    29
		/* Unreachable */
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 12
diff changeset
    30
		return -1;
12
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    31
	}
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    32
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    33
	/* Unix socket listen address */
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    34
	memset(&source_unix, 0, sizeof(source_unix));
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    35
	source_unix.sun_family = AF_UNIX;
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    36
	strcpy(source_unix.sun_path, socketname);
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    37
	result = connect(socket_unix, (struct sockaddr *) &source_unix,
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    38
		sizeof(source_unix));
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    39
	if (result == -1)
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    40
	{
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    41
		printf("Unix socket connect error: %s\n", strerror(errno));
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    42
		abort();
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 12
diff changeset
    43
		/* Unreachable */
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 12
diff changeset
    44
		return -1;
12
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    45
	}
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    46
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    47
	/* Here we should take care of the socket permissions */
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    48
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    49
	return socket_unix;
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    50
}
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    51
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 12
diff changeset
    52
void writer_loop(const char * restrict socketname)
12
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    53
{
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    54
	int socket_unix;
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    55
	int result;
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    56
	char message[MESSAGE_LENGTH+1];
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    57
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    58
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    59
	socket_unix = connect_unix(socketname);
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    60
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    61
	while (fgets(message, MESSAGE_LENGTH, stdin) != NULL )
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    62
	{
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    63
		send(socket_unix, message, strlen(message), 0);
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    64
		/* Strip the last \n */
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    65
		if (strlen(message) > 1)
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    66
			message[strlen(message)-1] = '\0';
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    67
		/* Allow exiting the program */
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    68
		if(!strcmp(message,"close"))
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    69
			break;
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    70
	}
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    71
	result = close(socket_unix);
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    72
	if (result == -1)
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    73
	{
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    74
		printf("Unix socket close error: %s\n", strerror(errno));
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    75
		abort();
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    76
	}
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    77
}
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    78
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    79
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    80
int main(int argn, char **argv)
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    81
{
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    82
	/* Processem els parĂ metres d'entrada */
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    83
	if (argn != 2) {
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    84
		show_help(argv[0]);
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    85
		return 2;
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    86
	}
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    87
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    88
	writer_loop(argv[1]);
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    89
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    90
	return 0;
46ce13615fd8 Added a unix writer for testing the unix-socket syslog.
viric@mandarina
parents:
diff changeset
    91
}