Skip to content

Commit

Permalink
Document fork, exec, wait and redirect2null Lua functions as deprecated
Browse files Browse the repository at this point in the history
Rpm scriptlets should have no business dealing with this level of
detail in process control, rpm.execut() is much saner and safer
for scriptlet needs. We can't just remove because this is a public
API with known users (eg glibc in Fedora), but we can at least document
these as deprecated with noisy warnings.

fork() and exec() are the main "problems" here, but wait() and
redirect2null() become meaningless once you take the first two away.

Fixes: #2420
  • Loading branch information
pmatilai authored and ffesti committed Mar 14, 2024
1 parent 2084a89 commit 46bd0ed
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
22 changes: 16 additions & 6 deletions docs/manual/lua.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ f = f:reopen('r.gzdio')
print(f:read())}
```

#### redirect2null(fdno)
#### redirect2null(fdno) (DEPRECATED)

Redirect file descriptor fdno to /dev/null (rpm >= 4.16, in rpm 4.13-4.15
this was known as posix.redirect2null())
Expand All @@ -307,6 +307,9 @@ elseif pid > 0 then
end
```

This function is deprecated and scheduled for removal in 6.0,
use `rpm.execute()` instead.

#### undefine(name)

Undefine a macro (rpm >= 4.14.0)
Expand Down Expand Up @@ -440,10 +443,12 @@ if not posix.chmod(f, 100) then
end
```

#### exec(path [, args...])
#### exec(path [, args...]) (DEPRECATED)

Execute a program. This may only be performed after posix.fork().
For executing external commands it's recommended to use `rpm.execute()` instead.

This function is deprecated and scheduled for removal in 6.0,
use `rpm.execute()` instead.

#### files([path])

Expand All @@ -456,10 +461,12 @@ for f in posix.files('/') do
end
```

#### fork()
#### fork() (DEPRECATED)

Fork a new process.
For executing external commands it's recommended to use `rpm.execute()` instead.

This function is deprecated and scheduled for removal in 6.0,
use `rpm.execute()` instead.

```
pid = posix.fork()
Expand Down Expand Up @@ -724,7 +731,7 @@ posix.mkdir('aaa')
posix.utime('aaa', 0, 0)
```

#### wait([pid])
#### wait([pid]) (DEPRECATED)

Wait for a child process. If pid is specified wait for that particular child.

Expand All @@ -737,6 +744,9 @@ elseif pid > 0 then
end
```

This function is deprecated and scheduled for removal in 6.0,
use `rpm.execute()` instead.

#### setenv(name, value [, overwrite])

Change or add an environment variable. The optional overwrite is a boolean
Expand Down
10 changes: 10 additions & 0 deletions rpmio/lposix.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@

extern int _rpmlua_have_forked;

#define check_deprecated() \
fprintf(stderr, \
"warning: posix.%s(): .fork(), .exec(), .wait() and .redirect2null() are deprecated, use rpm.execute() instead\n", __func__+1)

static const char *filetype(mode_t m)
{
if (S_ISREG(m)) return "regular";
Expand Down Expand Up @@ -333,6 +337,8 @@ static int Pmkfifo(lua_State *L) /** mkfifo(path) */

static int Pexec(lua_State *L) /** exec(path,[args]) */
{
check_deprecated();

const char *path = luaL_checkstring(L, 1);
int i,n=lua_gettop(L);
char **argv;
Expand All @@ -355,6 +361,8 @@ static int Pexec(lua_State *L) /** exec(path,[args]) */

static int Pfork(lua_State *L) /** fork() */
{
check_deprecated();

pid_t pid = fork();
if (pid == 0) {
_rpmlua_have_forked = 1;
Expand All @@ -365,6 +373,8 @@ static int Pfork(lua_State *L) /** fork() */

static int Pwait(lua_State *L) /** wait([pid]) */
{
check_deprecated();

pid_t pid = luaL_optinteger(L, 1, -1);
return pushresult(L, waitpid(pid, NULL, 0), NULL);
}
Expand Down
4 changes: 3 additions & 1 deletion tests/rpmmacro.at
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,8 @@ runroot_other rpmlua -e 'pid = posix.fork(); if pid == 0 then a,b,c=rpm.redirect
[0],
[nil Bad file descriptor 9.0
],
[warning: runaway fork() in Lua script]
[warning: posix.fork(): .fork(), .exec(), .wait() and .redirect2null() are deprecated, use rpm.execute() instead
warning: posix.wait(): .fork(), .exec(), .wait() and .redirect2null() are deprecated, use rpm.execute() instead
warning: runaway fork() in Lua script]
)
RPMTEST_CLEANUP

0 comments on commit 46bd0ed

Please sign in to comment.