-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_monitor.c
More file actions
49 lines (40 loc) · 938 Bytes
/
state_monitor.c
File metadata and controls
49 lines (40 loc) · 938 Bytes
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
/*
* state_monitor.c
*
* Script for monitoring the state of a block device.
* Copyright (c) 2017 Fabian Baumanis <fbaumanis@suse.de>
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
void signal_handler(int);
int main(void){
char buffer[16];
int sel_ret, fd;
fd_set rfds;
fd = open("/sys/block/sdb/device/state", O_RDONLY);
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
signal(SIGINT, signal_handler);
while (1){
sel_ret = select(fd+1, NULL, NULL, &rfds, NULL);
printf("%d\n", sel_ret);
if (sel_ret == 1){
read(fd, buffer, 32);
printf("Buffer value: %s\n", buffer);
lseek(fd,0L,SEEK_SET);
} else if (sel_ret == -1 ){
printf("select syscall failed with %s\n", sel_ret);
}
}
return 0;
}
void signal_handler(int sig)
{
printf("Interrupt was sent, exiting..\n");
exit(1);
}