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 int fd = *(int *)CMSG_DATA (h);
103 /* Duplicate the received file descriptor to stdout */
104 dup2 (fd, STDOUT_FILENO);
112 static struct cmsghdr *cmptr = NULL;
116 /* Our 1 byte dummy buffer */
119 /* Don't specify a destination */
123 /* Initialise the msghdr to send zero byte */
124 iov[0].iov_base = buf;
129 /* Initialize the zero byte */
132 /* Initialize the control data */
134 cmptr = malloc (controllen);
140 cmptr->cmsg_level = SOL_SOCKET;
141 cmptr->cmsg_type = SCM_RIGHTS;
142 cmptr->cmsg_len = controllen;
144 /* Add control header to the message */
145 msg.msg_control = cmptr;
146 msg.msg_controllen = controllen;
148 /* Add STDOUT to the control data */
149 *(int *)CMSG_DATA (cmptr) = STDOUT_FILENO;
151 if (sendmsg (s, &msg, 0) != 1) {
152 perror ("sendmsg stdout fd");
162 fd = open ("/dev/null", O_WRONLY);
164 perror ("/dev/null");
166 dup2 (fd, STDOUT_FILENO);
171 /* Remote control server. */
177 struct sockaddr_un addr;
181 guestfish_hello hello;
183 guestfish_reply reply;
187 memset (&hello, 0, sizeof hello);
188 memset (&call, 0, sizeof call);
197 /* Parent process. */
198 printf ("export GUESTFISH_PID=%d\n", pid);
205 * Create the listening socket for accepting commands.
207 * Unfortunately there is a small but unavoidable race here. We
208 * don't know the PID until after we've forked, so we cannot be
209 * sure the socket is created from the point of view of the parent
210 * (if the child is very slow).
213 create_sockpath (pid, sockpath, sizeof sockpath, &addr);
215 sock = socket (AF_UNIX, SOCK_STREAM, 0);
221 if (bind (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
225 if (listen (sock, 4) == -1) {
230 /* Now close stdout and substitute /dev/null. This is necessary
231 * so that eval `guestfish --listen` doesn't block forever.
235 /* Read commands and execute them. */
237 s = accept (sock, NULL, NULL);
243 fp = fdopen (s, "r+");
244 xdrstdio_create (&xdr, fp, XDR_DECODE);
246 if (!xdr_guestfish_hello (&xdr, &hello)) {
247 fprintf (stderr, _("guestfish: protocol error: could not read 'hello' message\n"));
251 if (strcmp (hello.vers, PACKAGE_VERSION) != 0) {
252 fprintf (stderr, _("guestfish: protocol error: version mismatch, server version '%s' does not match client version '%s'. The two versions must match exactly.\n"),
255 xdr_free ((xdrproc_t) xdr_guestfish_hello, (char *) &hello);
258 xdr_free ((xdrproc_t) xdr_guestfish_hello, (char *) &hello);
260 while (xdr_guestfish_call (&xdr, &call)) {
261 /* We have to extend and NULL-terminate the argv array. */
262 argc = call.args.args_len;
263 argv = realloc (call.args.args_val, (argc+1) * sizeof (char *));
268 call.args.args_val = argv;
272 fprintf (stderr, "guestfish(%d): %s", pid, call.cmd);
273 for (i = 0; i < argc; ++i)
274 fprintf (stderr, " %s", argv[i]);
275 fprintf (stderr, "\n");
278 /* Run the command. */
279 reply.r = issue_command (call.cmd, argv, NULL);
281 xdr_free ((xdrproc_t) xdr_guestfish_call, (char *) &call);
283 /* Send the reply. */
284 xdrstdio_create (&xdr2, fp, XDR_ENCODE);
285 (void) xdr_guestfish_reply (&xdr2, &reply);
289 if (call.exit_on_error && reply.r == -1) {
296 xdr_destroy (&xdr); /* NB. This doesn't close 'fp'. */
297 fclose (fp); /* Closes the underlying socket 's'. */
298 close_stdout(); /* Re-close stdout */
306 /* Remote control client. */
308 rc_remote (int pid, const char *cmd, int argc, char *argv[],
311 guestfish_hello hello;
313 guestfish_reply reply;
315 struct sockaddr_un addr;
320 memset (&reply, 0, sizeof reply);
322 /* This is fine as long as we never try to xdr_free this struct. */
323 hello.vers = (char *) PACKAGE_VERSION;
325 /* Check the other end is still running. */
326 if (kill (pid, 0) == -1) {
327 fprintf (stderr, _("guestfish: remote: looks like the server is not running\n"));
331 create_sockpath (pid, sockpath, sizeof sockpath, &addr);
333 sock = socket (AF_UNIX, SOCK_STREAM, 0);
339 if (connect (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
341 fprintf (stderr, _("guestfish: remote: looks like the server is not running\n"));
348 /* Send the greeting. */
349 fp = fdopen (sock, "r+");
350 xdrstdio_create (&xdr, fp, XDR_ENCODE);
352 if (!xdr_guestfish_hello (&xdr, &hello)) {
353 fprintf (stderr, _("guestfish: protocol error: could not send initial greeting to server\n"));
359 /* Send the command. The server supports reading multiple commands
360 * per connection, but this code only ever sends one command.
362 call.cmd = (char *) cmd;
363 call.args.args_len = argc;
364 call.args.args_val = argv;
365 call.exit_on_error = exit_on_error;
366 if (!xdr_guestfish_call (&xdr, &call)) {
367 fprintf (stderr, _("guestfish: protocol error: could not send initial greeting to server\n"));
374 /* Wait for the reply. */
375 xdrstdio_create (&xdr, fp, XDR_DECODE);
377 if (!xdr_guestfish_reply (&xdr, &reply)) {
378 fprintf (stderr, _("guestfish: protocol error: could not decode reply from server\n"));