5d5dcffff5d7c07d4cbc6e45514f8e3234ad91f6
[gregoa/zavai.git] / src / uevent.vala
1 /*
2  * uevent - zavai uevent handling
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 using GLib;
22
23 namespace zavai {
24 namespace uevent {
25
26 class UEvent : Service
27 {
28     protected IOChannel sock;
29     protected uint sock_watch = 0;
30     public Omhacks.UEvent.Event event_data;
31     public signal void event();
32
33     public UEvent()
34     {
35         //event_data = Omhacks.UEvent.Event();
36     }
37
38     protected bool on_input_data(IOChannel source, IOCondition condition)
39     {
40         if (condition != IOCondition.IN) return true;
41
42         Omhacks.UEvent.read(sock.unix_get_fd(), ref event_data);
43         event();
44
45         return true;
46     }
47
48     /// Activate the service
49     protected override void start()
50     {
51         if (started) return;
52
53         int sock_fd = Omhacks.UEvent.open();
54         if (sock_fd < 0)
55         {
56             zavai.log.error("UEvent: error getting socket");
57             return;
58         }
59
60         sock = new IOChannel.unix_new(sock_fd);
61         sock_watch = sock.add_watch(IOCondition.IN, on_input_data);
62
63         base.start();
64     }
65
66     /// Deactivate the service
67     protected override void stop()
68     {
69         if (!started) return;
70         Source.remove(sock_watch);
71         try {
72             sock.shutdown(false);
73         } catch (IOChannelError e) {
74             zavai.log.error("When closing UEvent socket: " + e.message);
75         }
76         sock = null;
77         base.stop();
78     }
79 }
80
81 UEvent uevent;
82
83 public void init()
84 {
85     if (Posix.getuid() == 0)
86         uevent = new UEvent();
87     else
88         uevent = null;
89 }
90
91 }
92 }