From: Prasanna Paithankar Date: Mon, 10 Feb 2025 19:37:55 +0000 (+0530) Subject: Defined macros for POSIX IPC compilation and removed path buffer. X-Git-Tag: v2.42-start~54^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ff5e642a9202be0509133a7f45e24456753d6ae3;p=thirdparty%2Futil-linux.git Defined macros for POSIX IPC compilation and removed path buffer. Signed-off-by: Prasanna Paithankar --- diff --git a/configure.ac b/configure.ac index f834652a0..44a4495a7 100644 --- a/configure.ac +++ b/configure.ac @@ -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 \ diff --git a/meson.build b/meson.build index a54e1e94b..1e0f4722e 100644 --- a/meson.build +++ b/meson.build @@ -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 ') 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 diff --git a/sys-utils/ipcmk.c b/sys-utils/ipcmk.c index 5386d41e3..32c23f79f 100644 --- a/sys-utils/ipcmk.c +++ b/sys-utils/ipcmk.c @@ -3,6 +3,9 @@ * * Copyright (C) 2008 Hayden A. James (hayden.james@gmail.com) * Copyright (C) 2008 Karel Zak + * + * 2025 Prasanna Paithankar + * - 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 @@ -26,13 +29,22 @@ #include #include #include -#include #include -#include #include +#include + +#ifdef HAVE_MQUEUE_H +#include +#endif + +#ifdef HAVE_SEMAPHORE_H +#include +#endif + +#ifdef HAVE_SYS_MMAN_H #include #include -#include +#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) { diff --git a/sys-utils/ipcrm.c b/sys-utils/ipcrm.c index 083d450cc..11adf2f80 100644 --- a/sys-utils/ipcrm.c +++ b/sys-utils/ipcrm.c @@ -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) { diff --git a/sys-utils/ipcutils.c b/sys-utils/ipcutils.c index 3ec2a0383..df6d15dd6 100644 --- a/sys-utils/ipcutils.c +++ b/sys-utils/ipcutils.c @@ -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) { diff --git a/sys-utils/ipcutils.h b/sys-utils/ipcutils.h index 16284c103..348da98fa 100644 --- a/sys-utils/ipcutils.h +++ b/sys-utils/ipcutils.h @@ -18,8 +18,6 @@ #include #include #include -#include -#include #include #include #include @@ -27,6 +25,14 @@ #include #include +#ifdef HAVE_MQUEUE_H +#include +#endif + +#ifdef HAVE_SEMAPHORE_H +#include +#endif + /* * SHM_DEST and SHM_LOCKED are defined in kernel headers, but inside * #ifdef __KERNEL__ ... #endif diff --git a/sys-utils/lsipc.c b/sys-utils/lsipc.c index 2838bb7d8..c3cc63f51 100644 --- a/sys-utils/lsipc.c +++ b/sys-utils/lsipc.c @@ -3,6 +3,9 @@ * * Copyright (C) 2015 Ondrej Oprala * Copyright (C) 2015 Karel Zak + * + * 2025 Prasanna Paithankar + * - 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;