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"
37 #define UNIX_PATH_MAX 108
40 create_sockpath (pid_t pid, char *sockpath, int len, struct sockaddr_un *addr)
43 uid_t euid = geteuid ();
45 snprintf (dir, sizeof dir, "/tmp/.guestfish-%d", euid);
48 snprintf (sockpath, len, "/tmp/.guestfish-%d/socket-%d", euid, pid);
50 addr->sun_family = AF_UNIX;
51 strcpy (addr->sun_path, sockpath);
54 /* Remote control server. */
60 struct sockaddr_un addr;
64 guestfish_hello hello;
66 guestfish_reply reply;
70 memset (&hello, 0, sizeof hello);
71 memset (&call, 0, sizeof call);
81 printf ("export GUESTFISH_PID=%d\n", pid);
88 * Create the listening socket for accepting commands.
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).
96 create_sockpath (pid, sockpath, sizeof sockpath, &addr);
98 sock = socket (AF_UNIX, SOCK_STREAM, 0);
104 if (bind (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
108 if (listen (sock, 4) == -1) {
113 /* Now close stdout and substitute /dev/null. This is necessary
114 * so that eval `guestfish --listen` doesn't block forever.
116 fd = open ("/dev/null", O_WRONLY);
118 perror ("/dev/null");
124 /* Read commands and execute them. */
126 s = accept (sock, NULL, NULL);
130 fp = fdopen (s, "r+");
131 xdrstdio_create (&xdr, fp, XDR_DECODE);
133 if (!xdr_guestfish_hello (&xdr, &hello)) {
134 fprintf (stderr, _("guestfish: protocol error: could not read 'hello' message\n"));
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"),
142 xdr_free ((xdrproc_t) xdr_guestfish_hello, (char *) &hello);
145 xdr_free ((xdrproc_t) xdr_guestfish_hello, (char *) &hello);
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 *));
155 call.args.args_val = argv;
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");
165 /* Run the command. */
166 reply.r = issue_command (call.cmd, argv, NULL);
168 xdr_free ((xdrproc_t) xdr_guestfish_call, (char *) &call);
170 /* Send the reply. */
171 xdrstdio_create (&xdr2, fp, XDR_ENCODE);
172 (void) xdr_guestfish_reply (&xdr2, &reply);
176 if (call.exit_on_error && reply.r == -1) {
183 xdr_destroy (&xdr); /* NB. This doesn't close 'fp'. */
184 fclose (fp); /* Closes the underlying socket 's'. */
192 /* Remote control client. */
194 rc_remote (int pid, const char *cmd, int argc, char *argv[],
197 guestfish_hello hello;
199 guestfish_reply reply;
201 struct sockaddr_un addr;
206 memset (&reply, 0, sizeof reply);
208 /* This is fine as long as we never try to xdr_free this struct. */
209 hello.vers = (char *) PACKAGE_VERSION;
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"));
217 create_sockpath (pid, sockpath, sizeof sockpath, &addr);
219 sock = socket (AF_UNIX, SOCK_STREAM, 0);
225 if (connect (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
227 fprintf (stderr, _("guestfish: remote: looks like the server is not running\n"));
232 /* Send the greeting. */
233 fp = fdopen (sock, "r+");
234 xdrstdio_create (&xdr, fp, XDR_ENCODE);
236 if (!xdr_guestfish_hello (&xdr, &hello)) {
237 fprintf (stderr, _("guestfish: protocol error: could not send initial greeting to server\n"));
243 /* Send the command. The server supports reading multiple commands
244 * per connection, but this code only ever sends one command.
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"));
258 /* Wait for the reply. */
259 xdrstdio_create (&xdr, fp, XDR_DECODE);
261 if (!xdr_guestfish_reply (&xdr, &reply)) {
262 fprintf (stderr, _("guestfish: protocol error: could not decode reply from server\n"));