]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
Defined macros for POSIX IPC compilation and removed path buffer.
authorPrasanna Paithankar <paithankarprasanna@gmail.com>
Mon, 10 Feb 2025 19:37:55 +0000 (01:07 +0530)
committerPrasanna Paithankar <paithankarprasanna@gmail.com>
Mon, 10 Feb 2025 19:37:55 +0000 (01:07 +0530)
Signed-off-by: Prasanna Paithankar <paithankarprasanna@gmail.com>
configure.ac
meson.build
sys-utils/ipcmk.c
sys-utils/ipcrm.c
sys-utils/ipcutils.c
sys-utils/ipcutils.h
sys-utils/lsipc.c

index f834652a04b43bf187ce379df0a1f45e6356e537..44a4495a788a0995ce9e073b884d395a0bc39c0b 100644 (file)
@@ -356,6 +356,7 @@ AC_CHECK_HEADERS([ \
        linux/if_alg.h \
        locale.h \
        mntent.h \
+  mqueue.h \
        net/if_dl.h \
        net/if.h \
        netinet/in.h \
@@ -363,6 +364,7 @@ AC_CHECK_HEADERS([ \
        pty.h \
        security/pam_appl.h \
        security/pam_modules.h \
+  semaphore.h \
        shadow.h \
        stdint.h \
        stdio_ext.h \
@@ -375,6 +377,7 @@ AC_CHECK_HEADERS([ \
        sys/ioctl.h \
        sys/io.h \
        sys/mkdev.h \
+  sys/mman.h \
        sys/mount.h \
        sys/param.h \
        sys/pidfd.h \
index a54e1e94ba4a838d0348eda18f59214f3588e5d0..1e0f4722ec8737da19f61e615af1bb0b91b49c44 100644 (file)
@@ -199,6 +199,7 @@ headers = '''
         mntent.h
         paths.h
         pty.h
+        semaphore.h
         shadow.h
         stdint.h
         stdio_ext.h
@@ -229,6 +230,7 @@ headers = '''
         linux/tiocl.h
         linux/version.h
         linux/watchdog.h
+        mqueue.h
         net/if.h
         net/if_dl.h
         netinet/in.h
@@ -244,6 +246,7 @@ headers = '''
         sys/ioccom.h
         sys/ioctl.h
         sys/mkdev.h
+        sys/mman.h
         sys/mount.h
         sys/param.h
         sys/pidfd.h
@@ -1464,11 +1467,11 @@ endif
 has_seminfo_type = cc.has_type('struct seminfo', args : '-D_GNU_SOURCE', prefix : '#include <sys/sem.h>')
 
 posixipc_libs = []
-if not cc.has_function('shm_open')
+if not cc.has_function('shm_open') and conf.get('HAVE_SYS_MMAN_H').to_string() == '1'
   posixipc_libs = cc.find_library('rt', required : true)
 endif
 
-if not cc.has_function('sem_close')
+if not cc.has_function('sem_close') and conf.get('HAVE_SEMAPHORE_H').to_string() == '1'
   posixipc_libs += cc.find_library('pthread', required : true)
 endif
 
index 5386d41e3802fe7c4b04f693c8b885fa334a57e3..32c23f79fcca5038a5dac9f7eaa828dc301b6728 100644 (file)
@@ -3,6 +3,9 @@
  *
  *  Copyright (C) 2008 Hayden A. James (hayden.james@gmail.com)
  *  Copyright (C) 2008 Karel Zak <kzak@redhat.com>
+ *  
+ *  2025 Prasanna Paithankar <paithankarprasanna@gmail.com>
+ *     - Added POSIX IPC support
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 #include <stdlib.h>
 #include <sys/ipc.h>
 #include <sys/msg.h>
-#include <mqueue.h>
 #include <sys/sem.h>
-#include <semaphore.h>
 #include <sys/shm.h>
+#include <sys/time.h>
+
+#ifdef HAVE_MQUEUE_H
+#include <mqueue.h>
+#endif
+
+#ifdef HAVE_SEMAPHORE_H
+#include <semaphore.h>
+#endif
+
+#ifdef HAVE_SYS_MMAN_H
 #include <sys/stat.h>
 #include <sys/mman.h>
-#include <sys/time.h>
+#endif
 
 #include "c.h"
 #include "nls.h"
@@ -48,6 +60,15 @@ static int create_shm(size_t size, int permission)
        return shmget(key, size, permission | IPC_CREAT);
 }
 
+#ifndef HAVE_SYS_MMAN_H
+static int create_posix_shm(const char *name __attribute__((__unused__)),
+                                                       size_t size __attribute__((__unused__)),
+                                                       int permission __attribute__((__unused__)))
+{
+       warnx(_("POSIX shared memory is not supported"));
+       return -1;
+}
+#else
 static int create_posix_shm(const char *name, size_t size, int permission)
 {
        int shmfd;
@@ -64,6 +85,7 @@ static int create_posix_shm(const char *name, size_t size, int permission)
        printf(_("POSIX shared memory name: %s\n"), name);
        return 0;
 }
+#endif
 
 static int create_msg(int permission)
 {
@@ -73,13 +95,26 @@ static int create_msg(int permission)
        return msgget(key, permission | IPC_CREAT);
 }
 
+#ifndef HAVE_MQUEUE_H
+static int create_posix_msg(const char *name __attribute__((__unused__)),
+                                                       int permission __attribute__((__unused__)))
+{
+       warnx(_("POSIX message queue is not supported"));
+       return -1;
+}
+#else
 static int create_posix_msg(const char *name, int permission)
 {
-       if (-1 == mq_open(name, O_RDWR | O_CREAT, permission, NULL))
+       mqd_t mqd;
+
+       if (-1 == (mqd = mq_open(name, O_RDWR | O_CREAT, permission, NULL)))
                return -1;
+
+       mq_close(mqd);
        printf(_("POSIX message queue name: %s\n"), name);
        return 0;
 }
+#endif
 
 static int create_sem(int nsems, int permission)
 {
@@ -89,6 +124,14 @@ static int create_sem(int nsems, int permission)
        return semget(key, nsems, permission | IPC_CREAT);
 }
 
+#ifndef HAVE_SEMAPHORE_H
+static int create_posix_sem(const char *name __attribute__((__unused__)),
+                                                       int permission __attribute__((__unused__)))
+{
+       warnx(_("POSIX semaphore is not supported"));
+       return -1;
+}
+#else
 static int create_posix_sem(const char *name, int permission)
 {
        sem_t *sem;
@@ -100,6 +143,7 @@ static int create_posix_sem(const char *name, int permission)
        printf(_("POSIX semaphore name: %s\n"), name);
        return 0;
 }
+#endif
 
 static void __attribute__((__noreturn__)) usage(void)
 {
index 083d450cc1788e66ffe3be34a3efd8a03cca1478..11adf2f80dea1eedfbcde6ef9ceeb01fce56da70 100644 (file)
@@ -242,23 +242,35 @@ static int key_to_id(type_id type, char *s)
 
 static int remove_name(type_id type, char *name)
 {
-       int ret;
+       int ret = 0;
        
        switch (type) {
                case PSHM:
+                       #ifndef HAVE_SYS_MMAN_H
+                       warnx(_("POSIX shared memory is not supported"));
+                       #else
                        if (verbose)
                                printf(_("removing POSIX shared memory `%s'\n"), name);
                        ret = shm_unlink(name);
+                       #endif
                        break;
                case PMSG:
+                       #ifndef HAVE_MQUEUE_H
+                       warnx(_("POSIX message queues are not supported"));
+                       #else
                        if (verbose)
                                printf(_("removing POSIX message queue `%s'\n"), name);
                        ret = mq_unlink(name);
+                       #endif
                        break;
                case PSEM:
+                       #ifndef HAVE_SEMAPHORE_H
+                       warnx(_("POSIX semaphores are not supported"));
+                       #else
                        if (verbose)
                                printf(_("removing POSIX semaphore `%s'\n"), name);
                        ret = sem_unlink(name);
+                       #endif
                        break;
                default:
                        errx(EXIT_FAILURE, "impossible occurred");
@@ -282,7 +294,7 @@ static int remove_name(type_id type, char *name)
                return 1;
        }
        return 0;
-}              
+}
 
 static int remove_all(type_id type)
 {
index 3ec2a0383c81ac0a4c22790322b301c0bfcd0c67..df6d15dd6b8fe945535a1aabf23ff213689ad72c 100644 (file)
@@ -52,6 +52,7 @@ int ipc_msg_get_limits(struct ipc_limits *lim)
        }
 
        /* POSIX IPC */
+       #ifdef HAVE_MQUEUE_H
        FILE *f;
 
        f = fopen(_PATH_PROC_POSIX_IPC_MSGMNI, "r");
@@ -74,6 +75,7 @@ int ipc_msg_get_limits(struct ipc_limits *lim)
                        lim->msgmax_posix = 0;
                fclose(f);
        }
+       #endif
 
        return 0;
 }
@@ -251,6 +253,14 @@ void ipc_shm_free_info(struct shm_data *shmds)
        }
 }
 
+#ifndef HAVE_SYS_MMAN_H
+int posix_ipc_shm_get_info(const char *name __attribute__((unused)),
+                                                  struct posix_shm_data **shmds __attribute__((unused)))
+{
+       warnx(_("POSIX shared memory is not supported"));
+       return -1;
+}
+#else
 int posix_ipc_shm_get_info(const char *name, struct posix_shm_data **shmds)
 {
        DIR *d;
@@ -258,13 +268,15 @@ int posix_ipc_shm_get_info(const char *name, struct posix_shm_data **shmds)
        struct posix_shm_data *p;
        struct dirent *de;
 
+       d = opendir(_PATH_DEV_SHM);
+       if (!d) {
+               warnx(_("cannot open %s"), _PATH_DEV_SHM);
+               return -1;
+       }
+
        p = *shmds = xcalloc(1, sizeof(struct posix_shm_data));
        p->next = NULL;
 
-       d = opendir(_PATH_DEV_SHM);
-       if (!d)
-               err(EXIT_FAILURE, _("cannot open %s"), _PATH_DEV_SHM);
-
        while ((de = readdir(d)) != NULL) {
                if ((de->d_name[0] == '.' && de->d_name[1] == '\0') ||
                    (de->d_name[0] == '.' && de->d_name[1] == '.' &&
@@ -275,16 +287,10 @@ int posix_ipc_shm_get_info(const char *name, struct posix_shm_data **shmds)
                        continue;
 
                struct stat st;
-               char path[PATH_MAX];
-               snprintf(path, sizeof(path), "%s/%s", _PATH_DEV_SHM, de->d_name);
-               if (stat(path, &st) < 0)
+               if (fstatat(dirfd(d), de->d_name, &st, 0) < 0)
                        continue;
-               
-               memset(path, 0, sizeof(path));
-               path[0] = '/';
-               strcat(path, de->d_name);
 
-               p->name = xstrdup(path);
+               p->name = xstrdup(de->d_name);
                p->cuid = st.st_uid;
                p->cgid = st.st_gid;
                p->size = st.st_size;
@@ -292,7 +298,7 @@ int posix_ipc_shm_get_info(const char *name, struct posix_shm_data **shmds)
                p->mtime = st.st_mtime;
 
                if (name != NULL) {
-                       if (strcmp(name, path) == 0) {
+                       if (strcmp(name, p->name) == 0) {
                                i = 1;
                                break;
                        }
@@ -310,6 +316,7 @@ int posix_ipc_shm_get_info(const char *name, struct posix_shm_data **shmds)
        closedir(d);
        return i;
 }
+#endif
 
 void posix_ipc_shm_free_info(struct posix_shm_data *shmds)
 {
@@ -459,6 +466,14 @@ void ipc_sem_free_info(struct sem_data *semds)
        }
 }
 
+#ifndef HAVE_SEMAPHORE_H
+int posix_ipc_sem_get_info(const char *name __attribute__((__unused__)),
+                                                  struct posix_sem_data **semds __attribute__((__unused__)))
+{
+       warnx(_("POSIX semaphore is not supported"));
+       return -1;
+}
+#else
 int posix_ipc_sem_get_info(const char *name, struct posix_sem_data **semds)
 {
        DIR *d;
@@ -466,13 +481,15 @@ int posix_ipc_sem_get_info(const char *name, struct posix_sem_data **semds)
        struct posix_sem_data *p;
        struct dirent *de;
 
+       d = opendir(_PATH_DEV_SHM);
+       if (!d) {
+               warnx(_("cannot open %s"), _PATH_DEV_SHM);
+               return -1;
+       }
+
        p = *semds = xcalloc(1, sizeof(struct posix_sem_data));
        p->next = NULL;
 
-       d = opendir(_PATH_DEV_SHM);
-       if (!d)
-               err(EXIT_FAILURE, _("cannot open %s"), _PATH_DEV_SHM);
-
        while ((de = readdir(d)) != NULL) {
                if ((de->d_name[0] == '.' && de->d_name[1] == '\0') ||
                    (de->d_name[0] == '.' && de->d_name[1] == '.' &&
@@ -483,9 +500,7 @@ int posix_ipc_sem_get_info(const char *name, struct posix_sem_data **semds)
                        continue;
 
                struct stat st;
-               char path[PATH_MAX];
-               snprintf(path, sizeof(path), "%s/%s", _PATH_DEV_SHM, de->d_name);
-               if (stat(path, &st) < 0)
+               if (fstatat(dirfd(d), de->d_name, &st, 0) < 0)
                        continue;
 
                sem_t *sem = sem_open(de->d_name + 4, 0);
@@ -493,19 +508,15 @@ int posix_ipc_sem_get_info(const char *name, struct posix_sem_data **semds)
                        continue;
                sem_getvalue(sem, &p->sval);
                sem_close(sem);
-               
-               memset(path, 0, sizeof(path));
-               path[0] = '/';
-               strcat(path, de->d_name + 4);
 
-               p->sname = xstrdup(path);
+               p->sname = xstrdup(de->d_name + 4);
                p->cuid = st.st_uid;
                p->cgid = st.st_gid;
                p->mode = st.st_mode;
                p->mtime = st.st_mtime;
 
                if (name != NULL) {
-                       if (strcmp(name, path) == 0) {
+                       if (strcmp(name, p->sname) == 0) {
                                i = 1;
                                break;
                        }
@@ -523,6 +534,7 @@ int posix_ipc_sem_get_info(const char *name, struct posix_sem_data **semds)
        closedir(d);
        return i;
 }
+#endif
 
 void posix_ipc_sem_free_info(struct posix_sem_data *semds)
 {
@@ -644,6 +656,14 @@ void ipc_msg_free_info(struct msg_data *msgds)
        }
 }
 
+#ifndef HAVE_MQUEUE_H
+int posix_ipc_msg_get_info(const char *name __attribute__((unused)),
+                                                  struct posix_msg_data **msgds __attribute__((unused)))
+{
+       warnx(_("POSIX message queues are not supported"));
+       return -1;
+}
+#else
 int posix_ipc_msg_get_info(const char *name, struct posix_msg_data **msgds)
 {
        FILE *f;
@@ -652,20 +672,25 @@ int posix_ipc_msg_get_info(const char *name, struct posix_msg_data **msgds)
        struct posix_msg_data *p;
        struct dirent *de;
 
-       p = *msgds = xcalloc(1, sizeof(struct msg_data));
-       p->next = NULL;
-
        if (access(_PATH_DEV_MQUEUE, F_OK) != 0) {
                warnx(_("%s does not seem to be mounted. Use 'mount -t mqueue none %s' to mount it."), _PATH_DEV_MQUEUE, _PATH_DEV_MQUEUE);
                return -1;
        }
 
+       if (name && name[0] != '/') {
+               warnx(_("mqueue name must start with '/': %s"), name);
+               return -1;
+       }
+
        d = opendir(_PATH_DEV_MQUEUE);
        if (!d) {
                warnx(_("cannot open %s"), _PATH_DEV_MQUEUE);
                return -1;
        }
 
+       p = *msgds = xcalloc(1, sizeof(struct msg_data));
+       p->next = NULL;
+
        while ((de = readdir(d)) != NULL) {
                if ((de->d_name[0] == '.' && de->d_name[1] == '\0') ||
                    (de->d_name[0] == '.' && de->d_name[1] == '.' &&
@@ -673,17 +698,12 @@ int posix_ipc_msg_get_info(const char *name, struct posix_msg_data **msgds)
                        continue;
 
                struct stat st;
-               char path[PATH_MAX];
-               snprintf(path, sizeof(path), "%s/%s", _PATH_DEV_MQUEUE, de->d_name);
-
-               int fd = open(path, O_RDONLY);
-               if (fd < 0)
+               if (fstatat(dirfd(d), de->d_name, &st, 0) < 0)
                        continue;
 
-               if (fstat(fd, &st) < 0) {
-                       close(fd);
+               int fd = openat(dirfd(d), de->d_name, O_RDONLY);
+               if (fd < 0)
                        continue;
-               }
 
                f = fdopen(fd, "r");
                if (!f) {
@@ -695,14 +715,10 @@ int posix_ipc_msg_get_info(const char *name, struct posix_msg_data **msgds)
                        continue;
                }
                fclose(f);
-               
-               memset(path, 0, sizeof(path));
-               path[0] = '/';
-               strcat(path, de->d_name);
 
-               p->mname = xstrdup(path);
+               p->mname = strconcat("/", de->d_name);
 
-               mqd_t mq = mq_open(path, O_RDONLY);
+               mqd_t mq = mq_open(p->mname, O_RDONLY);
                struct mq_attr attr;
                mq_getattr(mq, &attr);
                p->q_qnum = attr.mq_curmsgs;
@@ -714,7 +730,7 @@ int posix_ipc_msg_get_info(const char *name, struct posix_msg_data **msgds)
                p->mtime = st.st_mtime;
 
                if (name != NULL) {
-                       if (strcmp(name, path) == 0) {
+                       if (strcmp(name, p->mname) == 0) {
                                i = 1;
                                break;
                        }
@@ -733,6 +749,7 @@ int posix_ipc_msg_get_info(const char *name, struct posix_msg_data **msgds)
 
        return i;
 }
+#endif
 
 void posix_ipc_msg_free_info(struct posix_msg_data *msgds)
 {
index 16284c1038c0e00c58d66f23fd547fcc890c7233..348da98faff0be9a8ba183542e72c16d381c6e3f 100644 (file)
@@ -18,8 +18,6 @@
 #include <sys/msg.h>
 #include <sys/sem.h>
 #include <sys/shm.h>
-#include <mqueue.h>
-#include <semaphore.h>
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
 #include <pwd.h>
 #include <stdint.h>
 
+#ifdef HAVE_MQUEUE_H
+#include <mqueue.h>
+#endif
+
+#ifdef HAVE_SEMAPHORE_H
+#include <semaphore.h>
+#endif
+
 /*
  * SHM_DEST and SHM_LOCKED are defined in kernel headers, but inside
  * #ifdef __KERNEL__ ... #endif
index 2838bb7d87b041563939c0936c491af2a62fedfe..c3cc63f51c6e8cd7c4aa2d930985004064f12fc3 100644 (file)
@@ -3,6 +3,9 @@
  *
  * Copyright (C) 2015 Ondrej Oprala <ooprala@redhat.com>
  * Copyright (C) 2015 Karel Zak <ooprala@redhat.com>
+ * 
+ * 2025 Prasanna Paithankar <paithankarprasanna@gmail.com>
+ * - Added POSIX IPC support
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -773,12 +776,18 @@ static void do_posix_sem(const char *name, struct lsipc_control *ctl, struct lib
        struct posix_sem_data *semds, *p;
        char *arg = NULL;
 
-       scols_table_set_name(tb, "posix-semaphores");
-       if (posix_ipc_sem_get_info(name, &semds) < 1) {
-               if (name)
-                       warnx(_("name %s not found"), name);
+       int retval = posix_ipc_sem_get_info(name, &semds);
+       if (retval == -1)
+               return;
+
+       if (retval < 1) {
+               if (name != NULL)
+                       warnx(_("mqueue %s not found"), name);
                return;
        }
+
+       scols_table_set_name(tb, "posix-semaphores");
+
        for (p = semds; p->next != NULL || name != NULL; p = p->next) {
                size_t n;
 
@@ -1001,6 +1010,7 @@ static void do_posix_msg(const char *name, struct lsipc_control *ctl, struct lib
                        warnx(_("mqueue %s not found"), name);
                return;
        }
+
        scols_table_set_name(tb, "posix-messages");
 
        for (p = msgds; p->next != NULL || name != NULL; p = p->next) {
@@ -1106,6 +1116,12 @@ static void do_msg_global(struct lsipc_control *ctl, struct libscols_table *tb)
        global_set_data(ctl, tb, "MSGMNB", _("Default max size of System V queue (bytes)"), 0, lim.msgmnb, 0, 1);
 }
 
+#ifndef HAVE_MQUEUE_H
+static void do_posix_msg_global(struct lsipc_control *ctl __attribute__((unused)), struct libscols_table *tb __attribute__((unused)))
+{
+       return;
+}
+#else
 static void do_posix_msg_global(struct lsipc_control *ctl, struct libscols_table *tb)
 {
        struct posix_msg_data *pmsgds;
@@ -1121,6 +1137,7 @@ static void do_posix_msg_global(struct lsipc_control *ctl, struct libscols_table
        global_set_data(ctl, tb, "MQUMAX", _("Max size of POSIX message (bytes)"), 0, lim.msgmax_posix, 0, 1);
        global_set_data(ctl, tb, "MQUMNB", _("Number of messages in POSIX message queue"), 0, lim.msgmnb_posix, 0, 0);
 }
+#endif
 
 
 static void do_shm(int id, struct lsipc_control *ctl, struct libscols_table *tb)
@@ -1300,7 +1317,11 @@ static void do_posix_shm(const char *name, struct lsipc_control *ctl, struct lib
        struct posix_shm_data *shmds, *p;
        char *arg = NULL;
 
-       if (posix_ipc_shm_get_info(name, &shmds) < 1) {
+       int retval = posix_ipc_shm_get_info(name, &shmds);
+       if (retval == -1)
+               return;
+
+       if (retval < 1) {
                if (name != NULL)
                        warnx(_("shm %s not found"), name);
                return;