File: diskd.c

package info (click to toggle)
fdutils 5.5-20060227-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,156 kB
  • ctags: 678
  • sloc: ansic: 6,280; sh: 3,087; makefile: 299; sed: 4
file content (110 lines) | stat: -rw-r--r-- 2,383 bytes parent folder | download | duplicates (9)
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
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/fd.h>
#include <linux/fdreg.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <linux/major.h>
#include "enh_options.h"


int eioctl(int fd, int command,void * param, char *emsg)
{
  int r;
  if ((r=ioctl(fd,command,param))<0 )
    {
      perror(emsg);
      exit(1);
    }
  return r;
}

static char procFd[80];


int doPoll(int fd,
	   struct floppy_drive_params *dpr,
	   struct floppy_drive_struct *state)
{
	if(! (dpr->flags & FD_SILENT_DCL_CLEAR)) {
		/* Work around a bug in floppy driver when silent dcl is not
		   set */
		struct floppy_raw_cmd raw_cmd;
		int fd2;
		fd2=open(procFd, 3 | O_NDELAY);
		if(fd2 != -1)
			close(fd2);
		/* Perform "dummy" rawcmd to flush out newchange */
		raw_cmd.flags = FD_RAW_NEED_DISK;
		raw_cmd.cmd_count = 0;
		raw_cmd.track = 0;
		ioctl(fd, FDRAWCMD, &raw_cmd, "rawcmd");
	}
	eioctl(fd, FDPOLLDRVSTAT, state, "reset");
	return state->flags;
}

int main(int argc, char **argv)
{
	int mask=0;
	int fd=-2;
	char *command=0;
	int interval=10;
	int ch;
	struct timeval timval;
	struct floppy_drive_struct state;
	struct floppy_drive_params dpr;

	struct enh_options optable[] = {
	{ 'd', "drive", 1, EO_TYPE_FILE_OR_FD, 3, 0,
		  (void *) &fd,
		  "drive to be polled"},
		
	{ 'e', "exec", 1, EO_TYPE_STRING, 0, 0,
		  (void *) &command,
		  "shell command to be executed after disk insertion" },
		
	{ 'i', "interval", 1, EO_TYPE_LONG, 0, 0, 
		  (void *) &interval,
		  "set polling interval (in tenth of seconds)" },
	{ '\0', 0 }		
	};

	while((ch=getopt_enh(argc, argv, optable, 
			     0, &mask, "drive") ) != EOF ){
		if ( ch== '?' ){
			fprintf(stderr,"exiting\n");
			exit(1);
		}
		printf("unhandled option %d\n", ch);
		exit(1);
	}

	if ( fd == -2 )
		fd = open("/dev/fd0", 3 | O_NDELAY);
	if ( fd < 0 ){
		perror("can't open floppy drive");
		exit(1);
	}

	eioctl(fd,FDGETDRVPRM,(void *) &dpr, "Get drive parameters");
	sprintf(procFd, "/proc/self/fd/%d", fd);

	doPoll(fd, &dpr, &state);
	while (state.flags & (FD_VERIFY|FD_DISK_NEWCHANGE)) {
		timval.tv_sec = interval / 10;
		timval.tv_usec = (interval % 10) * 100000;
		select(0, 0, 0, 0, &timval);
		doPoll(fd, &dpr, &state);
	} 
	close(fd);
	if ( command)
		system(command);
	exit(0);
}