Fix non-srcdir builds: further fixes to OCaml build rules.
[libguestfs.git] / fish / rc.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/un.h>
29 #include <signal.h>
30
31 #include <rpc/types.h>
32 #include <rpc/xdr.h>
33
34 #include "fish.h"
35 #include "rc_protocol.h"
36
37 #define UNIX_PATH_MAX 108
38
39 static void
40 create_sockpath (pid_t pid, char *sockpath, int len, struct sockaddr_un *addr)
41 {
42   char dir[128];
43   uid_t euid = geteuid ();
44
45   snprintf (dir, sizeof dir, "/tmp/.guestfish-%d", euid);
46   mkdir (dir, 0700);
47
48   snprintf (sockpath, len, "/tmp/.guestfish-%d/socket-%d", euid, pid);
49
50   addr->sun_family = AF_UNIX;
51   strcpy (addr->sun_path, sockpath);
52 }
53
54 /* Remote control server. */
55 void
56 rc_listen (void)
57 {
58   char sockpath[128];
59   pid_t pid;
60   struct sockaddr_un addr;
61   int sock, s, i, fd;
62   FILE *fp;
63   XDR xdr, xdr2;
64   guestfish_hello hello;
65   guestfish_call call;
66   guestfish_reply reply;
67   char **argv;
68   int argc;
69
70   memset (&hello, 0, sizeof hello);
71   memset (&call, 0, sizeof call);
72
73   pid = fork ();
74   if (pid == -1) {
75     perror ("fork");
76     exit (1);
77   }
78
79   if (pid > 0) {
80     /* Parent process. */
81     printf ("export GUESTFISH_PID=%d\n", pid);
82     fflush (stdout);
83     _exit (0);
84   }
85
86   /* Child process.
87    *
88    * Create the listening socket for accepting commands.
89    *
90    * Unfortunately there is a small but unavoidable race here.  We
91    * don't know the PID until after we've forked, so we cannot be
92    * sure the socket is created from the point of view of the parent
93    * (if the child is very slow).
94    */
95   pid = getpid ();
96   create_sockpath (pid, sockpath, sizeof sockpath, &addr);
97
98   sock = socket (AF_UNIX, SOCK_STREAM, 0);
99   if (sock == -1) {
100     perror ("socket");
101     exit (1);
102   }
103   unlink (sockpath);
104   if (bind (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
105     perror (sockpath);
106     exit (1);
107   }
108   if (listen (sock, 4) == -1) {
109     perror ("listen");
110     exit (1);
111   }
112
113   /* Now close stdout and substitute /dev/null.  This is necessary
114    * so that eval `guestfish --listen` doesn't block forever.
115    */
116   fd = open ("/dev/null", O_WRONLY);
117   if (fd == -1)
118     perror ("/dev/null");
119   else {
120     dup2 (fd, 1);
121     close (fd);
122   }
123
124   /* Read commands and execute them. */
125   while (!quit) {
126     s = accept (sock, NULL, NULL);
127     if (s == -1)
128       perror ("accept");
129     else {
130       fp = fdopen (s, "r+");
131       xdrstdio_create (&xdr, fp, XDR_DECODE);
132
133       if (!xdr_guestfish_hello (&xdr, &hello)) {
134         fprintf (stderr, _("guestfish: protocol error: could not read 'hello' message\n"));
135         goto error;
136       }
137
138       if (strcmp (hello.vers, PACKAGE_VERSION) != 0) {
139         fprintf (stderr, _("guestfish: protocol error: version mismatch, server version '%s' does not match client version '%s'.  The two versions must match exactly.\n"),
140                  PACKAGE_VERSION,
141                  hello.vers);
142         xdr_free ((xdrproc_t) xdr_guestfish_hello, (char *) &hello);
143         goto error;
144       }
145       xdr_free ((xdrproc_t) xdr_guestfish_hello, (char *) &hello);
146
147       while (xdr_guestfish_call (&xdr, &call)) {
148         /* We have to extend and NULL-terminate the argv array. */
149         argc = call.args.args_len;
150         argv = realloc (call.args.args_val, (argc+1) * sizeof (char *));
151         if (argv == NULL) {
152           perror ("realloc");
153           exit (1);
154         }
155         call.args.args_val = argv;
156         argv[argc] = NULL;
157
158         if (verbose) {
159           fprintf (stderr, "guestfish(%d): %s", pid, call.cmd);
160           for (i = 0; i < argc; ++i)
161             fprintf (stderr, " %s", argv[i]);
162           fprintf (stderr, "\n");
163         }
164
165         /* Run the command. */
166         reply.r = issue_command (call.cmd, argv, NULL);
167
168         xdr_free ((xdrproc_t) xdr_guestfish_call, (char *) &call);
169
170         /* Send the reply. */
171         xdrstdio_create (&xdr2, fp, XDR_ENCODE);
172         (void) xdr_guestfish_reply (&xdr2, &reply);
173         xdr_destroy (&xdr2);
174
175         /* Exit on error? */
176         if (call.exit_on_error && reply.r == -1) {
177           unlink (sockpath);
178           exit (1);
179         }
180       }
181
182     error:
183       xdr_destroy (&xdr);       /* NB. This doesn't close 'fp'. */
184       fclose (fp);              /* Closes the underlying socket 's'. */
185     }
186   }
187
188   unlink (sockpath);
189   exit (0);
190 }
191
192 /* Remote control client. */
193 int
194 rc_remote (int pid, const char *cmd, int argc, char *argv[],
195            int exit_on_error)
196 {
197   guestfish_hello hello;
198   guestfish_call call;
199   guestfish_reply reply;
200   char sockpath[128];
201   struct sockaddr_un addr;
202   int sock;
203   FILE *fp;
204   XDR xdr;
205
206   memset (&reply, 0, sizeof reply);
207
208   /* This is fine as long as we never try to xdr_free this struct. */
209   hello.vers = (char *) PACKAGE_VERSION;
210
211   /* Check the other end is still running. */
212   if (kill (pid, 0) == -1) {
213     fprintf (stderr, _("guestfish: remote: looks like the server is not running\n"));
214     return -1;
215   }
216
217   create_sockpath (pid, sockpath, sizeof sockpath, &addr);
218
219   sock = socket (AF_UNIX, SOCK_STREAM, 0);
220   if (sock == -1) {
221     perror ("socket");
222     return -1;
223   }
224
225   if (connect (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
226     perror (sockpath);
227     fprintf (stderr, _("guestfish: remote: looks like the server is not running\n"));
228     close (sock);
229     return -1;
230   }
231
232   /* Send the greeting. */
233   fp = fdopen (sock, "r+");
234   xdrstdio_create (&xdr, fp, XDR_ENCODE);
235
236   if (!xdr_guestfish_hello (&xdr, &hello)) {
237     fprintf (stderr, _("guestfish: protocol error: could not send initial greeting to server\n"));
238     fclose (fp);
239     xdr_destroy (&xdr);
240     return -1;
241   }
242
243   /* Send the command.  The server supports reading multiple commands
244    * per connection, but this code only ever sends one command.
245    */
246   call.cmd = (char *) cmd;
247   call.args.args_len = argc;
248   call.args.args_val = argv;
249   call.exit_on_error = exit_on_error;
250   if (!xdr_guestfish_call (&xdr, &call)) {
251     fprintf (stderr, _("guestfish: protocol error: could not send initial greeting to server\n"));
252     fclose (fp);
253     xdr_destroy (&xdr);
254     return -1;
255   }
256   xdr_destroy (&xdr);
257
258   /* Wait for the reply. */
259   xdrstdio_create (&xdr, fp, XDR_DECODE);
260
261   if (!xdr_guestfish_reply (&xdr, &reply)) {
262     fprintf (stderr, _("guestfish: protocol error: could not decode reply from server\n"));
263     fclose (fp);
264     xdr_destroy (&xdr);
265     return -1;
266   }
267
268   fclose (fp);
269   xdr_destroy (&xdr);
270
271   return reply.r;
272 }