]> git.ozlabs.org Git - petitboot/blob - ui/common/discover-client.c
c0cfea0fe19a8ddae06d5b0546a2f6f504d6c369
[petitboot] / ui / common / discover-client.c
1
2 #include <errno.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <stdint.h>
7
8 #include <sys/socket.h>
9 #include <sys/un.h>
10 #include <asm/byteorder.h>
11
12 #include <talloc/talloc.h>
13 #include <log/log.h>
14
15 #include "discover-client.h"
16 #include "pb-protocol/pb-protocol.h"
17
18 struct discover_client {
19         int fd;
20         struct discover_client_ops ops;
21         int n_devices;
22         struct device **devices;
23 };
24
25 static int discover_client_destructor(void *arg)
26 {
27         struct discover_client *client = arg;
28
29         if (client->fd >= 0)
30                 close(client->fd);
31
32         return 0;
33 }
34
35 void discover_client_destroy(struct discover_client *client)
36 {
37         talloc_free(client);
38 }
39
40 static void device_add(struct discover_client *client, struct device *device)
41 {
42         client->n_devices++;
43         client->devices = talloc_realloc(client, client->devices,
44                         struct device *, client->n_devices);
45
46         client->devices[client->n_devices - 1] = device;
47         talloc_steal(client, device);
48
49         client->ops.device_add(device, client->ops.cb_arg);
50 }
51
52 static void device_remove(struct discover_client *client, const char *id)
53 {
54         struct device *device = NULL;
55         int i;
56
57         for (i = 0; i < client->n_devices; i++) {
58                 if (!strcmp(client->devices[i]->id, id)) {
59                         device = client->devices[i];
60                         break;
61                 }
62         }
63
64         if (!device)
65                 return;
66
67         /* remove the device from the client's device array */
68         client->n_devices--;
69         memmove(&client->devices[i], &client->devices[i+1],
70                         (client->n_devices - i) * sizeof(client->devices[0]));
71         client->devices = talloc_realloc(client, client->devices,
72                         struct device *, client->n_devices);
73
74         /* notify the UI */
75         client->ops.device_remove(device, client->ops.cb_arg);
76
77         talloc_free(device);
78 }
79
80 static int discover_client_process(void *arg)
81 {
82         struct discover_client *client = arg;
83         struct pb_protocol_message *message;
84         struct device *dev;
85         char *dev_id;
86         int rc;
87
88         message = pb_protocol_read_message(client, client->fd);
89
90         if (!message)
91                 return -1;
92
93         switch (message->action) {
94         case PB_PROTOCOL_ACTION_ADD:
95                 dev = talloc(client, struct device);
96
97                 rc = pb_protocol_deserialise_device(dev, message);
98                 if (rc) {
99                         pb_log("%s: no device?\n", __func__);
100                         return 0;
101                 }
102
103                 device_add(client, dev);
104                 break;
105         case PB_PROTOCOL_ACTION_REMOVE:
106                 dev_id = pb_protocol_deserialise_string(client, message);
107                 if (!dev_id) {
108                         pb_log("%s: no device id?\n", __func__);
109                         return 0;
110                 }
111                 device_remove(client, dev_id);
112                 break;
113         default:
114                 pb_log("%s: unknown action %d\n", __func__, message->action);
115         }
116
117
118         return 0;
119 }
120
121 struct discover_client* discover_client_init(struct waitset *waitset,
122         const struct discover_client_ops *ops, void *cb_arg)
123 {
124         struct discover_client *client;
125         struct sockaddr_un addr;
126
127         client = talloc(NULL, struct discover_client);
128         if (!client)
129                 return NULL;
130
131         memcpy(&client->ops, ops, sizeof(client->ops));
132         client->ops.cb_arg = cb_arg;
133
134         client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
135         if (client->fd < 0) {
136                 pb_log("%s: socket: %s\n", __func__, strerror(errno));
137                 goto out_err;
138         }
139
140         talloc_set_destructor(client, discover_client_destructor);
141
142         client->n_devices = 0;
143         client->devices = NULL;
144
145         addr.sun_family = AF_UNIX;
146         strcpy(addr.sun_path, PB_SOCKET_PATH);
147
148         if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
149                 pb_log("%s: connect: %s\n", __func__, strerror(errno));
150                 goto out_err;
151         }
152
153         waiter_register(waitset, client->fd, WAIT_IN, discover_client_process,
154                         client);
155
156         return client;
157
158 out_err:
159         talloc_free(client);
160         return NULL;
161 }
162
163 /* accessors for discovered devices */
164 int discover_client_device_count(struct discover_client *client)
165 {
166         return client->n_devices;
167 }
168
169 struct device *discover_client_get_device(struct discover_client *client,
170                 int index)
171 {
172         if (index < 0 || index >= client->n_devices)
173                 return NULL;
174
175         return client->devices[index];
176 }
177
178 static void create_boot_command(struct boot_command *command,
179                 const struct device *device __attribute__((unused)),
180                 const struct boot_option *boot_option,
181                 const struct pb_boot_data *data)
182 {
183
184         command->option_id = boot_option->id;
185         command->boot_image_file = data->image;
186         command->initrd_file = data->initrd;
187         command->boot_args = data->args;
188 }
189
190 int discover_client_boot(struct discover_client *client,
191                 const struct device *device,
192                 const struct boot_option *boot_option,
193                 const struct pb_boot_data *data)
194 {
195         struct pb_protocol_message *message;
196         struct boot_command boot_command;
197         int len, rc;
198
199         create_boot_command(&boot_command, device, boot_option, data);
200
201         len = pb_protocol_boot_len(&boot_command);
202
203         message = pb_protocol_create_message(client,
204                         PB_PROTOCOL_ACTION_BOOT, len);
205
206         if (!message)
207                 return -1;
208
209         pb_protocol_serialise_boot_command(&boot_command,
210                         message->payload, len);
211
212         rc = pb_protocol_write_message(client->fd, message);
213
214         return rc;
215 }