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