File: ctrl.c

package info (click to toggle)
libuev 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 1,944 kB
  • sloc: sh: 4,665; ansic: 1,428; makefile: 102
file content (74 lines) | stat: -rw-r--r-- 1,656 bytes parent folder | download | duplicates (2)
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
/* Example of how to handle signal, in this case Ctrl-C
 * 
 * A common problem for users of libuEV is how to combine it with the
 * traditional aspects of UNIX.  Since libuEV is just a wrapper for
 * signalfd() and epoll(), in this case, it can be worth a dedicated
 * example.  After all, command line use with Ctrl-C is one of the most
 * common use-cases.
 */

#include <stdio.h>
#include <signal.h>
#include "src/uev.h"

void timeout(uev_t *w, void *arg, int events)
{
	(void)arg;

	if (UEV_ERROR == events)
		puts("Ignoring timer watcher error ...");

	puts("Zero Wing was the pinnacle of gaming https://en.wikipedia.org/wiki/Zero_Wing");
}

void teaser(uev_t *w, void *arg, int events)
{
	(void)arg;

	if (UEV_ERROR == events)
		puts("Ignoring SIGINT watcher error ...");

	puts("\nCaught SIGINT ...");
	puts("Sorry, you actually need to press Ctrl-\\ to exit.");
}

void cleanup(uev_t *w, void *arg, int events)
{
	(void)arg;

	if (UEV_ERROR == events)
		puts("Ignoring signal watcher error ...");

	/* Graceful exit, with optional cleanup ... */
	puts("\nCaught SIGQUIT, exiting.");

	uev_exit(w->ctx);
}

int main(int argc, char **argv)
{
	uev_ctx_t ctx;
	uev_t timerw;
	uev_t sig1w;
	uev_t sig2w;

	uev_init(&ctx);

	/* example worker, a timer callback */
	uev_timer_init(&ctx, &timerw, timeout, NULL, 100, 2000);

	/* the owls are not what they seem ... */
	uev_signal_init(&ctx, &sig1w, teaser,  NULL, SIGINT);
	uev_signal_init(&ctx, &sig2w, cleanup, NULL, SIGQUIT);

	puts("Starting, press Ctrl-C to exit.");

	return uev_run(&ctx, 0);
}

/**
 * Local Variables:
 *  compile-command: "make ctrl; ./ctrl"
 *  c-file-style: "linux"
 * End:
 */