2
* ivykis, an event handling library
3
* Copyright (C) 2010 Lennert Buytenhek
4
* Dedicated to Marija Kulikova.
6
* This library is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License version
8
* 2.1 as published by the Free Software Foundation.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Lesser General Public License version 2.1 for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License version 2.1 along with this library; if not, write to the
17
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18
* Boston, MA 02110-1301, USA.
29
struct iv_popen_request popen_req;
31
struct iv_fd popen_fd;
32
struct iv_timer closeit;
35
static void done(struct req *req, int timeout)
37
iv_popen_request_close(&req->popen_req);
39
iv_fd_unregister(&req->popen_fd);
40
close(req->popen_fd.fd);
43
iv_timer_unregister(&req->closeit);
46
static void got_data(void *_req)
48
struct req *req = _req;
52
ret = read(req->popen_fd.fd, buf, sizeof(buf));
55
fprintf(stderr, "got EOF\n");
57
} else if (errno != EAGAIN && errno != EINTR) {
66
fwrite(buf, 1, ret, stdout);
69
static void do_close(void *_req)
71
struct req *req = _req;
73
printf("%p: timeout expired, closing the popen request\n", req);
77
static void open_child_request(struct req *req)
81
IV_POPEN_REQUEST_INIT(&req->popen_req);
82
req->popen_req.file = "/usr/bin/vmstat";
83
req->argv[0] = "/usr/bin/vmstat";
86
req->popen_req.argv = req->argv;
87
req->popen_req.type = "r";
88
f = iv_popen_request_submit(&req->popen_req);
90
printf("submitted the popen request, fd is %d\n", f);
92
IV_FD_INIT(&req->popen_fd);
94
req->popen_fd.cookie = req;
95
req->popen_fd.handler_in = got_data;
96
iv_fd_register(&req->popen_fd);
98
IV_TIMER_INIT(&req->closeit);
100
req->closeit.expires = iv_now;
101
req->closeit.expires.tv_sec += 5;
102
req->closeit.cookie = req;
103
req->closeit.handler = do_close;
104
iv_timer_register(&req->closeit);
114
for (i = 0; i < NUM; i++)
115
open_child_request(&req[i]);