author | viric@llimona |
Fri, 16 Jun 2006 18:45:24 +0200 | |
changeset 62 | 39bf7ecd7b21 |
parent 53 | 667cd5966695 |
permissions | -rw-r--r-- |
39
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
1 |
#include <sys/types.h> |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
2 |
#include <string.h> |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
3 |
#include <stdbool.h> |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
4 |
#include <stdio.h> |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
5 |
#include <unistd.h> |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
6 |
#include <errno.h> |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
7 |
#include <assert.h> |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
8 |
#include <fcntl.h> |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
9 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
10 |
#include "syslog.h" |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
11 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
12 |
/* Prototypes */ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
13 |
static int init_handle(const char * restrict name); |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
14 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
15 |
/* Local globals */ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
16 |
static int out_file; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
17 |
static bool file_enabled = false; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
18 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
19 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
20 |
/* Returns -2 when the file wasn't initialized by config, 0 when all was ok, |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
21 |
* -1 on more important error */ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
22 |
int init_out_file() |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
23 |
{ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
24 |
char name[MAX_STRING]; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
25 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
26 |
assert (get_config(LOG_FILE, name, MAX_STRING) > 0); |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
27 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
28 |
if (strncmp(name, "disabled", MAX_STRING) == 0) |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
29 |
return -2; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
30 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
31 |
return init_handle(name); |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
32 |
} |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
33 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
34 |
/* Returns 0 when all was ok, -1 on error */ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
35 |
static int init_handle(const char * restrict name) |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
36 |
{ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
37 |
/* We must specify the mode, because we add O_CREAD */ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
38 |
out_file = open(name, O_WRONLY | O_APPEND | O_CREAT, 0600); |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
39 |
if (out_file == -1) |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
40 |
{ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
41 |
fprintf(stderr, "Error opening the log file: %s\n", |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
42 |
strerror(errno)); |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
43 |
return -1; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
44 |
} |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
45 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
46 |
file_enabled = true; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
47 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
48 |
return 0; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
49 |
} |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
50 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
51 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
52 |
/* Return -2 when the file is not enabled. Otherwise, the output of send().*/ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
53 |
int write_out_file(const char * restrict buf, const int len) |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
54 |
{ |
53
667cd5966695
Finer message processing - now only at file and screen output appear '\n'.
viric@llimona
parents:
39
diff
changeset
|
55 |
int res, res2; |
39
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
56 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
57 |
if (!file_enabled) |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
58 |
return -2; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
59 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
60 |
res = write(out_file, buf, len); |
53
667cd5966695
Finer message processing - now only at file and screen output appear '\n'.
viric@llimona
parents:
39
diff
changeset
|
61 |
res2 = write(out_file, "\n", 1); |
667cd5966695
Finer message processing - now only at file and screen output appear '\n'.
viric@llimona
parents:
39
diff
changeset
|
62 |
if (res == -1 || res2 == -1) |
39
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
63 |
{ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
64 |
fprintf(stderr, "Error writing to the log file: %s\n", |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
65 |
strerror(errno)); |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
66 |
} |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
67 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
68 |
return res; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
69 |
} |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
70 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
71 |
/* Return -2 when the file is not enabled. Otherwise, the output of close().*/ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
72 |
int close_out_file() |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
73 |
{ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
74 |
int res; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
75 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
76 |
if (!file_enabled) |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
77 |
return -2; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
78 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
79 |
res = close(out_file); |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
80 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
81 |
if (res == -1) |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
82 |
{ |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
83 |
fprintf(stderr, "Error closing the log file: %s\n", |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
84 |
strerror(errno)); |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
85 |
} |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
86 |
file_enabled = false; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
87 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
88 |
return res; |
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
89 |
|
60858d13b22c
Log file output written in a C module. Now the kernel doesn't check the errors
viric@mandarina
parents:
diff
changeset
|
90 |
} |