Handle return values of system calls
[connman] / plugins / supplicant.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #include <net/if.h>
35
36 #include <glib.h>
37
38 #include "supplicant.h"
39
40 struct supplicant_task {
41         GPid pid;
42         int ifindex;
43         char *ifname;
44         struct connman_iface *iface;
45         int socket;
46         GIOChannel *channel;
47 };
48
49 static GSList *tasks = NULL;
50
51 static struct supplicant_task *find_task(int ifindex)
52 {
53         GSList *list;
54
55         for (list = tasks; list; list = list->next) {
56                 struct supplicant_task *task = list->data;
57
58                 if (task->ifindex == ifindex) 
59                         return task;
60         }
61
62         return NULL;
63 }
64
65 static int exec_cmd(struct supplicant_task *task, char *cmd)
66 {
67         return write(task->socket, cmd, strlen(cmd));
68 }
69
70 static gboolean control_event(GIOChannel *chan,
71                                 GIOCondition cond, gpointer data)
72 {
73         struct supplicant_task *task = data;
74         char buf[256];
75         gsize len;
76         GIOError err;
77
78         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
79                 return FALSE;
80
81         memset(buf, 0, sizeof(buf));
82
83         err = g_io_channel_read(chan, buf, sizeof(buf), &len);
84         if (err) {
85                 if (err == G_IO_ERROR_AGAIN)
86                         return TRUE;
87                 return FALSE;
88         }
89
90         if (buf[0] != '<')
91                 return TRUE;
92
93         printf("[SUPPLICANT] %s\n", buf + 3);
94
95         if (g_str_has_prefix(buf + 3, "CTRL-EVENT-CONNECTED") == TRUE) {
96                 printf("[SUPPLICANT] connected\n");
97                 connman_iface_update(task->iface,
98                                         CONNMAN_IFACE_STATE_CARRIER);
99         }
100
101         if (g_str_has_prefix(buf + 3, "CTRL-EVENT-DISCONNECTED") == TRUE) {
102                 printf("[SUPPLICANT] disconnected\n");
103         }
104
105         if (g_str_has_prefix(buf + 3, "CTRL-EVENT-TERMINATING") == TRUE) {
106                 printf("[SUPPLICANT] terminating\n");
107         }
108
109         return TRUE;
110 }
111
112 static int open_control(struct supplicant_task *task)
113 {
114         struct sockaddr_un addr;
115         int sk;
116
117         printf("[SUPPLICANT] open control for %s\n", task->ifname);
118
119         sk = socket(PF_UNIX, SOCK_DGRAM, 0);
120         if (sk < 0)
121                 return -1;
122
123         memset(&addr, 0, sizeof(addr));
124         addr.sun_family = AF_UNIX;
125         snprintf(addr.sun_path, sizeof(addr.sun_path),
126                                         "%s/%s.cli", STATEDIR, task->ifname);
127         //unlink(addr.sun_path);
128
129         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
130                 close(sk);
131                 return -1;
132         }
133
134         memset(&addr, 0, sizeof(addr));
135         addr.sun_family = AF_UNIX;
136         snprintf(addr.sun_path, sizeof(addr.sun_path),
137                                         "%s/%s", STATEDIR, task->ifname);
138
139         if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
140                 close(sk);
141                 return -1;
142         }
143
144         task->socket = sk;
145
146         task->channel = g_io_channel_unix_new(sk);
147         g_io_channel_set_close_on_unref(task->channel, TRUE);
148
149         g_io_add_watch(task->channel,
150                         G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
151                                                 control_event, task);
152
153         exec_cmd(task, "ATTACH");
154         exec_cmd(task, "ADD_NETWORK");
155
156         return 0;
157 }
158
159 int __supplicant_start(struct connman_iface *iface)
160 {
161         struct ifreq ifr;
162         struct supplicant_task *task;
163         char *argv[9];
164         int sk, err;
165
166         sk = socket(PF_INET, SOCK_DGRAM, 0);
167         if (sk < 0)
168                 return -EIO;
169
170         memset(&ifr, 0, sizeof(ifr));
171         ifr.ifr_ifindex = iface->index;
172
173         err = ioctl(sk, SIOCGIFNAME, &ifr);
174
175         close(sk);
176
177         if (err < 0)
178                 return -EIO;
179
180         printf("[SUPPLICANT] start %s\n", ifr.ifr_name);
181
182         task = g_try_new0(struct supplicant_task, 1);
183         if (task == NULL)
184                 return -ENOMEM;
185
186         task->ifindex = iface->index;
187         task->ifname = strdup(ifr.ifr_name);
188         task->iface = iface;
189
190         if (task->ifname == NULL) {
191                 g_free(task);
192                 return -ENOMEM;
193         }
194
195         argv[0] = "/sbin/wpa_supplicant";
196         argv[1] = "-qq";
197         argv[2] = "-C";
198         argv[3] = STATEDIR;
199         argv[4] = "-D";
200         argv[5] = "wext";
201         argv[6] = "-i";
202         argv[7] = task->ifname;
203         argv[8] = NULL;
204
205         if (g_spawn_async(NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
206                                 NULL, NULL, &task->pid, NULL) == FALSE) {
207                 printf("Failed to spawn wpa_supplicant\n");
208                 return -1;
209         }
210
211         tasks = g_slist_append(tasks, task);
212
213         printf("[SUPPLICANT] executed with pid %d\n", task->pid);
214
215         sleep(1);
216
217         task->socket = -1;
218
219         if (open_control(task) < 0)
220                 printf("[SUPPLICANT] control failed\n");
221
222         return 0;
223 }
224
225 int __supplicant_stop(struct connman_iface *iface)
226 {
227         struct supplicant_task *task;
228         char pathname[PATH_MAX];
229
230         task = find_task(iface->index);
231         if (task == NULL)
232                 return -ENODEV;
233
234         printf("[SUPPLICANT] stop %s\n", task->ifname);
235
236         tasks = g_slist_remove(tasks, task);
237
238         exec_cmd(task, "DISABLE_NETWORK 0");
239         exec_cmd(task, "DETACH");
240
241         sleep(1);
242
243         kill(task->pid, SIGTERM);
244
245         g_io_channel_shutdown(task->channel, TRUE, NULL);
246         g_io_channel_unref(task->channel);
247
248         snprintf(pathname, sizeof(pathname),
249                                         "%s/%s.cli", STATEDIR, task->ifname);
250         unlink(pathname);
251
252         free(task->ifname);
253
254         g_free(task);
255
256         return 0;
257 }
258
259 int __supplicant_connect(struct connman_iface *iface,
260                                 const char *network, const char *passphrase)
261 {
262         struct supplicant_task *task;
263         char cmd[128];
264
265         task = find_task(iface->index);
266         if (task == NULL)
267                 return -ENODEV;
268
269         printf("[SUPPLICANT] connect %s\n", task->ifname);
270
271         exec_cmd(task, "DISABLE_NETWORK 0");
272
273         sprintf(cmd, "SET_NETWORK 0 ssid \"%s\"", network);
274         exec_cmd(task, cmd);
275
276         if (passphrase && strlen(passphrase) > 0) {
277                 exec_cmd(task, "SET_NETWORK 0 proto RSN WPA");
278                 exec_cmd(task, "SET_NETWORK 0 key_mgmt WPA-PSK");
279
280                 sprintf(cmd, "SET_NETWORK 0 psk \"%s\"", passphrase);
281                 exec_cmd(task, cmd);
282         } else {
283                 exec_cmd(task, "SET_NETWORK 0 proto RSN WPA");
284                 exec_cmd(task, "SET_NETWORK 0 key_mgmt NONE");
285         }
286
287         exec_cmd(task, "ENABLE_NETWORK 0");
288
289         return 0;
290 }