Page MenuHomeFreeBSD

Avoid switching to root twice for saving options
Needs ReviewPublic

Authored by dim on May 25 2017, 1:53 PM.
Tags
None
Referenced Files
Unknown Object (File)
Feb 19 2024, 10:05 PM
Unknown Object (File)
Feb 10 2024, 9:32 PM
Unknown Object (File)
Jan 1 2024, 5:25 AM
Unknown Object (File)
Nov 11 2023, 7:08 PM
Unknown Object (File)
Nov 7 2023, 6:19 PM
Unknown Object (File)
Nov 6 2023, 9:39 PM
Unknown Object (File)
Nov 1 2023, 1:24 AM
Unknown Object (File)
Oct 10 2023, 6:05 PM
Subscribers

Details

Reviewers
bapt
bdrewery
Group Reviewers
O5: Ports Framework(Owns No Changed Paths)
portmgr
Summary

When creating new options for a port, bsd.port.mk switches to root
twice: first for creating the options directory (/var/db/ports/foo_bar),
second for copying the temporary options file to that directory.

Avoid this by using SU_CMD just once, creating the options directory and
copying the temporary options file in one command.

(Some of the commands are now pretty long, but wrapping them to 80
characters makes them rather awkward. Suggestions are greatly
appreciated. :)

Diff Detail

Repository
rP FreeBSD ports repository
Lint
No Lint Coverage
Unit
No Test Coverage
Build Status
Buildable 9489
Build 9939: arc lint + arc unit

Event Timeline

The patch works when I run 'make config' as root, but I get this output:

> Switching to root credentials to write /var/db/ports/comms_dcf77pi/options

> Returning to user credentials

It works fine as user, but somehow I do not see why, line 4731 seems to suggest that /var/db/ports/foo_bar must exist (true) and (be writable for non-root (false) or not owned by the current user (true)) for the non-root commands to be run.
When run as root, this line evaluates to /var/db/ports/foo_bar exists (true) and (writable for root (true) or not owned by current user (false)) so in that case the "then" part of the if clause should be run, not the "else" part

I've been wondering why this is needed, and then, I saw that in my make.conf, I have:

SU_CMD=/usr/local/bin/sudo -E sh -c
In D10898#268363, @mat wrote:

I've been wondering why this is needed, and then, I saw that in my make.conf, I have:

SU_CMD=/usr/local/bin/sudo -E sh -c

Indeed, that is almost exactly the same thing I have:

SU_CMD=${LOCALBASE}/bin/sudo -E /bin/sh -c

IIRC this comes from one or other handbook or guide, but I forgot which. :)

In D10898#265281, @rene wrote:

The patch works when I run 'make config' as root, but I get this output:

> Switching to root credentials to write /var/db/ports/comms_dcf77pi/options

> Returning to user credentials

It works fine as user, but somehow I do not see why, line 4731 seems to suggest that /var/db/ports/foo_bar must exist (true) and (be writable for non-root (false) or not owned by the current user (true)) for the non-root commands to be run.
When run as root, this line evaluates to /var/db/ports/foo_bar exists (true) and (writable for root (true) or not owned by current user (false)) so in that case the "then" part of the if clause should be run, not the "else" part

You shouldn't run as root. ;) But joking aside, it might indeed be better if the statement ${MKDIR} $${optionsdir} 2> /dev/null && ${CAT} $${TMPOPTIONSFILE} > ${OPTIONS_FILE}" is not repeated twice, but somehow folded into one.

Mk/bsd.port.mk
4731

I don't think [ -e "${optionsdir}" ] is correct, or even needed here. We do not care wether the directory exists at all.

Say you are running as root and it does not exist, you end up running the code through ${SU_CMD} where you really do not need to.