diff options
Diffstat (limited to 'src/ftee/main.c')
-rw-r--r-- | src/ftee/main.c | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/src/ftee/main.c b/src/ftee/main.c new file mode 100644 index 000000000..4dca3cf48 --- /dev/null +++ b/src/ftee/main.c | |||
@@ -0,0 +1,228 @@ | |||
1 | #include "ftee.h" | ||
2 | #include <errno.h> | ||
3 | #include <sys/types.h> | ||
4 | #include <sys/stat.h> | ||
5 | #include <unistd.h> | ||
6 | #define MAXBUF 512 | ||
7 | |||
8 | static unsigned char buf[MAXBUF]; | ||
9 | |||
10 | static FILE *out_fp = NULL; | ||
11 | static int out_cnt = 0; | ||
12 | static int out_max = 500 * 1024; | ||
13 | |||
14 | static void log_close(void) { | ||
15 | if (out_fp) { | ||
16 | fclose(out_fp); | ||
17 | out_fp = NULL; | ||
18 | } | ||
19 | } | ||
20 | |||
21 | static void log_rotate(const char *fname) { | ||
22 | struct stat s; | ||
23 | int index = strlen(fname); | ||
24 | char *name1 = malloc(index + 2 + 1); | ||
25 | char *name2 = malloc(index + 2 + 1); | ||
26 | if (!name1 || !name2) | ||
27 | errExit("malloc"); | ||
28 | strcpy(name1, fname); | ||
29 | strcpy(name2, fname); | ||
30 | fflush(0); | ||
31 | |||
32 | // delete filename.5 | ||
33 | sprintf(name1 + index, ".5"); | ||
34 | if (stat(name1, &s) == 0) { | ||
35 | int rv = unlink(name1); | ||
36 | if (rv == -1) | ||
37 | perror("unlink"); | ||
38 | } | ||
39 | |||
40 | // move files 1 to 4 down one position | ||
41 | sprintf(name2 + index, ".4"); | ||
42 | if (stat(name2, &s) == 0) { | ||
43 | int rv = rename(name2, name1); | ||
44 | if (rv == -1) | ||
45 | perror("rename"); | ||
46 | } | ||
47 | |||
48 | sprintf(name1 + index, ".3"); | ||
49 | if (stat(name1, &s) == 0) { | ||
50 | int rv = rename(name1, name2); | ||
51 | if (rv == -1) | ||
52 | perror("rename"); | ||
53 | } | ||
54 | |||
55 | sprintf(name2 + index, ".2"); | ||
56 | if (stat(name2, &s) == 0) { | ||
57 | /* coverity[toctou] */ | ||
58 | int rv = rename(name2, name1); | ||
59 | if (rv == -1) | ||
60 | perror("rename"); | ||
61 | } | ||
62 | |||
63 | sprintf(name1 + index, ".1"); | ||
64 | if (stat(name1, &s) == 0) { | ||
65 | int rv = rename(name1, name2); | ||
66 | if (rv == -1) | ||
67 | perror("rename"); | ||
68 | } | ||
69 | |||
70 | // move the first file | ||
71 | if (out_fp) | ||
72 | fclose(out_fp); | ||
73 | |||
74 | out_fp = NULL; | ||
75 | if (stat(fname, &s) == 0) { | ||
76 | int rv = rename(fname, name1); | ||
77 | if (rv == -1) | ||
78 | perror("rename"); | ||
79 | } | ||
80 | |||
81 | free(name1); | ||
82 | free(name2); | ||
83 | } | ||
84 | |||
85 | static void log_write(const unsigned char *str, int len, const char *fname) { | ||
86 | assert(fname); | ||
87 | |||
88 | if (out_fp == NULL) { | ||
89 | out_fp = fopen(fname, "w"); | ||
90 | if (!out_fp) { | ||
91 | fprintf(stderr, "Error: cannot open log file %s\n", fname); | ||
92 | exit(1); | ||
93 | } | ||
94 | out_cnt = 0; | ||
95 | } | ||
96 | |||
97 | // rotate files | ||
98 | out_cnt += len; | ||
99 | if (out_cnt >= out_max) { | ||
100 | log_rotate(fname); | ||
101 | |||
102 | // reopen the first file | ||
103 | if (out_fp) | ||
104 | fclose(out_fp); | ||
105 | out_fp = fopen(fname, "w"); | ||
106 | if (!out_fp) { | ||
107 | fprintf(stderr, "Error: cannot open log file %s\n", fname); | ||
108 | exit(1); | ||
109 | } | ||
110 | out_cnt = len; | ||
111 | } | ||
112 | |||
113 | fwrite(str, len, 1, out_fp); | ||
114 | fflush(0); | ||
115 | } | ||
116 | |||
117 | |||
118 | // return 1 if the file is a directory | ||
119 | static int is_dir(const char *fname) { | ||
120 | assert(fname); | ||
121 | if (*fname == '\0') | ||
122 | return 0; | ||
123 | |||
124 | // if fname doesn't end in '/', add one | ||
125 | int rv; | ||
126 | struct stat s; | ||
127 | if (fname[strlen(fname) - 1] == '/') | ||
128 | rv = stat(fname, &s); | ||
129 | else { | ||
130 | char *tmp; | ||
131 | if (asprintf(&tmp, "%s/", fname) == -1) { | ||
132 | fprintf(stderr, "Error: cannot allocate memory, %s:%d\n", __FILE__, __LINE__); | ||
133 | exit(1); | ||
134 | } | ||
135 | rv = stat(tmp, &s); | ||
136 | free(tmp); | ||
137 | } | ||
138 | |||
139 | if (rv == -1) | ||
140 | return 0; | ||
141 | |||
142 | if (S_ISDIR(s.st_mode)) | ||
143 | return 1; | ||
144 | |||
145 | return 0; | ||
146 | } | ||
147 | |||
148 | // return 1 if the file is a link | ||
149 | static int is_link(const char *fname) { | ||
150 | assert(fname); | ||
151 | if (*fname == '\0') | ||
152 | return 0; | ||
153 | |||
154 | struct stat s; | ||
155 | if (lstat(fname, &s) == 0) { | ||
156 | if (S_ISLNK(s.st_mode)) | ||
157 | return 1; | ||
158 | } | ||
159 | |||
160 | return 0; | ||
161 | } | ||
162 | |||
163 | |||
164 | |||
165 | |||
166 | |||
167 | static void usage(void) { | ||
168 | printf("Usage: ftee filename\n"); | ||
169 | } | ||
170 | |||
171 | int main(int argc, char **argv) { | ||
172 | if (argc < 2) { | ||
173 | fprintf(stderr, "Error: please provide a filename to store the program output\n"); | ||
174 | usage(); | ||
175 | exit(1); | ||
176 | } | ||
177 | char *fname = argv[1]; | ||
178 | |||
179 | |||
180 | // do not accept directories, links, and files with ".." | ||
181 | if (strstr(fname, "..") || is_link(fname) || is_dir(fname)) { | ||
182 | fprintf(stderr, "Error: invalid output file. Links, directories and files with \"..\" are not allowed.\n"); | ||
183 | exit(1); | ||
184 | } | ||
185 | |||
186 | struct stat s; | ||
187 | if (stat(fname, &s) == 0) { | ||
188 | // check permissions | ||
189 | if (s.st_uid != getuid() || s.st_gid != getgid()) { | ||
190 | fprintf(stderr, "Error: the output file needs to be owned by the current user.\n"); | ||
191 | exit(1); | ||
192 | } | ||
193 | |||
194 | // check hard links | ||
195 | if (s.st_nlink != 1) { | ||
196 | fprintf(stderr, "Error: no hard links allowed.\n"); | ||
197 | exit(1); | ||
198 | } | ||
199 | } | ||
200 | |||
201 | // check if we can append to this file | ||
202 | /* coverity[toctou] */ | ||
203 | FILE *fp = fopen(fname, "a"); | ||
204 | if (!fp) { | ||
205 | fprintf(stderr, "Error: cannot open output file %s\n", fname); | ||
206 | exit(1); | ||
207 | } | ||
208 | fclose(fp); | ||
209 | |||
210 | |||
211 | // preserve the last log file | ||
212 | log_rotate(fname); | ||
213 | |||
214 | setvbuf (stdout, NULL, _IONBF, 0); | ||
215 | while(1) { | ||
216 | int n = read(0, buf, sizeof(buf)); | ||
217 | if (n < 0 && errno == EINTR) | ||
218 | continue; | ||
219 | if (n <= 0) | ||
220 | break; | ||
221 | |||
222 | fwrite(buf, n, 1, stdout); | ||
223 | log_write(buf, n, fname); | ||
224 | } | ||
225 | |||
226 | log_close(); | ||
227 | return 0; | ||
228 | } | ||