On Mon, Feb 22, 2010 at 12:51 PM, Luiz Capitulino
<lcapitul...@redhat.com> wrote:
> How do you reproduce it?

Here's a test program that reproduces the problem. Start qemu with

  -chardev socket,id=monitor,path=/tmp/qemu-monitor,server,nowait -mon
chardev=monitor,mode=readline

and run check_getfd /tmp/qemu-monitor. It will print an error and
return nonzero if the monitor output indicates getfd or closefd
failed.

--Ed
/*
 * check_getfd
 *
 * Tests the qemu getfd monitor command
 *
 * Copyright (c) 2010 Arista Networks, Inc.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

int receive_output(int s, char *m) {
   unsigned int i = 0;
   char buf[10240];

   buf[0] = '\0';
   while (1) {
      if (recv(s, &buf[i], 1, 0) < 0) {
         perror("Failed to receive");
         return -1;
      }
      buf[++i] = '\0';
      if ((i > 7) && !strcmp(&buf[i-7], "(qemu) "))
         break;
   }

   if (m &&
       ((i < strlen(m) + 7) || strncmp(&buf[i-7-strlen(m)], m, strlen(m)))) {
      fprintf(stderr, "%s\n", buf);
      return -1;
   }

   return 0;
}

int main(int argc, char *argv[]) {
   struct sockaddr_un addr;
   int s;
   int fd;
   char fdbuf[CMSG_SPACE(sizeof(fd))];
   struct msghdr msg;
   struct cmsghdr *cmsg;
   struct iovec mvec;
   char *cmd = "getfd MYFD\nclosefd MYFD\n";

   if (argc != 2) {
      printf("Usage: %s QEMU_MONITOR\n\n", argv[0]);
      printf("  (start qemu with -chardev socket,id=monitor,path=QEMU_MONITOR"
             ",server,nowait -mon chardev=monitor,mode=readline)\n");
      return 1;
   }

   fd = open("/dev/null", O_RDWR);
   if (fd < 0) {
      perror("Failed to open /dev/null");
      return 1;
   }

   memset(&addr, 0, sizeof(addr));
   addr.sun_family = AF_UNIX;
   strncpy(addr.sun_path, argv[1], sizeof(addr.sun_path));

   s = socket(PF_UNIX, SOCK_STREAM, 0);
   if (s < 0) {
      perror("No socket");
      return 1;
   }

   if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
      perror("Failed to connect");
      return 1;
   }

   if (receive_output(s, NULL) < 0)
      return 1;

   mvec.iov_base = cmd;
   mvec.iov_len = strlen(cmd) + 1;
   msg.msg_name = NULL;
   msg.msg_namelen = 0;
   msg.msg_iov = &mvec;
   msg.msg_iovlen = 1;
   msg.msg_control = fdbuf;
   msg.msg_controllen = CMSG_LEN(sizeof(fd));
   msg.msg_flags = 0;

   cmsg = CMSG_FIRSTHDR(&msg);
   cmsg->cmsg_level = SOL_SOCKET;
   cmsg->cmsg_type = SCM_RIGHTS;
   cmsg->cmsg_len = msg.msg_controllen;
   memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
   
   if (sendmsg(s, &msg, 0) < 0) {
      perror("Failed to send");
      return 1;
   }

   if (receive_output(s, "\033[K\r\n") < 0)
      return 1;
   if (receive_output(s, "\033[K\r\n") < 0)
      return 1;

   return 0;
}

Reply via email to