Skip to content

Commit

Permalink
Filter Lua deprecation warnings based on the originating rpm version
Browse files Browse the repository at this point in the history
Issuing deprecation warnings on packages built long time ago is
just antisocial behavior when the user is powerless to do anything
about it.

When running scripts, pass the builder rpm version to Lua through the
registry, and filter out the warnings when the package was built with
an rpm version where these functions were not yet deprecated.
Usage through rpmlua and macros always gets warnings: those are things
that can technically be fixed by the user.
  • Loading branch information
pmatilai authored and ffesti committed Sep 23, 2024
1 parent dd41700 commit 532e6b9
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 8 deletions.
11 changes: 11 additions & 0 deletions lib/rpmscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct rpmScript_s {
char *body; /* script body */
char *descr; /* description for logging */
rpmscriptFlags flags; /* flags to control operation */
char *rpmver; /* builder rpm version */
int chroot; /* chrooted script? */
struct scriptNextFileFunc_s *nextFileFunc; /* input function */
};
Expand Down Expand Up @@ -179,11 +180,19 @@ static rpmRC runLuaScript(rpmPlugins plugins, ARGV_const_t prefixes,
mode_t oldmask = umask(0);
umask(oldmask);

lua_pushstring(L, "RPM_PACKAGE_RPMVERSION");
lua_pushstring(L, script->rpmver);
lua_settable(L, LUA_REGISTRYINDEX);

if (chdir("/") == 0 &&
rpmluaRunScript(lua, scriptbuf, script->descr, NULL, *argvp) == 0) {
rc = RPMRC_OK;
}

lua_getfield(L, LUA_REGISTRYINDEX, "RPM_PACKAGE_RPMVERSION");
lua_pushnil(L);
lua_settable(L, LUA_REGISTRYINDEX);

/* This failing would be fatal, return something different for it... */
if (fchdir(cwd)) {
rpmlog(RPMLOG_ERR, _("Unable to restore current directory: %m"));
Expand Down Expand Up @@ -530,6 +539,7 @@ static rpmScript rpmScriptNew(Header h, rpmTagVal tag, const char *body,
script->type = getScriptType(tag);
script->flags = getDefFlags(tag) | flags;
script->body = (body != NULL) ? xstrdup(body) : NULL;
script->rpmver = headerGetAsString(h, RPMTAG_RPMVERSION);
script->chroot = 1;
rasprintf(&script->descr, "%%%s%s(%s)", prefix, tag2sln(tag), nevra);

Expand Down Expand Up @@ -706,6 +716,7 @@ rpmScript rpmScriptFree(rpmScript script)
free(script->args);
free(script->body);
free(script->descr);
free(script->rpmver);
delete script->nextFileFunc;
delete script;
}
Expand Down
6 changes: 3 additions & 3 deletions rpmio/lposix.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ static int Pmkfifo(lua_State *L) /** mkfifo(path) */

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

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

static int Pfork(lua_State *L) /** fork() */
{
check_deprecated(L, "posix.fork");
check_deprecated(L, "posix.fork", "4.20.0");

pid_t pid = fork();
if (pid == 0) {
Expand All @@ -366,7 +366,7 @@ static int Pfork(lua_State *L) /** fork() */

static int Pwait(lua_State *L) /** wait([pid]) */
{
check_deprecated(L, "posix.wait");
check_deprecated(L, "posix.wait", "4.20.0");

pid_t pid = luaL_optinteger(L, 1, -1);
return pushresult(L, waitpid(pid, NULL, 0), NULL);
Expand Down
2 changes: 1 addition & 1 deletion rpmio/lposix.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <rpm/rpmutil.h>

RPM_GNUC_INTERNAL
void check_deprecated(lua_State *L, const char *func);
void check_deprecated(lua_State *L, const char *func, const char *deprecated_in);

RPM_GNUC_INTERNAL
int luaopen_posix (lua_State *L);
Expand Down
22 changes: 18 additions & 4 deletions rpmio/rpmlua.c
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ static int rpm_redirect2null(lua_State *L)
{
int target_fd, fd, r, e;

check_deprecated(L, "rpm.redirect2null");
check_deprecated(L, "rpm.redirect2null", "4.20.0");

if (!_rpmlua_have_forked)
return luaL_error(L, "redirect2null not permitted in this context");
Expand Down Expand Up @@ -1365,8 +1365,22 @@ static int luaopen_rpm(lua_State *L)
return 1;
}

void check_deprecated(lua_State *L, const char *func)
void check_deprecated(lua_State *L, const char *func, const char *deprecated_in)
{
fprintf(stderr,
"warning: %s(): .fork(), .exec(), .wait() and .redirect2null() are deprecated, use rpm.spawn() or rpm.execute() instead\n", func);
int warn = 1;
lua_getfield(L, LUA_REGISTRYINDEX, "RPM_PACKAGE_RPMVERSION");
if (lua_isstring(L, -1)) {
rpmver v1 = rpmverParse(lua_tostring(L, -1));
rpmver v2 = rpmverParse(deprecated_in);
if (v1 && v2 && rpmverCmp(v1, v2) < 0)
warn = 0;
rpmverFree(v2);
rpmverFree(v1);
}
lua_pop(L, 1);

if (warn) {
fprintf(stderr,
"warning: %s(): .fork(), .exec(), .wait() and .redirect2null() are deprecated, use rpm.spawn() or rpm.execute() instead\n", func);
}
}
Binary file added tests/data/RPMS/luafork-1.0-1.noarch.rpm
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/data/SPECS/luafork.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Name: luafork
Version: 1.0
Release: 1
License: Public domain
Summary: Testing Lua fork behavior
BuildArch: noarch

%description
%{summary}

%pre -p <lua>
local pid = posix.fork()
if pid == 0 then
io.stdout:write("child\n")
os.exit(0)
elseif pid > 0 then
posix.wait(pid)
else
io.stderr:write("fork failed")
end

%files
28 changes: 28 additions & 0 deletions tests/rpmscript.at
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,31 @@ runroot_other test -f /tmp/scriptwrite.log
[])
RPMTEST_CLEANUP

AT_SETUP([deprecated Lua functions])
AT_KEYWORDS([script lua])

# binary pre-built on rpm 4.18 should emit no warnings
RPMTEST_CHECK([
RPMDB_INIT
runroot rpm -U /data/RPMS/luafork-1.0-1.noarch.rpm
],
[0],
[child
],
[])

RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet /data/SPECS/luafork.spec
runroot rpm -U /build/RPMS/noarch/luafork-1.0-1.noarch.rpm
],
[0],
[child
],
[warning: posix.fork(): .fork(), .exec(), .wait() and .redirect2null() are deprecated, use rpm.spawn() or rpm.execute() instead
warning: posix.wait(): .fork(), .exec(), .wait() and .redirect2null() are deprecated, use rpm.spawn() or rpm.execute() instead
])

RPMTEST_CLEANUP

0 comments on commit 532e6b9

Please sign in to comment.