[Toybox] xargs with blank input

Robert Thompson robertt.thompson at gmail.com
Wed Oct 16 21:35:15 PDT 2013


Not that it's really relevant to most linux environments, but the OSX
version (with a BSD manpage) appears to not execute any commandline that
would have no added arguments. A quick test suggests this is true for both
normal and null-separated modes.

Specifically, echo "" | xargs echo foo; echo "returned $?" will output
exactly one line: "returned 0", instead of the two lines seen on my debian
test box. On the other hand, echo "bar" | xargs echo foo; echo "returned
$?" behaves identically on both machines.



The manpage mentions that "The xargs utility is expected to be IEEE Std
1003.2 (``POSIX.2'') compliant.", for what it's worth.

It appears that they are reading the "shall construct ..." to instruct that
if no arguments are read, there is no complete commandline, thus the xargs
terminates without ever trying to launch a process, but without any error.
My tests on current osx 10.8 indicate that xargs never execs the utility
and returns 0 for success.



On Wed, Oct 16, 2013 at 9:11 PM, <ibid.ag at gmail.com> wrote:

> On Wed, Oct 16, 2013 at 03:16:47PM -0400, William Haddon wrote:
> > How exactly should this behave? I haven't been able to decipher what
> > POSIX has to say about the subject. The GNU version executes the
> POSIX 2008 (I looked at the old tarball of docs...) says:
>
> "shall construct a command line consisting of the utility and argument
> operands specified followed by as many arguments read in sequence from
> standard input as fit in length and number constraints specified by
> the options."
> I read this as "Must in all cases construct the command line", since
> there's no note about blank input behaving differently.
>
> > command with no arguments, so that "xargs" with blank input is
> > equivalent to "echo" and "xargs ls" with blank input is equivalent
> > to "ls". Toybox currently appears to do nothing, but actually forks
> > a child process which silently seg-faults as a result of calling
> > xexec() with argv[0] == NULL. Patching toybox to implement the GNU
> > behavior fixes a lot of scripts on my test system that broke when I
> > switched the xargs implementation from busybox to toybox, including
> > the
> > Linux build scripts. I included the patch below, but I haven't
> > tested it with all the numerous options that are available to xargs.
> > I'm still investigating whether this toybox behavior is the only
> > factor contributing to behavior I observed.
> >
> > William Haddon
> >
> > diff --git a/toys/posix/xargs.c b/toys/posix/xargs.c
> > index e1611ec..6238af8 100644
> > --- a/toys/posix/xargs.c
> > +++ b/toys/posix/xargs.c
> > @@ -160,17 +160,16 @@ void xargs_main(void)
> >      if (data && !TT.entries) error_exit("argument too long");
> >      out = xzalloc((entries+TT.entries+1)*sizeof(char *));
> >
> > -    if (dlist) {
> > -      struct double_list *dtemp;
> > +    struct double_list *dtemp;
> >
> > -      // Fill out command line to exec
> > -      memcpy(out, toys.optargs, entries*sizeof(char *));
> > -      TT.entries = 0;
> > -      TT.bytes = bytes;
> > +    // Fill out command line to exec
> > +    memcpy(out, toys.optargs, entries*sizeof(char *));
> > +    TT.entries = 0;
> > +    TT.bytes = bytes;
> > +    if (dlist)
> >        dlist->prev->next = 0;
> > -      for (dtemp = dlist; dtemp; dtemp = dtemp->next)
> > -        handle_entries(dtemp->data, out+entries);
> > -    }
> > +    for (dtemp = dlist; dtemp; dtemp = dtemp->next)
> > +      handle_entries(dtemp->data, out+entries);
> >      pid_t pid=fork();
> >      if (!pid) {
> >        xclose(0);
> I thought I saw an issue with brace placement, but I didn't...
>
> Thanks,
> Isaac Dunham
> _______________________________________________
> Toybox mailing list
> Toybox at lists.landley.net
> http://lists.landley.net/listinfo.cgi/toybox-landley.net
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20131016/c00a8d93/attachment-0005.htm>


More information about the Toybox mailing list