ipc: Prefer key_t over int for System V IPC keys.
authorAndre Noll <maan@tuebingen.mpg.de>
Fri, 17 Feb 2017 14:40:58 +0000 (15:40 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Thu, 13 Jul 2017 17:02:30 +0000 (19:02 +0200)
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

diff --git a/ipc.c b/ipc.c
index 723570e60b291811b8de24214159d18423557ee3..bc8995aeef86590d5755635466bb86ab419b1133 100644 (file)
--- 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;