Ejemplo n.º 1
0
int main(int argc, char *argv[])

{
  speed_t baud = B57600;  // default baudrate
  stdinfd = fileno(stdin);
  stdoutfd = fileno(stdout);
  serialfd = -1;
  erlpipefd = -1;
  struct termios oldserial, oldstdin;
 
 
 //saveterminal(stdinfd,oldstdin);
 // check(tcgetattr(stdinfd,&oldstdin) == 0, "Failed to backup stdin");

 // if erlang mode is set then redirect stderr to erlerr.txt

   

  //read command line arguments
  check(parsecmdargs(argc, argv) != -1,"Command Line Argument Failure");


  if(erlang == TRUE)
     {
       freopen("erlerr.txt", "w", stderr);
     }  
 debug("pid is %d", getpid());
  //setup pipe if require doesn't work yet 
   if(pipedebug == 1) //
   {
  
     check(makepipe(ERLPIPE) == 0, "Pipe creation or opening failed for: ");
     debug("Pipe created:");
     debug("Process %d opening erlpipe", getpid());
     erlpipefd = open(ERLPIPE, O_WRONLY|O_NONBLOCK); 
     check(erlpipefd > 0, "Erlpipe failed to open pipe: %s\nSetup reader in second terminal\n tail -f erlpipe",ERLPIPE);
     debug("Erlpipe opened");
     /*
     check(makepipe(ERLPIPEIN) == 0, "Erlpipein  creation or opening failed for: "); 
     debug("Pipe created:");
     debug("Process %d opening erlpipein", getpid());
     erlpipeinfd = open(ERLPIPEIN);
     check(erlpipeinfd > 0, "Erlpipein: Failed to open pipe: %s\nSetup reader in second terminal\n tail -f erlpipe",ERLPIPEIN);
     debug("Erlpipe opened"); */
    }
    
   // save terminal settings
 
 
  //get baud rate from command line arguments
  baud = getbaud(baudrate);
  check(baud != B0, "Illegal baud rate"); 
  
  if(serial == TRUE){

  //open serial port
  serialfd = open(serialport, O_RDWR);
  check(serialfd > 0,"Can't open serial port\n Try dmesg | grep tty from shell \n");    
  debug("Opened serial port: %s",serialport);
 
  //set serial port to raw mode
  check(makeraw(serialfd)==0,"Failed to set serial port raw mode");
 //set baudrate serial
  check(setspeed(serialfd,baud)==0,"Failed to set baud rate");
  debug("Baud rate set to: %d", baudrate);
  }

  //set stdin to raw mode
  // check(makeraw(stdinfd)==0, "Failed to set stdin to raw mode");
  // debug("Not Entered raw mode:");

 
  
  //send acknowledge to erlang???

 /* Watch stdinfd and serialfd
       if input is available process then output
       Serial -> Erlang
       Erlang -> Serial
    */ 
  
  /*
  int numbytes;
  while(1){
  numbytes = read_erlang(erlmesg.mesg);
  check(numbytes > 0, "Read error from erlang");
	debug("Read %d bytes from Erlang", numbytes);
	write_serial(numbytes, erlmesg.mesg);
	debug("Wrote %d bytes to serial:", numbytes);
  }
  //main loop
  */
	
  {
    fd_set readfds;
    int maxfd;
    int is, numbytes;
    FD_ZERO(&readfds);
    
    maxfd = (stdinfd > serialfd) ? stdinfd : serialfd;
    maxfd = (maxfd > erlpipeinfd) ? maxfd : erlpipeinfd;

      
    while(1){
      if(serial){
      FD_SET(serialfd, &readfds);
      }
      
      if(pipedebug){
	  FD_SET(erlpipeinfd, &readfds);
	}

      FD_SET(stdinfd, &readfds);
      
      
      is = select(maxfd+1, &readfds, NULLFDS, NULLFDS, NULLTV);
      check(is>0,"Select error -"); 
      
      if (serial && FD_ISSET(serialfd,&readfds)){ // Serial porth ready to read
	FD_CLR(serialfd,&readfds);
	

	numbytes = read(serialfd, erlmesg.mesg, MAXMESG);
	check(numbytes > 0, "Read error from serial port");
	write_erlang(numbytes, erlmesg.mesg);

      }

      if (FD_ISSET(stdinfd,&readfds)){  //Stdin-Erlang ready to read
 	FD_CLR(stdinfd, &readfds);
	
        numbytes = read_erlang(erlmesg.mesg);
	check(numbytes > 0, "Read error from erlang");
	debug("Read %d bytes from Erlang", numbytes);
	write_serial(numbytes, erlmesg.mesg);
	debug("Wrote %d bytes to serial:", numbytes);

      }
      // input pipe
      /*
      if (pipedebug && (FD_ISSET(erlpipeinfd,&readfds)){  
	FD_CLR(erlpipeinfd, &readfds);

    }
      */
    }
  
}	  
  //hanldle input from erlang
  //handle input from serial port
  // restoreterminal(stdinfd,&oldstdin);

	//  tcsetattr(stdinfd,TCSANOW,&oldstdin);
    return 0;

 error:
    //restoreterminal(stdinfd,&oldstdin);
    //    tcsetattr(stdinfd,TCSANOW,&oldstdin);;
    debug("Restored terminal");
    exit(1);
   
    }
Ejemplo n.º 2
0
int
main (int argc, char *argv[])
{
  static const char *options = "rv?";
  static const struct option long_options[] = {
    { "help", 0, 0, '?' },
    { "verbose", 0, 0, 'v' },
    { 0, 0, 0, 0 }
  };
  int c;
  char *cmdline;

  ignore_value (chdir ("/"));

  if (winsock_init () == -1)
    error (EXIT_FAILURE, 0, "winsock initialization failed");

#ifdef HAVE_REGISTER_PRINTF_SPECIFIER
  /* http://udrepper.livejournal.com/20948.html */
  register_printf_specifier ('Q', print_shell_quote, print_arginfo);
  register_printf_specifier ('R', print_sysroot_shell_quote, print_arginfo);
#else
#ifdef HAVE_REGISTER_PRINTF_FUNCTION
  register_printf_function ('Q', print_shell_quote, print_arginfo);
  register_printf_function ('R', print_sysroot_shell_quote, print_arginfo);
#else
#error "HAVE_REGISTER_PRINTF_{SPECIFIER|FUNCTION} not defined"
#endif
#endif

  /* XXX The appliance /init script sets LD_PRELOAD=../libSegFault.so.
   * However if we CHROOT_IN to the sysroot that file might not exist,
   * resulting in all commands failing.  What we'd really like to do
   * is to have LD_PRELOAD only set while outside the chroot.  I
   * suspect the proper way to solve this is to remove the
   * CHROOT_IN/_OUT hack and replace it properly (fork), but that is
   * for another day.
   */
  unsetenv ("LD_PRELOAD");

  struct stat statbuf;
  if (stat ("/", &statbuf) == 0)
    root_device = statbuf.st_dev;

  for (;;) {
    c = getopt_long (argc, argv, options, long_options, NULL);
    if (c == -1) break;

    switch (c) {
      /* The -r flag is used when running standalone.  It changes
       * several aspects of the daemon.
       */
    case 'r':
      sysroot = "";
      sysroot_len = 0;
      autosync_umount = 0;
      break;

    case 'v':
      verbose = 1;
      break;

    case '?':
      usage ();
      exit (EXIT_SUCCESS);

    default:
      fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", c);
      exit (EXIT_FAILURE);
    }
  }

  if (optind < argc) {
    usage ();
    exit (EXIT_FAILURE);
  }

  cmdline = read_cmdline ();

  /* Set the verbose flag. */
  verbose = verbose ||
    (cmdline && strstr (cmdline, "guestfs_verbose=1") != NULL);
  if (verbose)
    printf ("verbose daemon enabled\n");

  if (verbose) {
    if (cmdline)
      printf ("linux commmand line: %s\n", cmdline);
    else
      printf ("could not read linux command line\n");
  }

  enable_network = cmdline && strstr (cmdline, "guestfs_network=1") != NULL;

#ifndef WIN32
  /* Make sure SIGPIPE doesn't kill us. */
  struct sigaction sa;
  memset (&sa, 0, sizeof sa);
  sa.sa_handler = SIG_IGN;
  sa.sa_flags = 0;
  if (sigaction (SIGPIPE, &sa, NULL) == -1)
    perror ("sigaction SIGPIPE"); /* but try to continue anyway ... */
#endif

#ifdef WIN32
# define setenv(n,v,f) _putenv(n "=" v)
#endif
  /* Set up a basic environment.  After we are called by /init the
   * environment is essentially empty.
   * https://bugzilla.redhat.com/show_bug.cgi?id=502074#c5
   *
   * NOTE: if you change $PATH, you must also change 'prog_exists'
   * function below.
   */
  setenv ("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1);
  setenv ("SHELL", "/bin/sh", 1);
  setenv ("LC_ALL", "C", 1);
  setenv ("TERM", "dumb", 1);

#ifndef WIN32
  /* We document that umask defaults to 022 (it should be this anyway). */
  umask (022);
#else
  /* This is the default for Windows anyway.  It's not even clear if
   * Windows ever uses this -- the MSDN documentation for the function
   * contains obvious errors.
   */
  _umask (0);
#endif

  /* Make a private copy of /etc/lvm so we can change the config (see
   * daemon/lvm-filter.c).
   */
  copy_lvm ();

  /* Connect to virtio-serial channel. */
  char *channel, *p;
  if (cmdline && (p = strstr (cmdline, "guestfs_channel=")) != NULL) {
    p += 16;
    channel = strndup (p, strcspn (p, " \n"));
  }
  else
    channel = strdup (VIRTIO_SERIAL_CHANNEL);
  if (!channel) {
    perror ("strdup");
    exit (EXIT_FAILURE);
  }

  if (verbose)
    printf ("trying to open virtio-serial channel '%s'\n", channel);

  int sock = open (channel, O_RDWR|O_CLOEXEC);
  if (sock == -1) {
    fprintf (stderr,
             "\n"
             "Failed to connect to virtio-serial channel.\n"
             "\n"
             "This is a fatal error and the appliance will now exit.\n"
             "\n"
             "Usually this error is caused by either QEMU or the appliance\n"
             "kernel not supporting the vmchannel method that the\n"
             "libguestfs library chose to use.  Please run\n"
             "'libguestfs-test-tool' and provide the complete, unedited\n"
             "output to the libguestfs developers, either in a bug report\n"
             "or on the libguestfs redhat com mailing list.\n"
             "\n");
    perror (channel);
    exit (EXIT_FAILURE);
  }

  /* If it's a serial-port like device then it probably has echoing
   * enabled.  Put it into complete raw mode.
   */
  if (STRPREFIX (channel, "/dev/ttyS"))
    makeraw (channel, sock);

  /* cmdline, channel not used after this point */
  free (cmdline);
  free (channel);

  /* Wait for udev devices to be created.  If you start libguestfs,
   * especially with disks that contain complex (eg. mdadm) data
   * already, then it is possible for the 'mdadm' and LVM commands
   * that the init script runs to have not completed by the time the
   * daemon starts executing library commands.  (This is very rare and
   * hard to test however, but we have seen it in 'brew').  Run
   * udev_settle, but do it as late as possible to minimize the chance
   * that we'll have to do any waiting here.
   */
  udev_settle ();

  /* Send the magic length message which indicates that
   * userspace is up inside the guest.
   */
  char lenbuf[4];
  XDR xdr;
  uint32_t len = GUESTFS_LAUNCH_FLAG;
  xdrmem_create (&xdr, lenbuf, sizeof lenbuf, XDR_ENCODE);
  xdr_u_int (&xdr, &len);

  if (xwrite (sock, lenbuf, sizeof lenbuf) == -1) {
    perror ("xwrite");
    exit (EXIT_FAILURE);
  }

  xdr_destroy (&xdr);

  /* Enter the main loop, reading and performing actions. */
  main_loop (sock);

  exit (EXIT_SUCCESS);
}