Bug 236564 - periodic.sh: Anticongestion function does not work as is expected.
Summary: periodic.sh: Anticongestion function does not work as is expected.
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-bugs mailing list
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2019-03-16 01:12 UTC by Yasuhiro KIMURA
Modified: 2019-04-22 21:46 UTC (History)
0 users

See Also:
yasu: mfc-stable11?
yasu: mfc-stable12?


Attachments
Patch file (749 bytes, patch)
2019-03-16 01:12 UTC, Yasuhiro KIMURA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Yasuhiro KIMURA 2019-03-16 01:12:31 UTC
Created attachment 202893 [details]
Patch file

I. Summary

Under some conditions anticongestion function in /etc/default/periodic.conf does not work as is expected

II. Backgroup

Anticongestion function is defined in /etc/default/periodic.conf. If I understand correctly the purpose of it is to insert random sleep just one time through all periodic scripts even if it is called more than once.

III. Problem Description

Currently (at base r345186) /etc/periodic/daily/480.leapfile-ntpd is a only periodic script on base system that calls anticongestion function. So let me assume following situation.

* There is no /etc/weekly.local.
* There is no periodic script under /usr/local/etc/periodic/security.
* There is only one periodic script 123-foo under /usr/local/etc/periodic/weekly.
* 123-foo calls anticongestion function in it.

Then what happens when 'periodic weekly' is executed from cron? Periodic(8) executes scripts with following order.

1. /etc/periodic/weekly/310.locate
2. /etc/periodic/weekly/320.whatis
3. /etc/periodic/weekly/340.noid
4. /etc/periodic/weekly/450.status-security
  4.01. /etc/periodic/security/100.chksetuid
  4.02. /etc/periodic/security/110.neggrpperm
  4.03. /etc/periodic/security/200.chkmounts
  4.04. /etc/periodic/security/300.chkuid0
  4.05. /etc/periodic/security/400.passwdless
  4.06. /etc/periodic/security/410.logincheck
  4.07. /etc/periodic/security/500.ipfwdenied
  4.08. /etc/periodic/security/510.ipfdenied
  4.09. /etc/periodic/security/520.pfdenied
  4.10. /etc/periodic/security/550.ipfwlimit
  4.11. /etc/periodic/security/610.ipf6denied
  4.12. /etc/periodic/security/700.kernelmsg
  4.13. /etc/periodic/security/800.loginfail
  4.14. /etc/periodic/security/900.tcpwrap
5. /etc/periodic/weekly/999.local
6. /usr/local/etc/periodic/weekly/123-foo

As is explained above no script under /etc/periodic/weekly and /etc/periodic/security calls anticongestion function. So expected behavior is that anticongestion function is called and random sleep insterted at step 6. But it is different from real one. What really happens is that anticongestion function is called at step 6 but random sleep is not inserted.

IV. Source of Problem

The source of problem is that periodic(8) is executed twice when 'periodic weekly' is executed from cron. That is, first when it is executed from cron, and next when /etc/periodic/weekly/450.status-security is executed.

There is following codes at line 79-81 of /usr/sbin/periodic.

----------------------------------------------------------------------
if [ -z "$PERIODIC_ANTICONGESTION_FILE" ] ; then
	export PERIODIC_ANTICONGESTION_FILE=`mktemp ${TMPDIR:-/tmp}/periodic.anticongestion.XXXXXXXXXX`
fi
----------------------------------------------------------------------

And there is following code at last line of /usr/sbin/periodic.

----------------------------------------------------------------------
rm -f $PERIODIC_ANTICONGESTION_FILE
----------------------------------------------------------------------

Anticongestion function is defined in /etc/default/periodic.conf as following.

----------------------------------------------------------------------
	# Sleep for a random amount of time in order to mitigate the thundering
	# herd problem of multiple hosts running periodic simultaneously.
	# Will not sleep when used interactively.
	# Will sleep at most once per invocation of periodic
	anticongestion() {
		[ -n "$PERIODIC_IS_INTERACTIVE" ] && return
		if [ -f "$PERIODIC_ANTICONGESTION_FILE" ]; then
			rm -f $PERIODIC_ANTICONGESTION_FILE
			sleep `jot -r 1 0 ${anticongestion_sleeptime}`
		fi
	}
----------------------------------------------------------------------

So what really happens is as following.

a. 'periodic weekly' is executed from cron and master process of periodic(8) is created. 
b. Because PERIODIC_ANTICONGESTION_FILE is not set in master process, temporary file is created, its path is set as value of PERIODIC_ANTICONGESTION_FILE and PERIODIC_ANTICONGESTION_FILE is exported.
c. /etc/periodic/weekly/450.status-security is executed and slave process of periodic(8) is created
d. Because PERIODIC_ANTICONGESTION_FILE is already set in slave process, another temporary file is not created and the value of PERIODIC_ANTICONGESTION_FILE is not changed.
e. At the end of slave process temporary file created at step b is removed.
f. Slave process exits and return to master process.
g. /usr/local/etc/periodic/weekly/123-foo is executed and anticongestion function is called.
h. Because temporary file created at step b is already removed at step e, random sleep is not inserted.

In this way anticongestion function does not work as is expected.

V. Solution

By applying attached patch the problem is fixed.