[multipathd] unix socket daemon control interface
[multipath-tools/.git] / multipathd / uxclnt.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdarg.h>
5 #include <fcntl.h>
6 #include <sys/ioctl.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <sys/un.h>
10 #include <sys/poll.h>
11
12 #include <uxsock.h>
13
14 /*
15  * process the client 
16  */
17 static void process(int fd)
18 {
19         char line[1000];
20         char *reply;
21
22         while (fgets(line, sizeof(line), stdin)) {
23                 size_t len = strlen(line);
24
25                 if (line[len-1] == '\n') {
26                         line[len-1] = 0;
27                         len--;
28                 }
29                 
30                 if (send_packet(fd, line, strlen(line)) != 0) break;
31                 if (recv_packet(fd, &reply, &len) != 0) break;
32
33                 printf("%*.*s\n", (int)len, (int)len, reply);
34                 free(reply);
35         }
36 }
37
38 static void process_req(int fd, char * inbuf)
39 {
40         char *reply;
41         size_t len;
42
43         send_packet(fd, inbuf, strlen(inbuf));
44         recv_packet(fd, &reply, &len);
45
46         printf("%*.*s\n", (int)len, (int)len, reply);
47         free(reply);
48 }
49         
50 /*
51  * entry point
52  */
53 int uxclnt(char * inbuf)
54 {
55         int fd;
56
57         fd = ux_socket_connect(SOCKET_NAME);
58         if (fd == -1) {
59                 perror("ux_socket_connect");
60                 exit(1);
61         }
62
63         if (inbuf)
64                 process_req(fd, inbuf);
65         else
66                 process(fd);
67         
68         return 0;
69 }