1 /* guestfish - the filesystem interactive shell
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
27 #include <sys/types.h>
31 #include <rpc/types.h>
35 #include "rc_protocol.h"
38 create_sockpath (pid_t pid, char *sockpath, int len, struct sockaddr_un *addr)
41 uid_t euid = geteuid ();
43 snprintf (dir, sizeof dir, "/tmp/.guestfish-%d", euid);
46 snprintf (sockpath, len, "/tmp/.guestfish-%d/socket-%d", euid, pid);
48 addr->sun_family = AF_UNIX;
49 strcpy (addr->sun_path, sockpath);
52 /* Remote control server. */
58 struct sockaddr_un addr;
62 guestfish_hello hello;
64 guestfish_reply reply;
68 memset (&hello, 0, sizeof hello);
69 memset (&call, 0, sizeof call);
79 printf ("export GUESTFISH_PID=%d\n", pid);
86 * Create the listening socket for accepting commands.
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).
94 create_sockpath (pid, sockpath, sizeof sockpath, &addr);
96 sock = socket (AF_UNIX, SOCK_STREAM, 0);
102 if (bind (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
106 if (listen (sock, 4) == -1) {
111 /* Now close stdout and substitute /dev/null. This is necessary
112 * so that eval `guestfish --listen` doesn't block forever.
114 fd = open ("/dev/null", O_WRONLY);
116 perror ("/dev/null");
122 /* Read commands and execute them. */
124 s = accept (sock, NULL, NULL);
128 fp = fdopen (s, "r+");
129 xdrstdio_create (&xdr, fp, XDR_DECODE);
131 if (!xdr_guestfish_hello (&xdr, &hello)) {
132 fprintf (stderr, _("guestfish: protocol error: could not read 'hello' message\n"));
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"),
140 xdr_free ((xdrproc_t) xdr_guestfish_hello, (char *) &hello);
143 xdr_free ((xdrproc_t) xdr_guestfish_hello, (char *) &hello);
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 *));
153 call.args.args_val = argv;
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");
163 /* Run the command. */
164 reply.r = issue_command (call.cmd, argv, NULL);
166 xdr_free ((xdrproc_t) xdr_guestfish_call, (char *) &call);
168 /* Send the reply. */
169 xdrstdio_create (&xdr2, fp, XDR_ENCODE);
170 (void) xdr_guestfish_reply (&xdr2, &reply);
174 if (call.exit_on_error && reply.r == -1) {
181 xdr_destroy (&xdr); /* NB. This doesn't close 'fp'. */
182 fclose (fp); /* Closes the underlying socket 's'. */
190 /* Remote control client. */
192 rc_remote (int pid, const char *cmd, int argc, char *argv[],
195 guestfish_hello hello;
197 guestfish_reply reply;
199 struct sockaddr_un addr;
204 memset (&reply, 0, sizeof reply);
206 /* This is fine as long as we never try to xdr_free this struct. */
207 hello.vers = (char *) PACKAGE_VERSION;
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"));
215 create_sockpath (pid, sockpath, sizeof sockpath, &addr);
217 sock = socket (AF_UNIX, SOCK_STREAM, 0);
223 if (connect (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
225 fprintf (stderr, _("guestfish: remote: looks like the server is not running\n"));
230 /* Send the greeting. */
231 fp = fdopen (sock, "r+");
232 xdrstdio_create (&xdr, fp, XDR_ENCODE);
234 if (!xdr_guestfish_hello (&xdr, &hello)) {
235 fprintf (stderr, _("guestfish: protocol error: could not send initial greeting to server\n"));
241 /* Send the command. The server supports reading multiple commands
242 * per connection, but this code only ever sends one command.
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"));
256 /* Wait for the reply. */
257 xdrstdio_create (&xdr, fp, XDR_DECODE);
259 if (!xdr_guestfish_reply (&xdr, &reply)) {
260 fprintf (stderr, _("guestfish: protocol error: could not decode reply from server\n"));