Bug 13266

Summary: run-postinst fails to fully execute scripts
Product: [Build System, Metadata & Runtime] OE-Core Reporter: Alejandro Hernandez <alejandr>
Component: devtools / tool chainAssignee: Alejandro Hernandez <alejandr>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: Medium+ CC: meta.mr.watcher, meta.watcher, richard.purdie
Version: unspecified   
Target Milestone: 2.7 M4   
Hardware: x86   
OS: Multiple   
Whiteboard:
OS type for building Yocto: --- Type of Regression: ---
Verified: Documentation change: No (bug/feature does not impact docs)

Description Alejandro Hernandez 2019-04-04 23:17:13 UTC
run-postinsts runs a given set of scripts during the first boot of the device,
when one of these scripts prints something to stdout, since stdout is
not available at that time, the script execution immediately returns with
an error (exit_group()), this error causes the script to terminate all threads within the process, causing undesired behavior since the script might still had to execute some other code.

There could be a case where the script to be run performs several commands, e.g. 
script_ran_at_boot:
cmda
cmdb
cmdc

Now, lets say that cmda echoes something to stdout.
stdout does not exist at this point, this is what produces the error and terminates execution (according to strace), hence cmdb and cmdc are never run, (its worth noting that cmda does get executed so if it was a single command, this would not be a big issue).

The reason why we have not seen this before (I believe) is because most or all of the scripts being run, either dont print anything to stdout or are one single command, also, this is only reproducible when debug-tweaks isn't enabled and since we have it enabled by default, we dont see this error, and that is because the following line in the run-postinsts script uses a file for logging purposes:

eval sh -c $i $append_log

but, the variable $append_log is obtained when sourcing the file:

[ -e #SYSCONFDIR#/default/postinst ] && . #SYSCONFDIR#/default/postinst

if [ "$POSTINST_LOGGING" = "1" ]; then
	rm -f $LOGFILE
	append_log=">>$LOGFILE 2>&1"
fi


and we can see that from the class:

https://git.yoctoproject.org/cgit/cgit.cgi/poky/tree/meta/classes/rootfs-postcommands.bbclass#n12

postinst_enable_logging () {
  mkdir -p ${IMAGE_ROOTFS}${sysconfdir}/default
  echo "POSTINST_LOGGING=1" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
  echo "LOGFILE=${POSTINST_LOGFILE}" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
}

We need to be able to make this work even when debug-tweaks isn't enabled, I believe a single and harmless fix would be to always pass 2>&1 even when POSTINST_LOGGING isn't enabled.