summaryrefslogtreecommitdiff
path: root/release/src/router/busybox/ktab.c
blob: 4f3b2561833a865fb84c6d6be168ed44d477700e (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 *   ktab implementation for busybox by Broadcom Corporation
 *   Based on ktab-0.4.0
 *   Directives and variables unsupported, minimal chatter
 *
 *   KTAB - Keith program TABing, src/ktab.c
 *   Copyright (C) 1998 Keith Fralick
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 1, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <unistd.h>
#include "busybox.h"

/*
 * config.h
 */

/*
 * MIN_PROGRAM_CHECK
 *  
 * Define this to the minimum time in which a script may contain
 * for checking a program's existance.  This number is in seconds.
 */
#define	MIN_PROGRAM_CHECK	30

/*
 * DEFAULT_TIME
 *
 * Define this value to the value in which shall be default if a
 * user supplies the -p option for a KTAB.  This number is in
 * minutes.
 */
#define	DEFAULT_TIME		3	

/*
 * READ_WRITE_PERMISSION
 *
 * Define this if you want your users to have read *AND* write
 * permissions in order to ktab a process under their UID.
 */
#define READ_WRITE_PERMISSION

/*
 * CHECK_RUN_FROM_FEET
 *
 * Define this to the value that you want KTAB to wait until
 * the period that it is off its feet in order to check the
 * desired process for the very first time.  This number is
 * in seconds.
 */
#define CHECK_RUN_FROM_FEET	10	

/*
 * SUPRESS_PROGRAM_DATA
 *
 * Define this if you want to attempt to supress any data that
 * the programs you run might try to print to the screen.  However,
 * this is only an attempt, if you undef this, you may still use
 * the '-' character in front of the path to your programs in
 * the script.  However, defining this, it will attempt to supress
 * whether or not you have the '-' character.
 */
#undef SUPRESS_PROGRAM_DATA 

/*
 * version.h
 */

#define KTAB_VERSION	"0.4.0"

/*
 * struct.h
 */

typedef struct processes Proc;
typedef struct shortcuts Short;

struct	processes	{
	char	path[1024];
	char	args[1024];		/* this is for arguments v0.3.1 */
	time_t	chktime;
	time_t	lastchk;
	struct	processes *next;
} *proc = NULL;

struct	shortcuts	{
	char	name[256];
	char	real[1024];
	int	num;
	struct	shortcuts *next;
} *shorty = NULL;

/*
 * ktab.c
 */

extern	void	add_process();
extern	void	do_script();
extern	void	check_jobs();
extern	void	rehash();
extern	void	loop_procs();
extern	void	file_exists(char *);

extern	Short	*make_short();

FILE	*i, *log, *ma;

#define	mycmp	strcasecmp

#define O_RW	 "rw"
#define O_APPEND  "a"
#ifndef READ_WRITE_PERMISSION
#define	O_RDONLY "r"
#else
#define O_RDONLY O_RW
#endif

int	dupp = DEFAULT_TIME * 60;
char	*logfile = NULL;
char	*prog = NULL, *sfile = NULL;
char	*curpath, *os, *shell, *home, *myhost, *trm, *luser;
char	tmp[1024];

int	opt_kill = 0;

int	review = 0, cntt = 0;

static	pid_t	w;

int ktab_main(int argc, char *argv[])
{
	time_t a = time(NULL) + CHECK_RUN_FROM_FEET;
	int opt;

	curpath = getenv("PWD");

	while ((opt = getopt(argc, argv, "f:p:r:")) != -1) {
		switch (opt) {
		case 'f':
			sfile = strdup(optarg);
			break;
		case 'p':
			prog = strdup(optarg);
			break;
		case 'r':
			sfile = strdup(optarg);
			review = 1;
			break;
		case 'v':
			printf("KTAB\tBy: Keith Fralick\tv%s\n\n", KTAB_VERSION);
			/* fall through */
		default:
			show_usage();
		}
	}

	if(!((i = fopen(sfile, O_RDONLY)) ||
		(i = fopen(prog, O_RDONLY))))
		perror_msg_and_die(sfile ? sfile : prog);
	
	if (prog)
		fclose(i);
	else
		do_script();
	if(review)
	{
		review = 0;
		perror_msg_and_die("%d errors\n", cntt);
	}
	w = getpid() + 1;
	argv[1] = argv[2] = NULL;
	if (daemon(1,0) < 0)
		exit(0);
	(void)signal(SIGHUP, rehash);
	if(prog)
		while (1)
		{
			if(curpath) chdir(curpath);
			while(time(NULL) <= a) sleep(1);
			if(prog)
				system(prog);
			a+=dupp;
		}
	if(sfile)
		while (1)
		{
			if(curpath) chdir(curpath);
			check_jobs();
			sleep(1);			
		}
	rehash(0);
	return 0;
}

void	add_process(int t, char pathto[1024], char args[1024])
{
	Proc 	*new, *current;
	char	tog[1024];	

	if(!(new = (Proc *)malloc(sizeof(Proc))))
		perror_msg_and_die("malloc");
	if(new != NULL)
		new->next = NULL;
	if(args)
		(void)sprintf(pathto, "%s %s", pathto, args);
	else
		strcpy(tog, pathto);
	strcpy(new->path, pathto);
	new->chktime = t;
	new->lastchk = 0;
	if(proc == NULL) 
		proc = new;
	else
	{
		current = proc;
		while(current->next)
			current = current->next;
		current->next = new;
	}
}

void	do_script()
{
	int	c = 0, p = 0;
	int	mvalue = 0;
	char	*f, *s;

	while(fgets(tmp, sizeof(tmp), i))
	{
		if(!tmp || *tmp == ' ' || !strchr(tmp, ' ') ||
			*tmp == '#')
			continue; 
		if(strlen(tmp) > 2048)
			continue;
		while(strchr(tmp, '\n'))
			*strchr(tmp, '\n') = '\0';
		if(*tmp == 's')
			mvalue = 1;
		else if(*tmp == 'm')
			mvalue = 60;
		else if(*tmp == 'h')
			mvalue = 3600;
		else if(*tmp == 'd')
			mvalue = 3600 * 24;
		f = tmp;
		s = strchr(tmp, ' ');
		*(s)++ = '\0';
		if(mvalue)
			f++;
		else
			mvalue = 60;
		p = atoi(f);
		p*=mvalue; 
		if (p == 0)
			p = mvalue ? mvalue : (DEFAULT_TIME * 60);
		if(p < MIN_PROGRAM_CHECK)
			perror_msg_and_die("minimum time %d s", MIN_PROGRAM_CHECK);
		if(mvalue == 60 && p < CHECK_RUN_FROM_FEET)
			p = MIN_PROGRAM_CHECK * 60;
		if(strchr(s, ' '))
		{
			f = s;
			s = strchr(f, ' ');
			*(s)++ = '\0';
		}
		else
			f = s;
		file_exists(f);
		add_process(p, f, s ? s : NULL);		
		c++;
	}
	if(!c)
	 exit (-1);
	fclose(i);
}

void	file_exists(char *filename)
{
	FILE	*a;

	if(*filename == '-')
		filename++;
	if(!((a = fopen(filename, O_RDONLY))))
		perror_msg_and_die(filename);
	fclose(a);
}

void	check_jobs()
{
	Proc	*z;
	char	*kind;

	for(z = proc; z; z = z->next)
	{
		if(z->lastchk == 0)
			z->lastchk = time(NULL) + CHECK_RUN_FROM_FEET;
		else
			if(time(NULL) - z->lastchk >= z->chktime)
			{			
				strcpy(tmp, z->path);
#ifdef 	SUPRESS_PROGRAM_DATA
				kind = tmp;
				if(*kind == '-') kind++;
				strcpy(tmp, kind);
				strcat(tmp, ">> /tmp/blah");
# else
				if(*tmp == '-')	
					{
						kind = tmp;
						kind++;
						strcpy(tmp, kind);
						strcat(tmp, ">> /tmp/blah");
					}
#endif
				system(tmp);
				z->lastchk = time(NULL);
			} 
	}
}

/*
 * When we get signal SIGHUP, we just die.
 * The reason being, why flush out the 
 * structure and start again?  If they want
 * to reload something, do it right.
 */
void	rehash(x)
{
	if(prog)
		free(prog);
	if(sfile)
		free(sfile);
	exit(0);
}

Short	*make_short()
{
        Short *new, *current;
        if(!(new = (Short *)malloc(sizeof(Short))))
		perror_msg_and_die("malloc");
        if(new != NULL)
                new->next = NULL;
        if(shorty == NULL)
                shorty = new;
        else
        {
                current = shorty;
                while(current->next)
                        current = current->next;
                current->next = new;
        }
	return (new);
}