Bug 232950 - execle in FreeBSD work differ than execle in Linux ?
Summary: execle in FreeBSD work differ than execle in Linux ?
Status: Closed Not A Bug
Alias: None
Product: Base System
Classification: Unclassified
Component: misc (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-11-04 09:45 UTC by aborche
Modified: 2022-03-17 17:50 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description aborche 2018-11-04 09:45:09 UTC
FreeBSD execle system call works wrong in code written for Linux.

In many Linux sources i see next system call notation like
execle("/sbin/shutdown", "shutdown", "-h", shutdown_flag, "+0",
               "hypervisor initiated shutdown", (char*)NULL, environ);

Where second argument used as short "program name".
In FreeBSD systems, this argument is not bypassed in call and attached to args list.

As a result, when the source is ported to FreeBSD, the system call is not executed correctly.
---- truss output start ----
946: execve("/sbin/shutdown",[ "shutdown", "-h", "-r", "+0", "hypervisor initiated shutdown" ],0x7fffffffeac0) = 0 (0x0)
946: geteuid() = 0 (0x0)
946: write(2,"shutdown: ",10) = 10 (0xa)
946: write(2,"incompatible switches -c, -h, -k"...,43) = 43 (0x2b)
---- truss output end ----

If we change system call to
execle("/sbin/shutdown", "-h", shutdown_flag, "+0",
               "hypervisor initiated shutdown", (char*)NULL, environ);

all is ok.

But, may be this an error in args parsing in FreeBSD "shutdown" ?
Comment 1 Ed Maste freebsd_committer freebsd_triage 2022-03-17 01:33:36 UTC
From the man page,

     int
     execle(const char *path, const char *arg, ..., NULL, char *const envp[]);

A simple example like

execle("/bin/echo", "echo", "test", NULL, environ);

works as expected (echoes test).
Comment 2 Cameron Katri 2022-03-17 17:45:51 UTC
The issue is that you are specifying both -h and -r which are incompatible in the first example. In the second example, argv[0] is being set to -h and is not being parsed as an argument, allowing -r to work.