]> git.ozlabs.org Git - petitboot/blob - ui/common/discover-client.c
discover-client: interact directly with waitset
[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
87         message = pb_protocol_read_message(client, client->fd);
88
89         if (!message)
90                 return -1;
91
92         switch (message->action) {
93         case PB_PROTOCOL_ACTION_ADD:
94                 dev = pb_protocol_deserialise_device(client, message);
95                 if (!dev) {
96                         pb_log("%s: no device?\n", __func__);
97                         return 0;
98                 }
99
100                 device_add(client, dev);
101                 break;
102         case PB_PROTOCOL_ACTION_REMOVE:
103                 dev_id = pb_protocol_deserialise_string(client, message);
104                 if (!dev_id) {
105                         pb_log("%s: no device id?\n", __func__);
106                         return 0;
107                 }
108                 device_remove(client, dev_id);
109                 break;
110         default:
111                 pb_log("%s: unknown action %d\n", __func__, message->action);
112         }
113
114
115         return 0;
116 }
117
118 struct discover_client* discover_client_init(struct waitset *waitset,
119         const struct discover_client_ops *ops, void *cb_arg)
120 {
121         struct discover_client *client;
122         struct sockaddr_un addr;
123
124         client = talloc(NULL, struct discover_client);
125         if (!client)
126                 return NULL;
127
128         memcpy(&client->ops, ops, sizeof(client->ops));
129         client->ops.cb_arg = cb_arg;
130
131         client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
132         if (client->fd < 0) {
133                 pb_log("%s: socket: %s\n", __func__, strerror(errno));
134                 goto out_err;
135         }
136
137         talloc_set_destructor(client, discover_client_destructor);
138
139         client->n_devices = 0;
140         client->devices = NULL;
141
142         addr.sun_family = AF_UNIX;
143         strcpy(addr.sun_path, PB_SOCKET_PATH);
144
145         if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
146                 pb_log("%s: connect: %s\n", __func__, strerror(errno));
147                 goto out_err;
148         }
149
150         waiter_register(waitset, client->fd, WAIT_IN, discover_client_process,
151                         client);
152
153         return client;
154
155 out_err:
156         talloc_free(client);
157         return NULL;
158 }
159
160 /* accessors for discovered devices */
161 int discover_client_device_count(struct discover_client *client)
162 {
163         return client->n_devices;
164 }
165
166 struct device *discover_client_get_device(struct discover_client *client,
167                 int index)
168 {
169         if (index < 0 || index >= client->n_devices)
170                 return NULL;
171
172         return client->devices[index];
173 }
174
175 static void create_boot_command(struct boot_command *command,
176                 const struct device *device __attribute__((unused)),
177                 const struct boot_option *boot_option,
178                 const struct pb_boot_data *data)
179 {
180
181         command->option_id = boot_option->id;
182         command->boot_image_file = data->image;
183         command->initrd_file = data->initrd;
184         command->boot_args = data->args;
185 }
186
187 int discover_client_boot(struct discover_client *client,
188                 const struct device *device,
189                 const struct boot_option *boot_option,
190                 const struct pb_boot_data *data)
191 {
192         struct pb_protocol_message *message;
193         struct boot_command boot_command;
194         int len, rc;
195
196         create_boot_command(&boot_command, device, boot_option, data);
197
198         len = pb_protocol_boot_len(&boot_command);
199
200         message = pb_protocol_create_message(client,
201                         PB_PROTOCOL_ACTION_BOOT, len);
202
203         if (!message)
204                 return -1;
205
206         pb_protocol_serialise_boot_command(&boot_command,
207                         message->payload, len);
208
209         rc = pb_protocol_write_message(client->fd, message);
210
211         return rc;
212 }