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>
30 #include <sys/socket.h>
32 #include <rpc/types.h>
36 #include "rc_protocol.h"
39 create_sockpath (pid_t pid, char *sockpath, int len, struct sockaddr_un *addr)
42 uid_t euid = geteuid ();
44 snprintf (dir, sizeof dir, "/tmp/.guestfish-%d", euid);
47 snprintf (sockpath, len, "/tmp/.guestfish-%d/socket-%d", euid, pid);
49 addr->sun_family = AF_UNIX;
50 strcpy (addr->sun_path, sockpath);
53 static const socklen_t controllen = CMSG_LEN (sizeof (int));
56 receive_stdout (int s)
58 static struct cmsghdr *cmptr = NULL, *h;
62 /* Our 1 byte buffer */
66 cmptr = malloc (controllen);
73 /* Don't specify a source */
77 /* Initialise the msghdr to receive zero byte */
78 iov[0].iov_base = buf;
83 /* Initialise the control data */
84 msg.msg_control = cmptr;
85 msg.msg_controllen = controllen;
87 /* Read a message from the socket */
88 ssize_t n = recvmsg (s, &msg, 0);
90 perror ("recvmsg stdout fd");
94 h = CMSG_FIRSTHDR(&msg);
96 fprintf (stderr, "didn't receive a stdout file descriptor\n");
100 /* Extract the transferred file descriptor from the control data */
101 unsigned char *data = CMSG_DATA (h);
102 int fd = *(int *)data;
104 /* Duplicate the received file descriptor to stdout */
105 dup2 (fd, STDOUT_FILENO);
113 static struct cmsghdr *cmptr = NULL;
117 /* Our 1 byte dummy buffer */
120 /* Don't specify a destination */
124 /* Initialise the msghdr to send zero byte */
125 iov[0].iov_base = buf;
130 /* Initialize the zero byte */
133 /* Initialize the control data */
135 cmptr = malloc (controllen);
141 cmptr->cmsg_level = SOL_SOCKET;
142 cmptr->cmsg_type = SCM_RIGHTS;
143 cmptr->cmsg_len = controllen;
145 /* Add control header to the message */
146 msg.msg_control = cmptr;
147 msg.msg_controllen = controllen;
149 /* Add STDOUT to the control data */
150 unsigned char *data = CMSG_DATA (cmptr);
151 *(int *)data = STDOUT_FILENO;
153 if (sendmsg (s, &msg, 0) != 1) {
154 perror ("sendmsg stdout fd");
164 fd = open ("/dev/null", O_WRONLY);
166 perror ("/dev/null");
168 dup2 (fd, STDOUT_FILENO);
173 /* Remote control server. */
179 struct sockaddr_un addr;
183 guestfish_hello hello;
185 guestfish_reply reply;
189 memset (&hello, 0, sizeof hello);
190 memset (&call, 0, sizeof call);
199 /* Parent process. */
200 printf ("export GUESTFISH_PID=%d\n", pid);
207 * Create the listening socket for accepting commands.
209 * Unfortunately there is a small but unavoidable race here. We
210 * don't know the PID until after we've forked, so we cannot be
211 * sure the socket is created from the point of view of the parent
212 * (if the child is very slow).
215 create_sockpath (pid, sockpath, sizeof sockpath, &addr);
217 sock = socket (AF_UNIX, SOCK_STREAM, 0);
223 if (bind (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
227 if (listen (sock, 4) == -1) {
232 /* Now close stdout and substitute /dev/null. This is necessary
233 * so that eval `guestfish --listen` doesn't block forever.
237 /* Read commands and execute them. */
239 s = accept (sock, NULL, NULL);
245 fp = fdopen (s, "r+");
246 xdrstdio_create (&xdr, fp, XDR_DECODE);
248 if (!xdr_guestfish_hello (&xdr, &hello)) {
249 fprintf (stderr, _("guestfish: protocol error: could not read 'hello' message\n"));
253 if (STRNEQ (hello.vers, PACKAGE_VERSION)) {
254 fprintf (stderr, _("guestfish: protocol error: version mismatch, server version '%s' does not match client version '%s'. The two versions must match exactly.\n"),
257 xdr_free ((xdrproc_t) xdr_guestfish_hello, (char *) &hello);
260 xdr_free ((xdrproc_t) xdr_guestfish_hello, (char *) &hello);
262 while (xdr_guestfish_call (&xdr, &call)) {
263 /* We have to extend and NULL-terminate the argv array. */
264 argc = call.args.args_len;
265 argv = realloc (call.args.args_val, (argc+1) * sizeof (char *));
270 call.args.args_val = argv;
274 fprintf (stderr, "guestfish(%d): %s", pid, call.cmd);
275 for (i = 0; i < argc; ++i)
276 fprintf (stderr, " %s", argv[i]);
277 fprintf (stderr, "\n");
280 /* Run the command. */
281 reply.r = issue_command (call.cmd, argv, NULL);
283 xdr_free ((xdrproc_t) xdr_guestfish_call, (char *) &call);
285 /* Send the reply. */
286 xdrstdio_create (&xdr2, fp, XDR_ENCODE);
287 (void) xdr_guestfish_reply (&xdr2, &reply);
291 if (call.exit_on_error && reply.r == -1) {
298 xdr_destroy (&xdr); /* NB. This doesn't close 'fp'. */
299 fclose (fp); /* Closes the underlying socket 's'. */
300 close_stdout(); /* Re-close stdout */
308 /* Remote control client. */
310 rc_remote (int pid, const char *cmd, int argc, char *argv[],
313 guestfish_hello hello;
315 guestfish_reply reply;
317 struct sockaddr_un addr;
322 memset (&reply, 0, sizeof reply);
324 /* This is fine as long as we never try to xdr_free this struct. */
325 hello.vers = (char *) PACKAGE_VERSION;
327 /* Check the other end is still running. */
328 if (kill (pid, 0) == -1) {
329 fprintf (stderr, _("guestfish: remote: looks like the server is not running\n"));
333 create_sockpath (pid, sockpath, sizeof sockpath, &addr);
335 sock = socket (AF_UNIX, SOCK_STREAM, 0);
341 if (connect (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
343 fprintf (stderr, _("guestfish: remote: looks like the server is not running\n"));
350 /* Send the greeting. */
351 fp = fdopen (sock, "r+");
352 xdrstdio_create (&xdr, fp, XDR_ENCODE);
354 if (!xdr_guestfish_hello (&xdr, &hello)) {
355 fprintf (stderr, _("guestfish: protocol error: could not send initial greeting to server\n"));
361 /* Send the command. The server supports reading multiple commands
362 * per connection, but this code only ever sends one command.
364 call.cmd = (char *) cmd;
365 call.args.args_len = argc;
366 call.args.args_val = argv;
367 call.exit_on_error = exit_on_error;
368 if (!xdr_guestfish_call (&xdr, &call)) {
369 fprintf (stderr, _("guestfish: protocol error: could not send initial greeting to server\n"));
376 /* Wait for the reply. */
377 xdrstdio_create (&xdr, fp, XDR_DECODE);
379 if (!xdr_guestfish_reply (&xdr, &reply)) {
380 fprintf (stderr, _("guestfish: protocol error: could not decode reply from server\n"));