]> git.ozlabs.org Git - petitboot/blob - utils/pb-event.c
Fix minor typo in discover-client file descriptor check
[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 <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/socket.h>
31 #include <sys/types.h>
32 #include <sys/un.h>
33
34 #include "discover/user-event.h"
35
36 #if defined(DEBUG)
37 #define DBG(_args...) do {fprintf(stderr, _args); fflush(stderr); } while (0)
38 #else
39 static inline int __attribute__ ((format (printf, 1, 2))) DBG(
40         __attribute__((unused)) const char *fmt, ...) {return 0; }
41 #endif
42
43 int main(void)
44 {
45         int result;
46         struct sockaddr_un addr;
47         char buf[PBOOT_USER_EVENT_SIZE];
48         ssize_t len;
49         int s;
50         int i;
51
52         s = socket(PF_UNIX, SOCK_DGRAM, 0);
53
54         if (s < 0) {
55                 fprintf(stderr, "pb-event: socket: %s\n", strerror(errno));
56                 return EXIT_FAILURE;
57         }
58
59         result = EXIT_SUCCESS;
60
61         len = fread(buf, 1, sizeof(buf), stdin);
62
63         if (!feof(stdin)) {
64                 fprintf(stderr, "pb-event: msg too big (%u byte max)\n",
65                         sizeof(buf));
66                 result = EXIT_FAILURE;
67                 /* continue on and try to write msg */
68         }
69
70         if (!len)
71                 return result;
72
73         memset(&addr, 0, sizeof(addr));
74         addr.sun_family = AF_UNIX;
75         strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
76
77         for (i = 10; i; i--) {
78                 ssize_t sent = sendto(s, buf, len, 0, (struct sockaddr *)&addr,
79                         SUN_LEN(&addr));
80
81                 if (sent == len)
82                         break;
83
84                 DBG("pb-event: waiting for server %d\n", i);
85                 sleep(1);
86         }
87
88         if (!i) {
89                 fprintf(stderr, "pb-event: send: %s\n", strerror(errno));
90                 return EXIT_FAILURE;
91         }
92
93         DBG("pb-event: wrote %u bytes\n", len);
94         return result;
95 }