syslog_in_npipe.c
author viric@llimona
Thu, 06 Apr 2006 01:47:35 +0200
changeset 18 84fa30ea0b0d
parent 16 de00e98450eb
child 26 7227789ca718
permissions -rw-r--r--
Ara sí - versió 1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
     1
#include <unistd.h> // Per les crides a fitxer
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
     2
#include <stdio.h>  // Per l'I/O de stdin/stdout
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
     3
#include <errno.h>  // Pels errors de les crides a fitxer
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
     4
#include <string.h> // Per strerror()
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
     5
#include <stdlib.h> // Per abort()
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
     6
#include <sys/types.h>  // Per crides de fitxer
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
     7
#include <sys/stat.h>  // Per crides de fitxer
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
     8
#include <fcntl.h>  // Per crides de fitxer
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
     9
#include <stdbool.h>  // Per les macros bool,true,false
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    10
#include <unistd.h>  // Pel lockf()
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    11
#include <fcntl.h>  // Pel lockf()
0
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
    12
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
    13
#include "rfc3164.h"
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
    14
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
    15
1
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    16
void showHelp(const char * restrict program)
0
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
    17
{
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
    18
	printf("Usage: %s <named_fifo>\n", program);
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
    19
}
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
    20
1
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    21
bool isNamedPipe(const char * restrict path)
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    22
{
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    23
	struct stat tmpStatus;
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    24
	int result;
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    25
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    26
	result = stat(path, &tmpStatus);
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    27
	if (result == -1)
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    28
	{
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    29
		fprintf(stderr, "Error trying to stat the file: %s\n", 
1
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    30
			strerror(errno));
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    31
		abort();
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    32
	}
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    33
	
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    34
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    35
	if (S_ISFIFO(tmpStatus.st_mode))
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    36
		return true;
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    37
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    38
	return false;
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    39
}
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
    40
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    41
int pipe_server(const char * restrict filename)
0
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
    42
{
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
    43
	int fd_entrada;
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
    44
	char missatge[MESSAGE_LENGTH];
5
2e2e16e59af9 No es fan servir funcions de rfc*.c ja.
viric@llimona
parents: 2
diff changeset
    45
	int status;
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    46
	struct flock blocking;
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    47
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    48
	blocking.l_type = F_RDLCK;
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    49
	blocking.l_whence = SEEK_SET;
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    50
	blocking.l_start = 0;
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    51
	blocking.l_len = 0;
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    52
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    53
	while(true)
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    54
	{
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    55
		/* Obrim la named fifo síncrona. */
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    56
		fd_entrada = open(filename, O_RDONLY);
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    57
		if (fd_entrada < 0) {
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    58
			fprintf(stderr, "Error opening the named pipe: %s\n",
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    59
				strerror(errno));
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    60
			return 1;
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    61
		}
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    62
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    63
		status = fcntl(fd_entrada, F_SETLKW, &blocking);
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    64
		if (fd_entrada < 0) {
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    65
			fprintf(stderr, "Error locking the named pipe: %s\n",
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    66
				strerror(errno));
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    67
			return 1;
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    68
		}
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    69
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    70
		while ((status = read(fd_entrada, missatge,
14
ef38057e925a Corregida una mida de buffer.
viric@llimona
parents: 8
diff changeset
    71
			MESSAGE_LENGTH)) > 0)
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    72
		{
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    73
			process_message(missatge);
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    74
			/* Debug */
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    75
			if(!strcmp(missatge,"close"))
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    76
			{
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    77
				/* Code duplication !! */
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    78
				blocking.l_type = F_UNLCK;
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    79
				status = fcntl(fd_entrada, F_SETLKW, &blocking);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    80
				if (fd_entrada < 0) {
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    81
					fprintf(stderr,
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    82
						"Error unlocking the named "
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    83
						"pipe: %s\n",
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    84
						strerror(errno));
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    85
					abort();
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    86
				}
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    87
				close (fd_entrada);
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    88
				/* End of code duplication */
16
de00e98450eb Versió 1 quasi per entregar. Falta molt RFC per implementar.
viric@llimona
parents: 15
diff changeset
    89
				return 0;
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    90
			}
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    91
		}
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    92
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    93
		if (status < 0)
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    94
		{
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    95
			fprintf(stderr, "Error reading the named pipe: %s\n",
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    96
				strerror(errno));
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
    97
			/* Code duplication !! */
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    98
			blocking.l_type = F_UNLCK;
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
    99
			status = fcntl(fd_entrada, F_SETLKW, &blocking);
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   100
			if (fd_entrada < 0) {
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
   101
				fprintf(stderr,
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
   102
					"Error unlocking the named pipe: %s\n",
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   103
					strerror(errno));
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   104
				abort();
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   105
			}
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   106
			close (fd_entrada);
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
   107
			/* End of code duplication */
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   108
			return 1;
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   109
		}
16
de00e98450eb Versió 1 quasi per entregar. Falta molt RFC per implementar.
viric@llimona
parents: 15
diff changeset
   110
		/* Code duplication !! */
de00e98450eb Versió 1 quasi per entregar. Falta molt RFC per implementar.
viric@llimona
parents: 15
diff changeset
   111
		blocking.l_type = F_UNLCK;
de00e98450eb Versió 1 quasi per entregar. Falta molt RFC per implementar.
viric@llimona
parents: 15
diff changeset
   112
		status = fcntl(fd_entrada, F_SETLKW, &blocking);
de00e98450eb Versió 1 quasi per entregar. Falta molt RFC per implementar.
viric@llimona
parents: 15
diff changeset
   113
		if (fd_entrada < 0) {
de00e98450eb Versió 1 quasi per entregar. Falta molt RFC per implementar.
viric@llimona
parents: 15
diff changeset
   114
			fprintf(stderr,
de00e98450eb Versió 1 quasi per entregar. Falta molt RFC per implementar.
viric@llimona
parents: 15
diff changeset
   115
				"Error unlocking the named pipe: %s\n",
de00e98450eb Versió 1 quasi per entregar. Falta molt RFC per implementar.
viric@llimona
parents: 15
diff changeset
   116
				strerror(errno));
de00e98450eb Versió 1 quasi per entregar. Falta molt RFC per implementar.
viric@llimona
parents: 15
diff changeset
   117
			abort();
de00e98450eb Versió 1 quasi per entregar. Falta molt RFC per implementar.
viric@llimona
parents: 15
diff changeset
   118
		}
de00e98450eb Versió 1 quasi per entregar. Falta molt RFC per implementar.
viric@llimona
parents: 15
diff changeset
   119
		close (fd_entrada);
de00e98450eb Versió 1 quasi per entregar. Falta molt RFC per implementar.
viric@llimona
parents: 15
diff changeset
   120
		/* End of code duplication */
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   121
	}
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   122
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   123
}
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   124
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   125
int main(int argn, char **argv)
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   126
{
0
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
   127
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
   128
	/* Processem els paràmetres d'entrada */
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
   129
	if (argn != 2) {
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
   130
		showHelp(argv[0]);
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
   131
		return 2;
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
   132
	}
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
   133
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
   134
	/* Comprovem que existeix i és una named pipe */
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
   135
	/* !!! */
1
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
   136
	if(!isNamedPipe(argv[1])) // Abortarà si falla
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
   137
	{
15
fea6e87812f0 Now everything works - the rfc3164 message parser must be improved.
viric@llimona
parents: 14
diff changeset
   138
		fprintf(stderr, "The file is not of a named pipe.\n");
1
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
   139
		abort();
cd184f6ef0d8 The named pipe opening and reading works.
viric@llimona
parents: 0
diff changeset
   140
	};
0
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
   141
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
   142
8
6d48acc561ca Process message improved.
viric@mandarina
parents: 5
diff changeset
   143
	return pipe_server(argv[1]);
0
4a369e7c2ce8 Initial.
viric@llimona
parents:
diff changeset
   144
}