From f6ae5e4753277ea35509c36dc618097b1175d632 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Fri, 17 Feb 2017 15:40:58 +0100 Subject: [PATCH 1/1] ipc: Prefer key_t over int for System V IPC keys. get_key() calls ftok(3), which returns a key_t value. key_t is also the type which semget(2), the only function which receives the key via mutex_get(), expects. It's stupid to convert the key_t from ftok(3) into an int, only to convert it back to key_t later. This patch changes ipc.c to use key_t everywhere. However, in mutex_get() we print a log message containing the value of the key, so the format string must be adjusted accordingly. Unfortunately, on Linux, key_t is the same as int while on FreeBSD and NetBSD it is defined as long. To avoid a warning from the compiler we use "%lx" in the format string and cast the value to long. --- ipc.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/ipc.c b/ipc.c index 723570e..bc8995a 100644 --- a/ipc.c +++ b/ipc.c @@ -22,9 +22,9 @@ #include "err.h" #include "ipc.h" -static int get_key(const char *config_file) +static key_t get_key(const char *config_file) { - int ret; + key_t ret; assert(config_file); ret = ftok(config_file, 'D'); @@ -40,11 +40,11 @@ static int get_key(const char *config_file) return 0x0D55; /* no magic, this number just looks a bit like DSS */ } -static int mutex_get(int key, int flags) +static int mutex_get(key_t key, int flags) { int ret; - DSS_DEBUG_LOG(("getting semaphore 0x%x\n", key)); + DSS_DEBUG_LOG(("getting semaphore 0x%lx\n", (long)key)); ret = semget(key, 2, flags); if (ret < 0) return -ERRNO_TO_DSS_ERROR(errno); @@ -108,7 +108,8 @@ static bool mutex_is_locked(int id) int lock_dss(char *config_file) { - int ret, key = get_key(config_file); + int ret; + key_t key = get_key(config_file); ret = mutex_get(key, IPC_CREAT | 0600); if (ret < 0) @@ -118,7 +119,8 @@ int lock_dss(char *config_file) int get_dss_pid(char *config_file, pid_t *pid) { - int ret, semid, key = get_key(config_file); + int ret, semid; + key_t key = get_key(config_file); if (pid) *pid = 0; -- 2.39.2