Commit Graph

35 Commits

Author SHA1 Message Date
Kyle Evans
f0fbdf1f4f execvPe: obviate the need for potentially large stack allocations
Some environments in which execvPe may be called have a limited amount of
stack available. Currently, it avoidably allocates a segment on the stack
large enough to hold PATH so that it may be mutated and use strsep() for
easy parsing. This logic is now rewritten to just operate on the immutable
string passed in and do the necessary math to extract individual paths,
since it will be copying out those segments to another buffer anyways and
piecing them together with the name for a full path.

Additional size is also needed for the stack in posix_spawnp(), because it
may need to push all of argv to the stack and rebuild the command with sh in
front of it. We'll make sure it's properly aligned for the new thread, but
future work should likely make rfork_thread a little easier to use by
ensuring proper alignment.

Some trivial cleanup has been done with a couple of error writes, moving
strings into char arrays for use with the less fragile sizeof().

Reported by:	Andrew Gierth <andrew_tao173.riddles.org.uk>
Reviewed by:	jilles, kib, Andrew Gierth
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D25038
2020-06-10 01:32:13 +00:00
Kyle Evans
301cb491ea execvp: fix up the ENOEXEC fallback
If execve fails with ENOEXEC, execvp is expected to rebuild the command
with /bin/sh instead and try again.

The previous version did this, but overlooked two details:

argv[0] can conceivably be NULL, in which case memp would never get
terminated.  We must allocate no less than three * sizeof(char *) so we can
properly terminate at all times. For the non-NULL argv standard case, we
count all the non-NULL elements and actually skip the first argument, so we
end up capturing the NULL terminator in our bcopy().

The second detail is that the spec is actually worded such that we should
have been preserving argv[0] as passed to execvp:

"[...] executed command shall be as if the process invoked the sh utility
using execl() as follows:

execl(<shell path>, arg0, file, arg1, ..., (char *)0);

where <shell path> is an unspecified pathname for the sh utility, file is
the process image file, and for execvp(), where arg0, arg1, and so on
correspond to the values passed to execvp() in argv[0], argv[1], and so on."

So we make this change at this time as well, while we're already touching
it. We decidedly can't preserve a NULL argv[0] as this would be incredibly,
incredibly fragile, so we retain our legacy behavior of using "sh" for
argv[] in this specific instance.

Some light tests are added to try and detect some components of handling the
ENOEXEC fallback; posix_spawnp_enoexec_fallback_null_argv0 is likely not
100% reliable, but it at least won't raise false-alarms and it did result in
useful failures with pre-change libc on my machine.

This is a secondary change in D25038.

Reported by:	Andrew Gierth <andrew_tao173.riddles.org.uk>
Reviewed by:	jilles, kib, Andrew Gierth
MFC after:	1 week
2020-06-10 01:30:37 +00:00
John Baldwin
c1920558b3 Clean up the vcs ID strings in libc's gen/ directory.
- Move CSRG IDs into __SCCSID().
- When a file has been copied, consistently use 'From: <tag>' for strings
  referencing the version of the source file copied from in the license
  block comment.
- Some of the 'From:' tags were using $FreeBSD$ that was being expanded on
  each checkout.  Fix those to hardcode the FreeBSD tag from the file that
  was copied at the time of the copy.
- When multiple strings are present list them in "chronological" order,
  so CSRG (__SCCSID) before FreeBSD (__FBSDID).  If a file came from
  OtherBSD and contains a CSRG ID from the OtherBSD file, use the order
  CSRG -> OtherBSD -> FreeBSD.

Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D15831
2018-07-03 17:31:45 +00:00
Pedro F. Giffuni
8a16b7a18f General further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
2017-11-20 19:49:47 +00:00
Warner Losh
fbbd9655e5 Renumber copyright clause 4
Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by:	Jan Schaumann <jschauma@stevens.edu>
Pull Request:	https://github.com/freebsd/freebsd/pull/96
2017-02-28 23:42:47 +00:00
Andriy Voskoboinyk
2cf5e9365d libc: do not include <sys/types.h> where <sys/param.h> was already included
According to style(9):
> normally, include <sys/types.h> OR <sys/param.h>, but not both.
(<sys/param.h> already includes <sys/types.h> when LOCORE is not defined).
2016-04-18 21:05:15 +00:00
Craig Rodrigues
55b6b759c8 Use ANSI C prototypes. Eliminates -Wold-style-definition warnings. 2015-09-20 20:23:16 +00:00
Ed Schouten
b3608ae18f Replace index() and rindex() calls with strchr() and strrchr().
The index() and rindex() functions were marked LEGACY in the 2001
revision of POSIX and were subsequently removed from the 2008 revision.
The strchr() and strrchr() functions are part of the C standard.

This makes the source code a lot more consistent, as most of these C
files also call into other str*() routines. In fact, about a dozen
already perform strchr() calls.
2012-01-03 18:51:58 +00:00
Ed Schouten
bbbe3054ff Remove warnings from exec.c.
The entries in the argv array are not const themselves, but sometimes we
want to fill in const values. Just make the array const and use
__DECONST() to make it const for the execve()-call itself.

Also convert the only K&R prototype to ANSI.
2009-12-05 18:55:16 +00:00
Ed Schouten
f67d07f0e1 Properly use the envp argument in execvPe().
execvPe() is called by _execvpe(), which we added to implement
posix_spawnp(). We just took execvP() and added the envp argument.
Unfortunately we forgot to change the implementation to use envp over
environ.

This fixes the following piece of code:

| char * const arg[2] = { "env", NULL };
| char * const env[2] = { "FOO=BAR", NULL };
| posix_spawnp(NULL, "/usr/bin/env", NULL, NULL, arg, env);

MFC after:	2 weeks
2009-11-27 13:05:14 +00:00
Ed Schouten
c605eea952 Turn execvpe() into an internal libc routine.
Adding exevpe() has caused some ports to break. Even though execvpe() is
a useful routine, it does not conform to any standards.

This patch is a little bit different from the patch sent to the mailing
list. I forgot to remove execvpe from the Symbol.map (which does not
seem to miscompile libc, though).

Reviewed by:	davidxu
Approved by:	philip
2008-06-23 05:22:06 +00:00
David Xu
947aa542e9 Add POSIX routines called posix_spawn() and posix_spawnp(), which
can be used as replacements for exec/fork in a lot of cases. This
change also added execvpe() which allows environment variable
PATH to be used for searching executable file, it is used for
implementing posix_spawnp().

PR: standards/122051
2008-06-17 06:26:29 +00:00
Warner Losh
c879ae3536 Per Regents of the University of Calfornia letter, remove advertising
clause.

# If I've done so improperly on a file, please let me know.
2007-01-09 00:28:16 +00:00
Bruce Evans
270f6e44db Fixed some style bugs. 2003-07-01 12:30:03 +00:00
Gordon Tetlow
09f49aab84 Add a libc function execvP that takes the search path as an arguement.
Change execvp to be a wrapper around execvP. This is necessary for some
of the /rescue pieces. It may also be more generally applicable as well.

Submitted by:	Tim Kientzle <kientzle@acm.org>
Approved by:	Silence on arch@
2003-06-29 17:33:34 +00:00
Tim J. Robbins
d3701b0441 Remove an unused variable, `sverrno', which has not been used since 1.11. 2003-01-03 23:16:55 +00:00
Alfred Perlstein
a82bbc730e Assume __STDC__, remove non-__STDC__ code.
Submitted by: keramida
2002-05-28 17:03:12 +00:00
David E. O'Brien
22626efa0f * Remove 'register'. (some functions had 7+ register functions...)
* Fix SCM ID's.
2002-02-01 01:08:48 +00:00
Ruslan Ermilov
8af1452cf8 Removed duplicate VCS ID tags, as per style(9). 2001-08-13 14:06:34 +00:00
Daniel Eischen
d201fe46e3 Remove _THREAD_SAFE and make libc thread-safe by default by
adding (weak definitions to) stubs for some of the pthread
functions.  If the threads library is linked in, the real
pthread functions will pulled in.

Use the following convention for system calls wrapped by the
threads library:
	__sys_foo - actual system call
	_foo - weak definition to __sys_foo
	foo - weak definition to __sys_foo

Change all libc uses of system calls wrapped by the threads
library from foo to _foo.  In order to define the prototypes
for _foo(), we introduce namespace.h and un-namespace.h
(suggested by bde).  All files that need to reference these
system calls, should include namespace.h before any standard
includes, then include un-namespace.h after the standard
includes and before any local includes.  <db.h> is an exception
and shouldn't be included in between namespace.h and
un-namespace.h  namespace.h will define foo to _foo, and
un-namespace.h will undefine foo.

Try to eliminate some of the recursive calls to MT-safe
functions in libc/stdio in preparation for adding a mutex
to FILE.  We have recursive mutexes, but would like to avoid
using them if possible.

Remove uneeded includes of <errno.h> from a few files.

Add $FreeBSD$ to a few files in order to pass commitprep.

Approved by:	-arch
2001-01-24 13:01:12 +00:00
Jason Evans
9233c4d942 Simplify sytem call renaming. Instead of _foo() <-- _libc_foo <-- foo(),
just use _foo() <-- foo().  In the case of a libpthread that doesn't do
call conversion (such as linuxthreads and our upcoming libpthread), this
is adequate.  In the case of libc_r, we still need three names, which are
now _thread_sys_foo() <-- _foo() <-- foo().

Convert all internal libc usage of: aio_suspend(), close(), fsync(), msync(),
nanosleep(), open(), fcntl(), read(), and write() to _foo() instead of foo().

Remove all internal libc usage of: creat(), pause(), sleep(), system(),
tcdrain(), wait(), and waitpid().

Make thread cancellation fully POSIX-compliant.

Suggested by:	deischen
2000-01-27 23:07:25 +00:00
Jason Evans
929273386f Add three-tier symbol naming in support of POSIX thread cancellation
points.  For library functions, the pattern is __sleep() <--
_libc_sleep() <-- sleep().  The arrows represent weak aliases.  For
system calls, the pattern is _read() <-- _libc_read() <-- read().
2000-01-12 09:23:48 +00:00
Peter Wemm
7f3dea244c $Id$ -> $FreeBSD$ 1999-08-28 00:22:10 +00:00
Peter Wemm
5fe5a4dd73 Fix a (relatively harmless) braino. I confused myself over the for() loop
that counted the number of elements in argv.  The counter is incremented
in the next-iteration section of the loop, not the body, so at termination
it's already "counted" the element that failed the continuation test - in
this case the NULL argv terminator.

Noted by:  bde
1999-03-24 02:43:05 +00:00
Peter Wemm
ce04fea445 Remove last remaining references to malloc/realloc and functions that
call them.  All the execX() libc functions should be vfork() safe now.
Specifically:
- execlp() does the argument count-and-build into a vector from alloca
    (like the others) - buildargv() is no longer used (and gone).
- execvp() uses alloca/strcpy rather than strdup().
- the ENOEXEC handler uses alloca rather than malloc.
- a couple of free() calls removed - alloca works on the local stack and
    the allocations are freed on function exit (which is why buildargv
    wasn't useful - it's alloca() context would disappear on return).
Along the way:
- If alloca() fails (can it?), set errno = ENOMEM explicitly.
- The ENOEXEC recovery routine that trys again with /bin/sh appeared to
    not be terminating the new argv[] array for /bin/sh, allowing it to
    walk off the end of the list.

I dithered a bit about using alloca() even more as it's most commonly
associated with gcc.  However, standalone portable (using malloc) and
machine-specific assembler alloca implementations appear to be available
on just about all the architectures we're likely to want to port to.
alloca will be the least of our problems if ever going to another compiler.
1999-03-23 16:40:34 +00:00
Dag-Erling Smørgrav
af806462dc Fix style issues in execl(), and make execle() vfork()-safe.
Reviewed by:	bde
Approved by:	jkh
1998-10-15 17:14:15 +00:00
Dag-Erling Smørgrav
7ea577e550 Correct braino in previous commit. I get the pointy hat again. 1998-10-14 20:23:40 +00:00
Dag-Erling Smørgrav
dea625c872 Make execl() vfork()-safe. This should fix potential bugs in rcp,
telnet and tip, and probably a few other apps.

Reviewed by:	bde
Approved by:	jkh
1998-10-14 18:53:36 +00:00
Bruce Evans
70df31a627 stat() the correct file in execvp() so that the fine tuned errno handling
actually works.
1997-11-20 15:09:38 +00:00
Bruce Evans
1df595f25d Fixed searching of $PATH in execvp(). Do what sh(1) should do according
to POSIX.2.  In particular:

- don't retry for ETXTBSY.  This matches what sh(1) does.  The retry code
  was broken anyway.  It only slept for several seconds for the first few
  retries.  Then it retried without sleeping.
- don't abort the search for errors related to the path prefix, in
  particular for ENAMETOOLONG, ENOTDIR, ELOOP.  This fixes PR1487.  sh(1)
  gets this wrong in the opposite direction by never aborting the search.
- don't confuse EACCES for errors related to the path prefix with EACCES
  for errors related to the file.  sh(1) gets this wrong.
- don't return a stale errno when the search terminates normally without
  finding anything.  The errno for the last unsuccessful execve() was
  usually returned.  This gave too much precedence to pathologies in the
  last component of $PATH.  This bug is irrelevant for sh(1).

The implementation still uses the optimization/race-inhibitor of trying
to execve() things first.  POSIX.2 seems to require looking at file
permissions using stat().  We now use stat() after execve() if execve()
fails with an ambiguous error.  Trying execve() first may actually be a
pessimization, since failing execve()s are fundamentally a little slower
than stat(), and are significantly slower when a file is found but has
unsuitable permissions or points to an unsuitable interpreter.

PR:		1487
1997-10-14 07:23:16 +00:00
Bruce Evans
766631018f Fixed execvp() of an empty pathname to fail POSIXly. Previously it
attempted to exec the components of $PATH and it usually set errno
to the wrong value.

Found by:	NIST PCTS
1996-11-18 19:24:47 +00:00
Bruce Evans
1ad652a54c Fixed uninitialized variables for the '/'-in-pathname case in execvp().
Garbage in `eacces' caused the wrong errno to be set for non-EACCES errors.
Garbage in `etxtbsy' caused a semi-random retry strategy for ETXTBSY errors.

Found by:	NIST-PCTS.  gcc -Wall reported the problem, but -Wall is not
		enabled for libc.
1996-11-18 16:56:51 +00:00
Jordan K. Hubbard
51295a4d3e General -Wall warning cleanup, part I.
Submitted-By: Kent Vander Velden <graphix@iastate.edu>
1996-07-12 18:57:58 +00:00
Bruce Evans
a2c0622293 Fix execl[e]. Multiple execle's failed because of bogus caching of the
pointer returned by realloc().  All callers free the pointer if the
execve fails.  Nuke the caching.  This essentially restores buildargv()
to the 1.1.5 version.  Also fix a memory leak if realloc() fails.  Also
nuke similar but non-broken caching in execvp().  malloc() should be
efficient enough.
1994-12-12 01:15:01 +00:00
Rodney W. Grimes
58f0484fa2 BSD 4.4 Lite Lib Sources 1994-05-27 05:00:24 +00:00