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

init: fix daemon forking #2521

Merged
merged 6 commits into from
Jun 22, 2022
Merged
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
7 changes: 5 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,11 @@ AC_CHECK_HEADERS([endian.h sys/endian.h byteswap.h stdio.h stdlib.h unistd.h str

AC_CHECK_DECLS([strnlen])

# Check for daemon(3), unrelated to --with-daemon (although used by it)
AC_CHECK_DECLS([daemon])
dnl These are used for daemonization in bitcoind
AC_CHECK_DECLS([fork])
AC_CHECK_DECLS([setsid])

AC_CHECK_DECLS([pipe2])

AC_CHECK_DECLS([le16toh, le32toh, le64toh, htole16, htole32, htole64, be16toh, be32toh, be64toh, htobe16, htobe32, htobe64],,,
[#if HAVE_ENDIAN_H
Expand Down
82 changes: 82 additions & 0 deletions contrib/init/gridcoinresearchd.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# It is not recommended to modify this file in-place, because it will
# be overwritten during package upgrades. If you want to add further
# options or overwrite existing ones then use
# $ systemctl edit gridcoinresearchd.service
# See "man systemd.service" for details.

# Note that almost all daemon options could be specified in
# /etc/gridcoin/gridcoin.conf, but keep in mind those explicitly
# specified as arguments in ExecStart= will override those in the
# config file.

[Unit]
Description=Gridcoin daemon
Documentation=https://github.com/gridcoin-community/Gridcoin-Research/blob/development/doc/gridcoinresearch.conf.md

# https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/bin/gridcoinresearchd -daemonwait \
-pid=/run/gridcoinresearchd/gridcoinresearchd.pid \
-conf=/etc/gridcoin/gridcoin.conf \
-datadir=/var/lib/gridcoinresearchd

# Make sure the config directory is readable by the service user
PermissionsStartOnly=true
ExecStartPre=/bin/chgrp gridcoin /etc/gridcoin

# Process management
####################

Type=forking
PIDFile=/run/gridcoinresearchd/gridcoinresearchd.pid
Restart=on-failure
TimeoutStartSec=infinity
TimeoutStopSec=600

# Directory creation and permissions
####################################

# Run as gridcoin:gridcoin
User=gridcoin
Group=gridcoin

# /run/gridcoinresearchd
RuntimeDirectory=gridcoinresearchd
RuntimeDirectoryMode=0710

# /etc/gridcoin
ConfigurationDirectory=gridcoin
ConfigurationDirectoryMode=0710

# /var/lib/gridcoinresearchd
StateDirectory=gridcoinresearchd
StateDirectoryMode=0710

# Hardening measures
####################

# Provide a private /tmp and /var/tmp.
PrivateTmp=true

# Mount /usr, /boot/ and /etc read-only for the process.
ProtectSystem=full

# Deny access to /home, /root and /run/user
ProtectHome=true

# Disallow the process and all of its children to gain
# new privileges through execve().
NoNewPrivileges=true

# Use a new /dev namespace only populated with API pseudo devices
# such as /dev/null, /dev/zero and /dev/random.
PrivateDevices=true

# Deny the creation of writable and executable memory mappings.
MemoryDenyWriteExecute=true

[Install]
WantedBy=multi-user.target
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,11 @@ GRIDCOIN_CORE_H = \
util/settings.h \
util/strencodings.h \
util/string.h \
util/syserror.h \
util/system.h \
util/threadnames.h \
util/time.h \
util/tokenpipe.h \
util.h \
validation.h \
version.h \
Expand Down Expand Up @@ -296,9 +298,11 @@ GRIDCOIN_CORE_CPP = addrdb.cpp \
util/settings.cpp \
util/strencodings.cpp \
util/string.cpp \
util/syserror.cpp \
util/system.cpp \
util/threadnames.cpp \
util/time.cpp \
util/tokenpipe.cpp \
util.cpp \
validation.cpp \
wallet/db.cpp \
Expand Down
7 changes: 6 additions & 1 deletion src/fs.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// Copyright (c) 2017-2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://opensource.org/licenses/mit-license.php.

#include <fs.h>
#include <util/syserror.h>

#ifndef WIN32
#include <fcntl.h>
Expand Down Expand Up @@ -34,7 +39,7 @@ fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
#ifndef WIN32

static std::string GetErrorReason() {
return std::strerror(errno);
return SysErrorString(errno);
}

FileLock::FileLock(const fs::path& file)
Expand Down
Loading