Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resolv_conf_mv=auto option #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SED_RCDIR= -e 's:@RCDIR@:${RCDIR}:g'
SED_RESTARTCMD= -e 's:@RESTARTCMD@:${RESTARTCMD}:g'
SED_RCDIR= -e 's:@RCDIR@:${RCDIR}:g'
SED_STATUSARG= -e 's:@STATUSARG@:${STATUSARG}:g'
SED_STAT_FMT= -e 's:@STAT_FMT@:${STAT_FMT}:g'

DISTPREFIX?= ${PKG}-${VERSION}
DISTFILE?= ${DISTPREFIX}.tar.xz
Expand All @@ -46,6 +47,8 @@ DISTINFOMD= ${DISTINFO}.md
DISTSIGN= ${DISTFILE}.asc
SHA256?= sha256
PGP?= gpg
STAT_FMT?= stat -c


GITREF?= HEAD

Expand All @@ -57,6 +60,7 @@ all: ${TARGET}
${SED} ${SED_SBINDIR} ${SED_SYSCONFDIR} ${SED_LIBEXECDIR} \
${SED_VARDIR} \
${SED_RCDIR} ${SED_RESTARTCMD} ${SED_RCDIR} ${SED_STATUSARG} \
${SED_STAT_FMT} \
$< > $@

clean:
Expand Down
6 changes: 6 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ linux*)
# cksum doesn't support -a and netpgp is rare
echo "CKSUM= sha256sum --tag" >>$CONFIG_MK
echo "PGP= gpg2" >>$CONFIG_MK
echo "STAT_FMT= stat -c" >>$CONFIG_MK
;;
esac

Expand All @@ -106,6 +107,10 @@ dragonfly*|freebsd*)
if [ -z "$STATUSARG" ]; then
STATUSARG="onestatus"
fi
echo "STAT_FMT= stat -f" >>$CONFIG_MK
;;
*bsd*)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the whole stat check needs to be in it's own section.
I'm tempted to suggest that we assume BSD style by default and add an exception for Linux.

The only potential issue I think is kFreeBSD which may use the coreutils stat rather than the FreeBSD one.
Also some stat's just don't offer the functionality we need such as Plan9 or disabled as in the case for busybox.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those unusual cases should be able to handle by make STAT_FMT=. I am not sure how often is crossbuilding done for such systems. I think configure could test the output by actually checking stat -c %d . and that it does report a number. Not sure if such check would work on busybox systems. At least stat from alpine docker image and BusyBox v1.36.1 has -c %d support.

I expect Linux systems are dominant today. But if the BSD variant is likely to work on different systems, which may use openresolv, it would be okay. It should have own case "$OS" just for stat check, is that what you mean?

echo "STAT_FMT= stat -f" >>$CONFIG_MK
;;
esac

Expand Down Expand Up @@ -164,4 +169,5 @@ echo
echo " RESTARTCMD = $RESTARTCMD"
echo " RCDIR = $RCDIR"
echo " STATUSARG = $STATUSARG"
echo " STAT_FMT = $STAT_FMT"
echo
44 changes: 37 additions & 7 deletions libc.in
Original file line number Diff line number Diff line change
Expand Up @@ -227,23 +227,53 @@ if $backup; then
fi
fi

# There are pros and cons for writing directly to resolv.conf
# instead of a temporary file and then moving it over.
# The default is to write to resolv.conf as it has the least
# issues and has been the long standing default behaviour.
case "${resolv_conf_mv:-NO}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
resolv_conf_mk_tmp()
{
# Protect against symlink attack, ensure new file does not exist
rm -f "$resolv_conf_tmp"
# Keep original file owner, group and mode
[ -r "$resolv_conf" ] && cp -p "$resolv_conf" "$resolv_conf_tmp"
}

resolv_conf_do_mv()
{
# Create our resolv.conf now
if (umask 022; printf %s "$newconf" >"$resolv_conf_tmp"); then
mv "$resolv_conf_tmp" "$resolv_conf"
fi
}

resolv_conf_do_print()
{
(umask 022; printf %s "$newconf" >"$resolv_conf")
}

# There are pros and cons for writing directly to resolv.conf
# instead of a temporary file and then moving it over.
# The default is to write to resolv.conf as it has the least
# issues and has been the long standing default behaviour.
case "${resolv_conf_mv:-NO}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
resolv_conf_mk_tmp
resolv_conf_do_mv
;;
# Do mv only if resolv_conf_tmp and resolv_conf are on
# the same filesystem. Use print method for bind-mounted file
[Aa][Uu][Tt][Oo])
if [ -r "$resolv_conf" ] && [ -n "@STAT_FMT@" ]; then
resolv_conf_mk_tmp
if [ "$(@STAT_FMT@ %d "$resolv_conf_tmp")" = "$(@STAT_FMT@ %d "$resolv_conf")" ]; then
resolv_conf_do_mv
else
rm -f "$resolv_conf_tmp"
resolv_conf_do_print
fi
else
resolv_conf_do_print
fi
;;
*)
(umask 022; printf %s "$newconf" >"$resolv_conf")
resolv_conf_do_print
;;
esac

Expand Down