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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 */
74 memset (&msg, 0, sizeof msg);
78 /* Initialise the msghdr to receive zero byte */
79 iov[0].iov_base = buf;
84 /* Initialise the control data */
85 msg.msg_control = cmptr;
86 msg.msg_controllen = controllen;
88 /* Read a message from the socket */
89 ssize_t n = recvmsg (s, &msg, 0);
91 perror ("recvmsg stdout fd");
95 h = CMSG_FIRSTHDR(&msg);
97 fprintf (stderr, "didn't receive a stdout file descriptor\n");
101 /* Extract the transferred file descriptor from the control data */
102 unsigned char *data = CMSG_DATA (h);
103 int fd = *(int *)data;
105 /* Duplicate the received file descriptor to stdout */
106 dup2 (fd, STDOUT_FILENO);
114 static struct cmsghdr *cmptr = NULL;
118 /* Our 1 byte dummy buffer */
121 /* Don't specify a destination */
122 memset (&msg, 0, sizeof msg);
126 /* Initialise the msghdr to send zero byte */
127 iov[0].iov_base = buf;
132 /* Initialize the zero byte */
135 /* Initialize the control data */
137 cmptr = malloc (controllen);
143 cmptr->cmsg_level = SOL_SOCKET;
144 cmptr->cmsg_type = SCM_RIGHTS;
145 cmptr->cmsg_len = controllen;
147 /* Add control header to the message */
148 msg.msg_control = cmptr;
149 msg.msg_controllen = controllen;
151 /* Add STDOUT to the control data */
152 unsigned char *data = CMSG_DATA (cmptr);
153 *(int *)data = STDOUT_FILENO;
155 if (sendmsg (s, &msg, 0) != 1) {
156 perror ("sendmsg stdout fd");
166 fd = open ("/dev/null", O_WRONLY);
168 perror ("/dev/null");
170 dup2 (fd, STDOUT_FILENO);
175 /* Remote control server. */
181 struct sockaddr_un addr;
186 guestfish_hello hello;
188 guestfish_reply reply;
192 memset (&hello, 0, sizeof hello);
193 memset (&call, 0, sizeof call);
202 /* Parent process. */
204 if (!remote_control_csh)
205 printf ("GUESTFISH_PID=%d; export GUESTFISH_PID\n", pid);
207 printf ("setenv GUESTFISH_PID %d\n", pid);
215 * Create the listening socket for accepting commands.
217 * Unfortunately there is a small but unavoidable race here. We
218 * don't know the PID until after we've forked, so we cannot be
219 * sure the socket is created from the point of view of the parent
220 * (if the child is very slow).
223 create_sockpath (pid, sockpath, sizeof sockpath, &addr);
225 sock = socket (AF_UNIX, SOCK_STREAM, 0);
231 if (bind (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
235 if (listen (sock, 4) == -1) {
240 /* Now close stdout and substitute /dev/null. This is necessary
241 * so that eval `guestfish --listen` doesn't block forever.
245 /* Read commands and execute them. */
247 s = accept (sock, NULL, NULL);
253 fp = fdopen (s, "r+");
254 xdrstdio_create (&xdr, fp, XDR_DECODE);
256 if (!xdr_guestfish_hello (&xdr, &hello)) {
257 fprintf (stderr, _("guestfish: protocol error: could not read 'hello' message\n"));
261 if (STRNEQ (hello.vers, PACKAGE_VERSION)) {
262 fprintf (stderr, _("guestfish: protocol error: version mismatch, server version '%s' does not match client version '%s'. The two versions must match exactly.\n"),
265 xdr_free ((xdrproc_t) xdr_guestfish_hello, (char *) &hello);
268 xdr_free ((xdrproc_t) xdr_guestfish_hello, (char *) &hello);
270 while (xdr_guestfish_call (&xdr, &call)) {
271 /* We have to extend and NULL-terminate the argv array. */
272 argc = call.args.args_len;
273 argv = realloc (call.args.args_val, (argc+1) * sizeof (char *));
278 call.args.args_val = argv;
282 fprintf (stderr, "guestfish(%d): %s", pid, call.cmd);
283 for (i = 0; i < argc; ++i)
284 fprintf (stderr, " %s", argv[i]);
285 fprintf (stderr, "\n");
288 /* Run the command. */
289 reply.r = issue_command (call.cmd, argv, NULL, 0);
291 xdr_free ((xdrproc_t) xdr_guestfish_call, (char *) &call);
293 /* Send the reply. */
294 xdrstdio_create (&xdr2, fp, XDR_ENCODE);
295 (void) xdr_guestfish_reply (&xdr2, &reply);
299 if (call.exit_on_error && reply.r == -1) {
306 xdr_destroy (&xdr); /* NB. This doesn't close 'fp'. */
307 fclose (fp); /* Closes the underlying socket 's'. */
308 close_stdout(); /* Re-close stdout */
316 /* Remote control client. */
318 rc_remote (int pid, const char *cmd, size_t argc, char *argv[],
321 guestfish_hello hello;
323 guestfish_reply reply;
325 struct sockaddr_un addr;
330 memset (&reply, 0, sizeof reply);
332 /* This is fine as long as we never try to xdr_free this struct. */
333 hello.vers = (char *) PACKAGE_VERSION;
335 /* Check the other end is still running. */
336 if (kill (pid, 0) == -1) {
337 fprintf (stderr, _("guestfish: remote: looks like the server is not running\n"));
341 create_sockpath (pid, sockpath, sizeof sockpath, &addr);
343 sock = socket (AF_UNIX, SOCK_STREAM, 0);
349 if (connect (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
351 fprintf (stderr, _("guestfish: remote: looks like the server is not running\n"));
358 /* Send the greeting. */
359 fp = fdopen (sock, "r+");
360 xdrstdio_create (&xdr, fp, XDR_ENCODE);
362 if (!xdr_guestfish_hello (&xdr, &hello)) {
363 fprintf (stderr, _("guestfish: protocol error: could not send initial greeting to server\n"));
369 /* Send the command. The server supports reading multiple commands
370 * per connection, but this code only ever sends one command.
372 call.cmd = (char *) cmd;
373 call.args.args_len = argc;
374 call.args.args_val = argv;
375 call.exit_on_error = exit_on_error;
376 if (!xdr_guestfish_call (&xdr, &call)) {
377 fprintf (stderr, _("guestfish: protocol error: could not send initial greeting to server\n"));
384 /* Wait for the reply. */
385 xdrstdio_create (&xdr, fp, XDR_DECODE);
387 if (!xdr_guestfish_reply (&xdr, &reply)) {
388 fprintf (stderr, _("guestfish: protocol error: could not decode reply from server\n"));