summaryrefslogtreecommitdiff
path: root/sys/src/ape/lib/bsd/getitimer.c
blob: 167a04eeb1a201f13daccf23310e23d942afe0f0 (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
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

typedef struct Timer Timer;
struct Timer {
	int pid, signal;
	struct itimerval itimer;
};

Timer timers[3] = {
	{0, SIGALRM},
	{0, SIGVTALRM},
	{0, SIGPROF},
};

void
timerloop(Timer *timer, const struct timeval tval)
{
	pid_t ppid;
	struct timespec t, s;

	ppid = getppid();
	t.tv_sec = tval.tv_sec;
	t.tv_nsec = tval.tv_usec*1000;
	for(;;){
		nanosleep(&t, &s);
		kill(ppid, timer->signal);
	}
}

int
setitimer(int which, const struct itimerval *new, struct itimerval *curr)
{
	pid_t pid;
	int status;
	Timer *timer;

	if(which < 0 || which >= 3){
		errno = EINVAL;
		return -1;
	}

	timer = timers+which;
	if(timer->pid != 0){
		kill(timer->pid, SIGKILL);
		waitpid(timer->pid, &status, 0);
	}

	switch(pid = fork()){
	default:
		timer->pid = pid;
		if(curr != NULL)
			*curr = timer->itimer;
		timer->itimer = *new;
		break;
	case -1:
		errno = EFAULT;
		return -1;
	case 0:
		timerloop(timer, new->it_interval);
		exit(0);
	}
	return 0;
}

int
getitimer(int which, struct itimerval *curr)
{
	Timer *timer;

	if(which < 0 || which >= 3){
		errno = EINVAL;
		return -1;
	}

	timer = timers+which;
	*curr = timer->itimer;
	return 0;
}