2

I am producing an rpm for Sia in ClearOS7 (based on Centos7) and have hit a brick wall. I am trying to set SIA_DATA_DIR=/var/lib/siad-data for the session where I am installing the rpm. It is needed for both the sia daemon and for the siac app which is a command line app. For the daemon it is simple enough and I can set it in the systemd unit file.

To try to set it globally I have created a file, /etc/profile.d/siad.sh and in it I've put

[ -x /usr/bin/siad ] && export SIA_DATA_DIR=/var/lib/siad-data

In the %post section of the spec file I have source /etc/profile.d/siad.sh. I can see the profile.d file works for a new shell, but in the current shell it is not work. Guessing, it is because yum is opening a subprocess and the variable does not get back up to the parent process. Putting export SIA_DATA_DIR=/var/lib/siad-data directly in the %post script does not work either.

How can I set the environment variable for the session used to install the rpm in the rpm?

Nick Howitt
  • 21
  • 2
  • 4
  • "... I am guessing that ... back up to the parent ..." is correct. A child process cannot modify the environment (or local) variables of it's parent process. Allowing that would allow JoeHacker to, for example, redirect a shell to a fake shell that grants them root access. You will need to look in to various methods to get the parent and child to communicate with each other to pass the desired information back to the parent. – C. M. Apr 27 '21 at 10:32
  • I have found a real kludge for the current session. I can do `alias siac='SIA_DATA_DIR=/var/lib/siad-data siac'` but it is pig-ugly. It only needs to be for the current session as the profile.d works for all new sessions. – Nick Howitt Apr 27 '21 at 10:45
  • Instead of adding an alias in your current session, you might as well `export SIA_DATA_DIR=/var/lib/siad-data`. – Stephen Kitt Apr 27 '21 at 11:20
  • The alias didn't work. It was a bad test which I did. Export does not work either, possibly because yum is using a subshell, then see the first comment from C. M "A child process cannot modify the environment (or local) variables of it's parent process" – Nick Howitt Apr 27 '21 at 11:45

1 Answers1

1

You can't do it. A process cannot modify the environment of a running process.

There isn't even such a thing as “the current session” when it comes to environment variables. Each process has its own environment variable. And there can be multiple “current sessions”.

It's bad practice for a program to require an environment variable anyway. You should only define an environment variable if it's needed for other software, and even so this should not be necessary at the system level: the software should be compiled or configured to look in the system directories by default.

If the software does require an environment variable to run, run it through a wrapper script that sets the variable. For example, if /usr/bin/siad needs the environment variable SIA_DATA_DIR to be set, then install the executable as /usr/bin/siad.real and make /usr/bin/siad a script containing

#!/bin/sh
if [ -z "${SIA_DATA_DIR+set}" ]; then
  export SIA_DATA_DIR="/var/lib/siad-data"
fi
exec /usr/bin/siad.real "$@"
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • I am struggling here. I can't recompile for various reasons so I am just repackaging the Sia Daemon binaries from https://sia.tech/get-started. Both siad and siac need to see the same variable and they behave differently if it is not set. I can see your method would work but I could set the variable in the systemd unit file for siad. Only siac would need your workaround. – Nick Howitt Apr 27 '21 at 11:57
  • Then replace `siad` with `siac` in the example given in the answer. (Being a shell script might have complications if `siac` needs root permissions.) – C. M. Apr 27 '21 at 12:29