File: exec.c

package info (click to toggle)
multitail 4.2.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 660 kB
  • ctags: 812
  • sloc: ansic: 12,046; makefile: 91; sh: 19
file content (326 lines) | stat: -rw-r--r-- 7,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#define _LARGEFILE64_SOURCE     /* required for GLIBC to enable stat64 and friends */
#include <sys/types.h>
#include <regex.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>

#include "mt.h"
#include "error.h"
#include "utils.h"
#include "mem.h"
#include "term.h"
#include "my_pty.h"
#include "globals.h"


int start_tail(char *filename, char retry_open, char follow_filename, int initial_tail, int *pipefd)
{
	pid_t pid;

	/* start child process */
	if ((pid = fork()) == 0)
	{
		char *pars[9], *posix_version = NULL;
		int npars = 0;
		char nlines_buffer[32];

		setpgid(0,0);

		setup_for_childproc(pipefd[1], 0, "dumb");

		/* create command for take last n lines & follow and start tail */

		/* the command to start */
		pars[npars++] = tail;

		/* Linux' tail has the --retry option, but not all
		 * other UNIX'es have this, so I implemented it
		 * myself
		 */
		if (retry_open)
		{
			/* FIXME: *//* #if defined(linux) */
#if 0
			pars[npars++] = "--retry";
#else
			int rc;
			struct stat64 buf;

			for(;;)
			{
				rc = stat64(filename, &buf);
				if (rc == -1)
				{
					if (errno != ENOENT)
					{
						fprintf(stderr, "Error while looking for file %s: %d\n", filename, errno);
						exit(EXIT_FAILURE);
					}
				}
				else if (rc == 0)
					break;

				usleep(WAIT_FOR_FILE_DELAY * 1000);
			}
#endif
		}

		/* get the posix compliance level */
		posix_version = getenv("_POSIX2_VERSION");

		/* follow filename is only supported on *BSD and Linux */
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(linux) || defined(__CYGWIN__) || defined(__APPLE__) || defined(__GNU__)
		if (follow_filename)
		{
#if defined(linux) || defined(__CYGWIN__) || defined(__GNU__)
			pars[npars++] = "--follow=name";
#elif defined(__OpenBSD__)
			pars[npars++] = "-f";
#else
			pars[npars++] = "-F";
#endif
			/* number of lines to tail initially */
			pars[npars++] = "-n";
			snprintf(nlines_buffer, sizeof(nlines_buffer), "%d", initial_tail);
			pars[npars++] = nlines_buffer;
		}
		else
#endif
		{
			/* check the posix compliance level */
			if (posix_version && atoi(posix_version) >= 200112)
			{
				pars[npars++] = "-f";
				pars[npars++] = "-n";
				snprintf(nlines_buffer, sizeof(nlines_buffer), "%d", initial_tail);
				pars[npars++] = nlines_buffer;
			}
			else
			{
				/* number of lines to tail initially and 'follow file' ('f') */
				snprintf(nlines_buffer, sizeof(nlines_buffer), "-%dlf", initial_tail);
				pars[npars++] = nlines_buffer;
			}
		}

		/* add the filename to monitor */
		pars[npars++] = filename;
		pars[npars] = NULL;

		/* run tail! */
		if (-1 == execvp(pars[0], pars)) error_exit("start_tail: execvp of tail failed\n");

		/* if execlp returns, an error occured */
		error_exit("start_tail: error while starting process!\n");
	}

	return pid;
}

void start_proc_signal_handler(int sig)
{
	if (sig != SIGTERM) error_exit("unexpected signal %d in start_proc_signalhandler\n", sig);

	stop_process(tail_proc);

	exit(1);
}

int start_proc(proginfo *cur, int initial_tail)
{
	cur -> n_runs++;

	if (cur -> is_command)
	{
		int fd_master, fd_slave;

		/* allocate pseudo-tty & fork*/
		cur -> pid = get_pty_and_fork(&fd_master, &fd_slave);
		if (-1 == cur -> pid) error_exit("start_proc: failed to create pseudo-tty & fork\n");

		/* child? */
		if (cur -> pid == 0)
		{
			setpgid(0,0);

			/* reset signal handler for SIGTERM */
			signal(SIGTERM, SIG_DFL);

			/* sleep if requested and only when 2nd or 3d (etc.) execution time */
			if (cur -> restart && cur -> first == 0)
				sleep(cur -> restart);

			/* connect slave-fd to stdin/out/err */
			setup_for_childproc(fd_slave, 1, term_t_to_string(cur -> term_emul));

			/* start process */
			if (-1 == execlp(shell, shell, "-c", cur -> filename, (void *)NULL)) error_exit("start_proc: execlp of %s failed\n", cur -> filename);

			/* if execlp returns, an error occured */
			error_exit("start_proc: error while starting process!\n");
		}

#if defined(sun) || defined(__sun) || defined(AIX) || defined(_HPUX_SOURCE) || defined(OSF1) || defined(scoos)
		/* these platforms only have the slave-fd available in the childprocess
		 *                  * so don't try to close it as the parent process doesn't have it
		 *
		 */
#else
		if (myclose(fd_slave) == -1) error_exit("start_proc: error closing slave-fd (pseudo tty, fd %d)\n", fd_slave);
#endif

		/* remember master-fd (we'll read from that one) */
		cur -> fd = fd_master;
		cur -> wfd = fd_master;

		/* next time, sleep */
		cur -> first = 0;
	}
	else
	{
		int pipefd[2];

		/* create a pipe, will be to child-process */
		if (-1 == pipe(pipefd)) error_exit("start_proc: error creating pipe\n");

		if (cur -> check_interval)
		{
			/* start the process that will check every 'interval' seconds
			 * if a matching file with a more recent mod-time is available
			 */
			cur -> pid = fork();
			if (cur -> pid == 0)
			{
				char *cur_file = NULL, *new_file;

				setpgid(0,0);

				signal(SIGTERM, start_proc_signal_handler);

				for(;;)
				{
					/* find (new) file */
					new_file = find_most_recent_file(cur -> filename, cur_file);

					/* if find_most_recent_file returned NOT null, a file was found
					 * which is more recent
					 */
					if (new_file != NULL)
					{
						/* if there was a previous file, see if it is different
						 * from the new filename. this should always be the case!
						 */
						if (cur_file && strcmp(new_file, cur_file) != 0)
						{
							LOG("killing process for %s\n", cur_file);
							stop_process(tail_proc);
						}

						/* remember new filename */
						myfree(cur_file);
						cur_file = new_file;

						LOG("new_file: %s\n", cur_file);

						/* and start a proc process */
						tail_proc = start_tail(cur_file, cur -> retry_open, cur -> follow_filename, initial_tail, pipefd);
						if (tail_proc == -1)
						{
							LOG("failed starting process for file %s: %d\n", cur_file, errno);
							break;
						}
					}
					else
					{
						LOG("no file found for pattern %s\n", cur -> filename);
					}

					sleep(cur -> check_interval);
				}

				LOG("stopped checking for file pattern %s\n", cur -> filename);

				exit(1);
			}
		}
		else
		{
			cur -> pid = start_tail(cur -> filename, cur -> retry_open, cur -> follow_filename, initial_tail, pipefd);
		}

		cur -> fd = pipefd[0];
		cur -> wfd = pipefd[1];
	}

	if (cur -> pid > -1)
		return 0;

	return -1;
}

int execute_program(char *execute, char bg)
{
	int status;
	pid_t child;

	if (bg)
	{
		/* to prevent meltdowns, only a limited number of
		 * processes can be executed
		 */
		if (n_children >= MAX_N_SPAWNED_PROCESSES)
			return 0;
	}
	else
		endwin();

	child = fork();
	if (child == 0)
	{
		setpgid(0,0);

		if (bg)
			setup_for_childproc(open_null(), 1, "dumb");

		/* start process */
		if (-1 == execlp(shell, shell, "-c", execute, (void *)NULL)) error_exit("execute_program: execlp of %s failed\n", execute);

		/* if execlp returns, an error occured */
		error_exit("execute_program: error while starting process!\n");
	}
	else if (child == -1)
	{
		error_exit("execute_program: failed to fork child process\n");
	}

	if (bg)
	{
		/* remember this childprocess: we'll see if it has
		 * exited in the main-loop
		 */
		children_list[n_children++] = child;
	}
	else
	{
		/* wait for the childprocess to exit */
		if (waitpid(child, &status, 0) == -1)
			error_exit("execute_program: failt waiting for process to exit!\n");

		/* restore (n)curses */
		mydoupdate();
	}

	return 0;
}

void init_children_reaper(void)
{
	/* init list of pids to watch for exit */
	memset(children_list, 0x00, sizeof(children_list));
}