]> git.ozlabs.org Git - petitboot/blob - utils/pb-event.c
pb-event: move send code to a separate function
[petitboot] / utils / pb-event.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #define _GNU_SOURCE
24 #include <assert.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <sys/un.h>
34
35 #include "discover/user-event.h"
36
37 #if defined(DEBUG)
38 #define DBG(_args...) do {fprintf(stderr, _args); fflush(stderr); } while (0)
39 #else
40 static inline int __attribute__ ((format (printf, 1, 2))) DBG(
41         __attribute__((unused)) const char *fmt, ...) {return 0; }
42 #endif
43
44 static void print_version(void)
45 {
46         printf("pb-event (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
47 }
48
49 static void print_usage(void)
50 {
51         print_version();
52         printf(
53 "Usage: pb-event [-h] [event...]\n"
54 "\n"
55 "       Send a single petitboot user event to the petitboot discover server.\n"
56 "       Events can be read from stdin, or provided on the command line.\n"
57 "       User events must have the following format:\n"
58 "\n"
59 "         (add|remove)@device-id [name=value] [image=value] [args=value]\n"
60 "\n"
61 "       When read from stdin, components are separated by NUL chars\n"
62 "\n"
63 "Examples:\n"
64 "\n"
65 "    args:\n"
66 "       pb-event add@/net/eth0 name=netboot image=tftp://192.168.1.10/vmlinux\n"
67 "       pb-event remove@/net/eth0\n"
68 "\n"
69 "    stdin:\n"
70 "       printf 'add@/net/eth0\\0name=netboot\\0image=tftp://10.0.0.2/vmlinux\\0' \\\n"
71 "           | pb-event\n"
72 "       printf 'remove@/net/eth0\\0' | pb-event\n"
73 "\n");
74 }
75
76 static const char *err_max_size = "pb-event: message too large "
77                                         "(%zu byte max)\n";
78
79 static ssize_t parse_event_args(int n, char * const * args,
80                 char *buf, size_t max_len)
81 {
82         ssize_t arg_len, total_len;
83         const char *arg;
84         int i;
85
86         total_len = 0;
87
88         for (i = 0; i < n; i++) {
89                 arg = args[i];
90                 arg_len = strlen(arg);
91
92                 if (total_len + (size_t)i + 1 > max_len) {
93                         fprintf(stderr, err_max_size, max_len);
94                         return -1;
95                 }
96
97                 memcpy(buf + total_len, arg, arg_len);
98                 total_len += arg_len;
99
100                 buf[total_len] = '\0';
101                 total_len++;
102         }
103
104         return total_len;
105
106 }
107
108 static ssize_t parse_event_file(FILE *filp, char *buf, size_t max_len)
109 {
110         int len;
111
112         len = fread(buf, 1, max_len, filp);
113
114         if (!feof(filp)) {
115                 fprintf(stderr, err_max_size, max_len);
116                 return -1;
117         }
118
119         if (!len)
120                 return -1;
121
122         return len;
123 }
124
125 static int send_event(char *buf, ssize_t len)
126 {
127         struct sockaddr_un addr;
128         int sd, i;
129
130         sd = socket(PF_UNIX, SOCK_DGRAM, 0);
131         if (sd < 0)
132                 err(EXIT_FAILURE, "socket");
133
134         memset(&addr, 0, sizeof(addr));
135         addr.sun_family = AF_UNIX;
136         strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
137
138         for (i = 10; i; i--) {
139                 ssize_t sent = sendto(sd, buf, len, 0,
140                                 (struct sockaddr *)&addr, SUN_LEN(&addr));
141
142                 if (sent == len)
143                         break;
144
145                 DBG("pb-event: waiting for server %d\n", i);
146                 sleep(1);
147         }
148
149         close(sd);
150
151         if (!i)
152                 err(EXIT_FAILURE, "send");
153
154         DBG("pb-event: wrote %zu bytes\n", len);
155
156         return 0;
157 }
158
159 int main(int argc, char *argv[])
160 {
161         char buf[PBOOT_USER_EVENT_SIZE];
162         ssize_t len;
163
164         if (argc >= 2 && !strcmp(argv[1], "-h")) {
165                 print_usage();
166                 return EXIT_SUCCESS;
167         }
168
169         if (argc > 1) {
170                 len = parse_event_args(argc - 1, argv + 1,
171                                         buf, sizeof(buf));
172         } else {
173                 len = parse_event_file(stdin, buf, sizeof(buf));
174         }
175
176         if (len < 0)
177                 return EXIT_FAILURE;
178
179         send_event(buf, len);
180
181         return EXIT_SUCCESS;
182 }