Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 20 Jun 2014 20:16:50 +0100
From:      Ben Morrow <ben@morrow.me.uk>
To:        lists@jnielsen.net, freebsd-stable@freebsd.org
Subject:   Re: freebsd-update from script without tty
Message-ID:  <20140620191647.GA22531@anubis.morrow.me.uk>
In-Reply-To: <295AB5F0-1601-4AF6-BCE1-FD592DE26FB1@jnielsen.net>

next in thread | previous in thread | raw e-mail | index | archive | help
Quoth John Nielsen <lists@jnielsen.net>:
> 
> I have a script that builds virtual FreeBSD disk images. After it
> populates the base system it runs "freebsd-update -b ${MOUNTPOINT} ..."
> to update the image with the latest security patch release.
> 
> If I run the script from a terminal it works fine. However if I try to
> run it from our user-friendly web interface (which spawns the script in
> the background with no tty) the freebsd-update step fails:
> 
> # UNAME_r=10.0-RELEASE freebsd-update -b /mnt/chroot fetch
> freebsd-update fetch should not be run non-interactively.
> Run freebsd-update cron instead.
> 
> I understand and appreciate the use case for 'freebsd-update cron' but
> in this case my script is running because a user requested and is
> waiting for it, and a delay of up to an hour won't really fly.
> 
> Can anyone suggest a workaround?

The obvious answer would be to run it on a pty. If you can't easily do
this from your script directly, something like this might do:

    #!/usr/bin/perl

    use IO::Pty;
    use IO::Select;

    my $pt = IO::Pty->new;
    my $sl = $pt->slave;

    unless (my $kid = fork) {
        close $pt;
        open STDIN, "<&", $sl;
        open STDOUT, ">&", $sl;
        open STDERR, ">&", $sl;
        close $sl;
        exec @ARGV;
    }

    close $sl;
    my $sel = IO::Select->new($pt);
    $pt->blocking(0);
    $/ = \4096; $| = 1;

    while (1) {
        $sel->can_read;
        $_ = <$pt>;
        defined or last;
        print;
    }

    waitpid $kid, 0;
    exit $?>>8 ? $?>>8 : $? ? 1 : 0;

Add proper error checking / signal handling / etc. to taste.

Ben




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20140620191647.GA22531>