# A bit contrived as most packages will either create a subdirectory or a single file. Not both
# Make sure permissions are correct
touch %{buildroot}/run/%{name}.pid
chmod 0644 %{buildroot}/run/%{name}.pid
An example:
Asking the tmpfiles.d mechanism to create directories for you just involves dropping a file into `+%{_tmpfilesdir}+`. You will need a build dependency on systemd-rpm-macros in order to make use of this macro.
d /run/mysqld 0755 mysql mysql -
`+d+` specifies that a directory is to be created if it doesn't exist. You can use a different type specifier if you need it. See `+man tmpfiles.d+` for possible values.
Example spec file
Files (*not directories*) that the program places directly into `+/run+` are listed in the `+%files+` section as `+%verify(not size mtime md5)+` so that rpm knows the file must exist as part of this package but will not complain when the file contents change. Files placed in the subdirectories may be listed the same way or omitted entirely as the files will be cleaned up on every reboot.
# Use %attr() if needed to change ownership of these two items
%dir /run/%{name}/
%verify(not size mtime md5) /run/%{name}.pid
For example, if the package needs a few directories to be created in `+/run+` in order for it to run, the packager needs to create a file named `+%{name}.conf+` that is installed as `+%{_tmpfilesdir}/%{name}.conf+`. The file has one or more lines of the following format:
# For the _tmpfilesdir macro.
BuildRequires: systemd-rpm-macros
`+GROUP+` is the name of the group of the directory.
Have the daemon create the directory when it starts up
Have the init script create the directory when it starts up the daemon
Having to add the mkdir to the systemd unit files when tmpfiles.d is already in place introduces the need to run shell code for that init script. Systemd is no longer able to handle starting the daemon by itself which slows things down. The shell code also introduces imperative constructs into the otherwise declarative structure which is nice to avoid.
Information on other options is available on the https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html[tmpfiles.d man page] should you need to do something more advanced.
mkdir -p %{buildroot}%{_tmpfilesdir}
install -m 0644 %{SOURCE1} %{buildroot}%{_tmpfilesdir}/%{name}.conf
In the spec file, the packager needs to install the tmpfiles.d conf file into the `+%{_tmpfilesdir}+` directory and also make sure the directory is included in the rpm.
Many times, daemons run as an unprivileged user who would not be allowed to create new directories directly into `+/run+`. If the daemon does not drop privileges, then you can patch it to create the files and directories when the daemon starts and submit the patch upstream.