File: jslaunch.c

package info (click to toggle)
jslaunch 2.0-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 156 kB
  • ctags: 132
  • sloc: ansic: 838; sh: 169; makefile: 110
file content (329 lines) | stat: -rw-r--r-- 7,743 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
327
328
329
/* jslaunch 2.0: a joystick reset daemon
   Copyright (C) Sander Pronk, 1998
   
   
   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 2 of the License, 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 <stdlib.h>

#include <unistd.h>

#include <asm/types.h>
#include "kernel-types.h"
#include <asm/io.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <time.h>

#ifdef DEBUG
#include <stdio.h>
#endif  

#include "jslaunch.h"

int  ioperm(unsigned  long,  unsigned  long, int);
/* I've supplied the prototype manually, because different
   distributions seem to have this at different places */

int main(int argc, char *argv[])
{
    int i;
    
    char *msk[NMASK]; /* the strings with executables */
    
    int joymask=MASKALL; /* bit mask for the joystick port */
    
    int port; /* data read in from port */
    char *cmd; /* command to be executed */

    int polltime=DEFAULTPOLL; /* polling time */
    int bounce=BOUNCETIME; /* wait time for actual read */

    int dmn=FALSE; /* whether to be a daemon */

    char *lockfile=NULL; /* path for lock file */

    int ret; /* return code for readargs */
    pid_t pid;  /* pid as returned by fork */

#ifdef DEBUG
    printf("%s\n%d\n",EXPN,strlen(EXPN));
    /* I did not use printf in the standard program because
       it introduces a memory overhead of a couple of pages. 
       That's why the strings include string length etc. */
#endif /* DEBUG */

    for(i=0;i<NMASK;i++) /* reset the masks */
	msk[i]=NULL;

    ret=readargs(argc, argv, msk, &polltime, &bounce, &joymask, &dmn, 
		 &lockfile);
    if (ret) /* if everything did not go as planned while reading
	      the arguments */
    {
	if (ret==2)
	    write(STDERR_FILENO,EXPN,EXPNLEN);
	exit(EXIT_FAILURE);
    }

    if (dmn) /* if we want to become a daemon */
    {
	/* become a daemon */
	pid=fork(); 
	if (pid>0)
	    exit(EXIT_SUCCESS); /* fork was succesful */
	if (pid<0)
	{
	    write(STDERR_FILENO,NOFORK,NOFORKLEN); 
	    exit(EXIT_FAILURE);
	}
	/* if pid==0 this is the child process */
    }

#if !defined(__sparc__) && !defined(__powerpc__) && !defined(__m68k__)
    if (ioperm(PORT,PORTLEN,PERMLEVEL)) 
	/* get permission for joystick port */
    {
	write(STDERR_FILENO,NOPERM,NOPERMLEN);
	exit(EXIT_FAILURE);
    }
#endif

    if ( setuid(getuid()) || setgid(getgid()) )
	/* lose our privileges as fast as we can. 
	   It's best NOT to use sudo with jslaunch, but to make
	   it a suid executable, because with sudo there may be 
	   a way to execute arbitrary 
	   commands as root for ordinary users */
    {
	write(STDERR_FILENO,UIDFAIL,UIDFAILLEN);
	exit(EXIT_FAILURE);
    }

    port=(~inb(PORT)&joymask) >> SHIFTVAL;
    if ( msk[port] )
	 /* test whether current mask triggers something */
    {
	write(STDERR_FILENO,ACTPRESSED,ACTPRESSEDLEN);
	exit(EXIT_FAILURE);
    }

    if (dmn)
    {
	setsid(); /* lose our term */
	chdir("/"); /* lose dependency on mounted fs */
    }

    if (lockfile) /* after we've given up root perms,
		     we can now safely set up a lock file */
    {
	setlock(lockfile);
    }

    if (dmn)
    {
	close(STDIN_FILENO);
	close(STDOUT_FILENO);
	close(STDERR_FILENO);	/* close all term dependecies,
				 the last error messages.
				 After this no more error messages
				 can be printed */
    }


    while(1) /* the actual loop */
    {
	usleep(polltime); /* wait... */

	port=inb(PORT)&joymask; /* get input from joystick interface */

	if ( (port)!=joymask ) /* whether one of the buttons is
					  pressed */
	{
	    usleep(bounce);
	    /* wait for delta E*delta t>= h/2 
	     (I'm writing this just before a quantum physics exam :-) */
	    port= ((~inb(PORT))&joymask) >> SHIFTVAL;
	    /* we do the negation because the joystick gives the
	       inverse value of a button press (ie. button NOT pressed = 1)*/

	    cmd=msk[port];
	    if (cmd) /* whether we've hit one of the 
			masks */
	    {
		system(cmd); /* do the thing... */
		while((inb(PORT) & MASKALL)!=MASKALL) /* wait for release */
		    usleep(polltime);  /* we don't want 100% cpu time
					  consumed by this program */
	    }
	}
    }

    return EXIT_SUCCESS;
}

int readargs(int argc, char *argv[], char *msk[], int *polltime,
	     int *bounce, int *joymask, int *dmn, char **lockfile)
{
    int i=1; /* arg counter, we start at argv[1]; */
    int cmds=0; /* number of -r options */
    int newmask; /* a new joystick mask, for -r etc. */

    if (argc==1) /* no arguments, return immediately */
	return 2;

    while ( i<argc )
    {
	if (argv[i][0]!='-') /* check whether all arguments start 
				with '-' */
	    return 2;

	switch (argv[i][1])
	{
	    case 'p': /* poll time */
		if (!argv[++i])
		    goto fwopt;
		if ( (*polltime=readtime(argv[i])) < 0 )
		    return 1;
		break;
	    case 'w': /* wait time */
		if (!argv[++i])
		    goto fwopt;
		if ( (*bounce=readtime(argv[i])) < 0 )
		    return 1;
		break;
	    case 'r': /* run */
		if (!argv[i+2])
		    goto fwopt;
		newmask=readmask(argv[++i]);
		if (!newmask)
		    return 1;
		if (msk[newmask]) /* whether action is already 
				     occupied */
		{
		    write(STDERR_FILENO,ACTTWICE,ACTTWICELEN);
		    return 1;
		}
		msk[newmask]=argv[++i]; /* set new command */
		cmds++;
		break;
	    case 'i': /* ignore */
		if (argc <= i+1)
		    goto fwopt;
		newmask=readmask(argv[++i]);
		if (!newmask)
		    return 1;
		*joymask= ((~newmask)&MASKMASK) << SHIFTVAL;
		break;
	    case 'd': /* daemon */
		*dmn=TRUE;
		break;
	    case 'l': /* lock */
		if (!argv[++i])
		    goto fwopt;
		*lockfile=argv[i];
		break;
	    default: /* wrong arg type */
		return 2;
	}
	i++;
    }

    if (!cmds)
    {
	write(STDERR_FILENO,NOCOMMAND,NOCOMMANDLEN);
	return 1;
    }

    return 0;

 fwopt:
    write(STDERR_FILENO,FEWOPTS,FEWOPTSLEN);
    return 1;
}


int readmask(const char *str)
{
    int curbut; /* current new button  */
    int curmask=0; /* current mask */

    while (*str)
    {
	curbut= (*str)-'1';
	if ( (curbut<0) || (curbut>3) )
	{
	    write(STDERR_FILENO,WRONGBUT,WRONGBUTLEN);
	    return 0;
	}
	if ( (curmask >> curbut) & 1 )
	{
	    write(STDERR_FILENO,BUTTWICE,BUTTWICELEN);
	    return 0;
	}
	curmask |= 1 << curbut;
	str++;
    }

    curmask &= MASKMASK;
    return curmask;
}

int readtime(const char *st)

{
    int t_read;

    t_read=atoi(st);
    
    if  ( (t_read<=0) || (t_read>POLLTIME_MAX) )
    {
	write(STDERR_FILENO,TIMEERR,TIMEERRLEN);
	return -1;
    }
    return t_read*MICRO_IN_MILLI;
}

void setlock(char *lockfile)
{
    int lock_fd;
    struct flock lck;

    lock_fd=open(lockfile, O_RDWR | O_CREAT, 
		 S_IRUSR | S_IWUSR | S_IRGRP |
		 S_IROTH); /* open file */
    if (lock_fd<0)
    {
	write(STDERR_FILENO,OPENFAIL,OPENFAILLEN);
	exit(EXIT_FAILURE);
    }
    lck.l_type=F_WRLCK; /* exclusive write lock */
    lck.l_start=0;
    lck.l_whence=SEEK_SET;
    lck.l_len=0; /* lock the whole file */
    lck.l_pid=getpid(); /* set our pid, so if another proc is reading 
			   the lock status, it gets our pid */
    if (fcntl(lock_fd,F_SETLK,&lck)) /* set lock */
    {
	write(STDERR_FILENO,LOCKFAIL,LOCKFAILLEN);
	exit(EXIT_FAILURE);
    }
}