Snapserver is running as snapcast user

This commit is contained in:
BadAix 2017-01-08 16:55:39 +01:00
parent b82970fab4
commit adcb19ee3f
3 changed files with 27 additions and 11 deletions

View file

@ -37,14 +37,14 @@ int pidFilehandle;
void daemonize(const std::string& user, const std::string& group, const std::string& pidfile) void daemonize(const std::string& user, const std::string& group, const std::string& pidfile)
{ {
if (pidfile.empty() || pidfile.find('/') == std::string::npos) if (pidfile.empty() || pidfile.find('/') == std::string::npos)
throw SnapException("invalid pid file \"" + pidfile + "\""); throw SnapException("invalid pid file \"" + pidfile + "\"");
std::string pidfileDir(pidfile.substr(0, pidfile.find_last_of('/'))); std::string pidfileDir(pidfile.substr(0, pidfile.find_last_of('/')));
mkdirRecursive(pidfileDir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); mkdirRecursive(pidfileDir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
/// Ensure only one copy /// Ensure only one copy
pidFilehandle = open(pidfile.c_str(), O_RDWR|O_CREAT, 0600); pidFilehandle = open(pidfile.c_str(), O_RDWR|O_CREAT, 0644);
if (pidFilehandle == -1 ) if (pidFilehandle == -1 )
{ {
/// Couldn't open lock file /// Couldn't open lock file
@ -58,7 +58,7 @@ void daemonize(const std::string& user, const std::string& group, const std::str
bool had_group = false; bool had_group = false;
#endif #endif
if (!user.empty()) if (!user.empty())
{ {
struct passwd *pwd = getpwnam(user.c_str()); struct passwd *pwd = getpwnam(user.c_str());
if (pwd == nullptr) if (pwd == nullptr)
@ -69,7 +69,7 @@ void daemonize(const std::string& user, const std::string& group, const std::str
/// this is needed by libs such as arts /// this is needed by libs such as arts
setenv("HOME", pwd->pw_dir, true); setenv("HOME", pwd->pw_dir, true);
} }
if (!group.empty()) { if (!group.empty()) {
struct group *grp = getgrnam(group.c_str()); struct group *grp = getgrnam(group.c_str());
if (grp == nullptr) if (grp == nullptr)
@ -81,7 +81,7 @@ void daemonize(const std::string& user, const std::string& group, const std::str
} }
/// set gid /// set gid
if (user_gid != (gid_t)-1 && user_gid != getgid() && setgid(user_gid) == -1) if (user_gid != (gid_t)-1 && user_gid != getgid() && setgid(user_gid) == -1)
throw SnapException("Failed to set group " + cpt::to_string((int)user_gid)); throw SnapException("Failed to set group " + cpt::to_string((int)user_gid));
//#if defined(FREEBSD) && !defined(MACOS) //#if defined(FREEBSD) && !defined(MACOS)

View file

@ -65,6 +65,7 @@ endif
BIN = snapserver BIN = snapserver
all: $(TARGET) all: $(TARGET)
$(TARGET): $(OBJ) $(TARGET): $(OBJ)
@ -86,8 +87,7 @@ dpkg:
ifdef DESTDIR ifdef DESTDIR
install: install:
install -D -g root -o root $(BIN) $(TARGET_DIR)/bin/$(BIN) $(MAKE) installfiles
install -D -g root -o root $(BIN).1 $(TARGET_DIR)/share/man/man1/$(BIN).1
else ifeq ($(TARGET), FREEBSD) else ifeq ($(TARGET), FREEBSD)
@ -109,8 +109,10 @@ install:
else else
install: install:
install -D -g root -o root $(BIN) $(TARGET_DIR)/bin/$(BIN) $(MAKE) adduser
install -D -g root -o root $(BIN).1 $(TARGET_DIR)/share/man/man1/$(BIN).1 $(MAKE) installfiles
install -g audio -o snapcast -d /var/run/$(BIN)
@if [[ `systemctl` =~ -\.mount ]]; then \ @if [[ `systemctl` =~ -\.mount ]]; then \
$(MAKE) installsystemd; \ $(MAKE) installsystemd; \
elif [[ `/sbin/init --version` =~ upstart ]]; then \ elif [[ `/sbin/init --version` =~ upstart ]]; then \
@ -123,6 +125,10 @@ install:
endif endif
installfiles:
install -D -g root -o root $(BIN) $(TARGET_DIR)/bin/$(BIN)
install -D -g root -o root $(BIN).1 $(TARGET_DIR)/share/man/man1/$(BIN).1
installsystemd: installsystemd:
@echo using systemd; \ @echo using systemd; \
cp debian/$(BIN).service /lib/systemd/system/$(BIN).service; \ cp debian/$(BIN).service /lib/systemd/system/$(BIN).service; \
@ -142,6 +148,11 @@ installbsd:
@echo using bsd; \ @echo using bsd; \
cp debian/$(BIN).bsd /usr/local/etc/rc.d/$(BIN); \ cp debian/$(BIN).bsd /usr/local/etc/rc.d/$(BIN); \
adduser:
@if ! getent passwd snapcast >/dev/null; then \
adduser --quiet --ingroup audio --system --no-create-home --home /var/lib/snapcast snapcast; \
fi; \
ifeq ($(TARGET), FREEBSD) ifeq ($(TARGET), FREEBSD)
@ -174,6 +185,8 @@ uninstall:
else \ else \
echo cannot tell; \ echo cannot tell; \
fi; \ fi; \
rm -rf /var/run/$(BIN)
$(MAKE) deluser
endif endif
@ -194,3 +207,6 @@ uninstallsystemd:
rm -f /etc/default/$(BIN); \ rm -f /etc/default/$(BIN); \
systemctl daemon-reload; \ systemctl daemon-reload; \
deluser:
@deluser --quiet --system snapcast > /dev/null || true

View file

@ -144,7 +144,7 @@ int main(int argc, char* argv[])
if (daemonOption.isSet()) if (daemonOption.isSet())
{ {
#ifdef HAS_DAEMON #ifdef HAS_DAEMON
daemonize("snapcast", "audio", "/var/run/snapserver.pid"); daemonize("snapcast", "audio", "/var/run/snapserver/pid");
if (processPriority < -20) if (processPriority < -20)
processPriority = -20; processPriority = -20;
else if (processPriority > 19) else if (processPriority > 19)