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

RFC: Stop depending on libsystemd for socket activation #171

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
11 changes: 0 additions & 11 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,6 @@ AC_CHECK_LIB(socket, socket, [LIBS="$LIBS -lsocket"])
# check for mq_getattr()
AC_CHECK_LIB(rt, mq_getattr, [LIBS="$LIBS -lrt"])

# check for libsystemd
AC_ARG_ENABLE(libsystemd,
AS_HELP_STRING([--disable-libsystemd], [do not use libsystemd]),
[ use_libsystemd="${enableval}" ], [ use_libsystemd="yes" ] )
AS_IF([test "$use_libsystemd" != "no"],
[PKG_CHECK_MODULES(LIBSYSTEMD, libsystemd,,
[ AC_MSG_ERROR([install libsystemd-dev or use --disable-libsystemd]) ])
AC_DEFINE(USE_LIBSYSTEMD, 1, [Use libsystemd])
PCSCLITE_FEATURES="${PCSCLITE_FEATURES} libsystemd"])

# --disable-serial
AC_ARG_ENABLE(serial,
AS_HELP_STRING([--disable-serial], [do not use serial reader.conf files]),
Expand Down Expand Up @@ -415,7 +405,6 @@ ATR parsing messages: ${debugatr}
ipcdir: ${ipcdir}
use serial: ${use_serial}
use usb: ${use_usb}
use libsystemd: ${use_libsystemd}
systemd unit directory: ${with_systemdsystemunitdir}
serial config dir.: ${confdir_exp}
filter: ${use_filter}
Expand Down
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ pcscd_SOURCES = \
winscard_svc.c \
winscard_svc.h
pcscd_CFLAGS = $(CFLAGS) $(PTHREAD_CFLAGS) $(LIBUSB_CFLAGS) $(LIBUDEV_CFLAGS) \
$(POLKIT_CFLAGS) $(LIBSYSTEMD_CFLAGS) \
$(POLKIT_CFLAGS) \
-DPCSCD -DSIMCLIST_NO_DUMPRESTORE
pcscd_LDFLAGS = $(LDFLAGS) $(LIBSYSTEMD_LIBS) -export-dynamic
pcscd_LDFLAGS = $(LDFLAGS) -export-dynamic
pcscd_LDADD = \
$(PTHREAD_LIBS) $(COREFOUNDATION) \
$(LIBUSB_LIBS) $(IOKIT) $(LIBUDEV_LIBS) \
Expand Down
16 changes: 3 additions & 13 deletions src/pcscdaemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#ifdef USE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif

#include "misc.h"
#include "pcsclite.h"
Expand Down Expand Up @@ -441,11 +438,7 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}

#ifdef USE_LIBSYSTEMD
/*
* Check if systemd passed us any file descriptors
*/
rv = sd_listen_fds(0);
rv = GetListenFdCount();
if (rv > 1)
{
Log1(PCSC_LOG_CRITICAL, "Too many file descriptors received");
Expand All @@ -456,12 +449,11 @@ int main(int argc, char **argv)
if (rv == 1)
{
SocketActivated = true;
Log1(PCSC_LOG_INFO, "Started by systemd");
Log1(PCSC_LOG_INFO, "Started with socket activation");
}
else
SocketActivated = false;
}
#endif

/*
* test the presence of /var/run/pcscd/pcscd.comm
Expand Down Expand Up @@ -720,11 +712,9 @@ int main(int argc, char **argv)
/*
* Initialize the comm structure
*/
#ifdef USE_LIBSYSTEMD
if (SocketActivated)
rv = ListenExistingSocket(SD_LISTEN_FDS_START + 0);
rv = ListenExistingSocket(LISTEN_FDS_START + 0);
else
#endif
rv = InitializeSocket();

if (rv)
Expand Down
37 changes: 37 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <dirent.h>
#include <fcntl.h>
#include <pthread.h>
#include <limits.h>

#include "config.h"
#include "debuglog.h"
Expand Down Expand Up @@ -187,4 +188,40 @@ int ThreadCreate(pthread_t * pthThread, int attributes,
pthread_attr_destroy(&attr);
return ret;
}

int GetListenFdCount(void)
{
long long num;
char *endptr;
const char *envv;
int flags;

envv = getenv("LISTEN_PID");
if (envv == NULL)
return 0;

errno = 0;
num = strtoll(envv, &endptr, 0);
if (errno != 0 || *endptr != '\0' || num != getpid())
return 0;

envv = getenv("LISTEN_FDS");
if (envv == NULL)
return 0;

errno = 0;
num = strtoll(envv, &endptr, 0);
if (errno != 0 || *endptr != '\0' || num < 0 || num > INT_MAX - LISTEN_FDS_START)
return 0;

for (int fd = LISTEN_FDS_START; fd < LISTEN_FDS_START + num; fd++) {
flags = fcntl(fd, F_GETFD);
if (flags == -1)
return -1;
if (fcntl(fd, F_SETFD, flags | O_CLOEXEC) == -1)
return -1;
}

return num;
}
#endif
3 changes: 3 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ int CheckForOpenCT(void);
int ThreadCreate(pthread_t *, int, PCSCLITE_THREAD_FUNCTION( ),
/*@null@*/ LPVOID);

#define LISTEN_FDS_START 3
int GetListenFdCount(void);

#endif

#endif
Expand Down
21 changes: 14 additions & 7 deletions src/winscard_msg_srv.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#ifdef USE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif

#include "misc.h"
#include "pcscd.h"
Expand Down Expand Up @@ -165,7 +162,6 @@ INTERNAL int32_t InitializeSocket(void)
return 0;
}

#ifdef USE_LIBSYSTEMD
/**
* @brief Acquires a socket passed in from systemd.
*
Expand All @@ -180,16 +176,27 @@ INTERNAL int32_t InitializeSocket(void)
*/
INTERNAL int32_t ListenExistingSocket(int fd)
{
if (!sd_is_socket(fd, AF_UNIX, SOCK_STREAM, -1))
{
socklen_t sockopt_size, sockaddr_size;
struct sockaddr_storage ss;
int rv, sockopt;

sockaddr_size = sizeof ss;
rv = getsockname(fd, (struct sockaddr *)&ss, &sockaddr_size);
if (rv == -1 || sockaddr_size < sizeof ss.ss_family || ss.ss_family != AF_UNIX) {
Log1(PCSC_LOG_CRITICAL, "Passed FD is not an UNIX socket");
return -1;
}

sockopt_size = sizeof sockopt;
rv = getsockopt(fd, SOL_SOCKET, SO_TYPE, &sockopt, &sockopt_size);
if (rv == -1 || sockopt_size != sizeof sockopt || sockopt != SOCK_STREAM) {
Log1(PCSC_LOG_CRITICAL, "Passed FD is not a stream type socket");
return -1;
}

commonSocket = fd;
return 0;
}
#endif

/**
* @brief Looks for messages sent by clients.
Expand Down