All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 00/14] some fixes, cleanup and portability issues
@ 2016-02-26  3:03 Ruediger Meier
  2016-02-26  3:03 ` [PATCH 01/14] logger: use SCM_CREDENTIALS on LINUX only Ruediger Meier
                   ` (9 more replies)
  0 siblings, 10 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:03 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

Some arbitrary mostly trivial issues.

This is github pull request #291
https://github.com/karelzak/util-linux/pull/291

Ruediger Meier (14):
  logger: use SCM_CREDENTIALS on LINUX only
  build-sys: add openat build conditional
  fdisk: fix warning, incompatible pointer types passing 'uint64_t *'
  libsmartcols: fix uninitialized variable
  misc: fix some includes
  newgrp: rename memset_s()
  build-sys: build_init should check for flock
  login-utils: minor utmp cleanup
  build-sys: disable login-utils if shadow.h or utmp.h is missing
  build-sys: add --disable-ipcrm --disable-ipcs
  build-sys: chrt requires a sched_set* function
  lib: provide mkostemp fallback function
  build-sys: remove duplicate cal sources
  lib: include strutils.h for mempcpy()

 configure.ac                       | 50 ++++++++++++++++++++++++++++++++------
 disk-utils/fdisk.c                 |  4 +--
 include/fileutils.h                |  4 +++
 lib/blkdev.c                       |  7 ------
 lib/fileutils.c                    | 27 ++++++++++++++++++++
 lib/mbsalign.c                     |  1 +
 libblkid/src/save.c                |  1 +
 libfdisk/src/gpt.c                 |  3 ++-
 libsmartcols/samples/Makemodule.am |  4 ++-
 libsmartcols/src/table_print.c     |  2 +-
 login-utils/last.c                 |  2 +-
 login-utils/login.c                |  1 -
 login-utils/newgrp.c               |  4 +--
 misc-utils/Makemodule.am           | 10 +-------
 misc-utils/logger.c                |  5 ++++
 misc-utils/test_uuidd.c            |  1 -
 sys-utils/ipcs.c                   |  1 -
 sys-utils/lsipc.c                  |  1 -
 sys-utils/rtcwake.c                |  1 -
 text-utils/pg.c                    |  1 -
 20 files changed, 92 insertions(+), 38 deletions(-)

-- 
1.8.4.5


^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH 01/14] logger: use SCM_CREDENTIALS on LINUX only
  2016-02-26  3:03 [PATCH 00/14] some fixes, cleanup and portability issues Ruediger Meier
@ 2016-02-26  3:03 ` Ruediger Meier
  2016-02-26  3:03 ` [PATCH 02/14] build-sys: add openat build conditional Ruediger Meier
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:03 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

This is a build fix of FreeBSD and OSX. Basically we revert
the following commit for non-linux systems:
27a9eb53 "use --id as local socket credentials"

Note I could also compile it like this:

  #ifdef HAVE_SYS_UCRED_H
  # define _WANT_UCRED
  # include <sys/param.h>
  # include <sys/ucred.h>
  # define SCM_CREDENTIALS SCM_CREDS
  #endif

  [...]
  #ifdef _linux_
  cred->pid = ctl->pid;
  #endif
  [...]

... but don't know how to test whether it does what it
should.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 misc-utils/logger.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 4201e43..d9aee0e 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -446,12 +446,14 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
 
 	if (!ctl->noact) {
 		struct msghdr message = { 0 };
+#ifdef SCM_CREDENTIALS
 		struct cmsghdr *cmhp;
 		struct ucred *cred;
 		union {
 			struct cmsghdr cmh;
 			char   control[CMSG_SPACE(sizeof(struct ucred))];
 		} cbuf;
+#endif
 
 		/* 4) add extra \n to make sure message is terminated */
 		if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count)
@@ -460,6 +462,7 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
 		message.msg_iov = iov;
 		message.msg_iovlen = iovlen;
 
+#ifdef SCM_CREDENTIALS
 		/* syslog/journald may follow local socket credentials rather
 		 * than in the message PID. If we use --id as root than we can
 		 * force kernel to accept another valid PID than the real logger(1)
@@ -479,6 +482,7 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
 
 			cred->pid = ctl->pid;
 		}
+#endif
 
 		if (sendmsg(ctl->fd, &message, 0) < 0)
 			warn(_("send message failed"));
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 02/14] build-sys: add openat build conditional
  2016-02-26  3:03 [PATCH 00/14] some fixes, cleanup and portability issues Ruediger Meier
  2016-02-26  3:03 ` [PATCH 01/14] logger: use SCM_CREDENTIALS on LINUX only Ruediger Meier
@ 2016-02-26  3:03 ` Ruediger Meier
  2016-02-26  3:03 ` [PATCH 03/14] fdisk: fix warning, incompatible pointer types passing 'uint64_t *' Ruediger Meier
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:03 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

Using this conditional for test progs only should be ok.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 configure.ac                       | 2 ++
 libsmartcols/samples/Makemodule.am | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index 7b6a9e7..b8b0074 100644
--- a/configure.ac
+++ b/configure.ac
@@ -397,6 +397,8 @@ AC_CHECK_FUNCS([inotify_init1], [have_inotify_init1=yes])
 AC_CHECK_FUNCS([open_memstream], [have_open_memstream=yes],[have_open_memstream=no])
 AC_CHECK_FUNCS([reboot], [have_reboot=yes],[have_reboot=no])
 
+AM_CONDITIONAL([HAVE_OPENAT], [test "x$have_openat" = xyes])
+
 dnl lib/mononotic.c may require -lrt
 AC_CHECK_FUNCS([clock_gettime], [],
 	[AC_CHECK_LIB([rt], [clock_gettime], [REALTIME_LIBS="-lrt"])]
diff --git a/libsmartcols/samples/Makemodule.am b/libsmartcols/samples/Makemodule.am
index be74fc3..0a2d892 100644
--- a/libsmartcols/samples/Makemodule.am
+++ b/libsmartcols/samples/Makemodule.am
@@ -1,6 +1,5 @@
 
 check_PROGRAMS += \
-	sample-scols-tree \
 	sample-scols-title \
 	sample-scols-wrap \
 	sample-scols-continuous
@@ -9,9 +8,12 @@ sample_scols_cflags = $(AM_CFLAGS) $(NO_UNUSED_WARN_CFLAGS) \
                       -I$(ul_libsmartcols_incdir)
 sample_scols_ldadd = $(LDADD) libsmartcols.la
 
+if HAVE_OPENAT
+check_PROGRAMS += sample-scols-tree
 sample_scols_tree_SOURCES = libsmartcols/samples/tree.c
 sample_scols_tree_LDADD = $(sample_scols_ldadd) libcommon.la
 sample_scols_tree_CFLAGS = $(sample_scols_cflags)
+endif
 
 sample_scols_title_SOURCES = libsmartcols/samples/title.c
 sample_scols_title_LDADD = $(sample_scols_ldadd)
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 03/14] fdisk: fix warning, incompatible pointer types passing 'uint64_t *'
  2016-02-26  3:03 [PATCH 00/14] some fixes, cleanup and portability issues Ruediger Meier
  2016-02-26  3:03 ` [PATCH 01/14] logger: use SCM_CREDENTIALS on LINUX only Ruediger Meier
  2016-02-26  3:03 ` [PATCH 02/14] build-sys: add openat build conditional Ruediger Meier
@ 2016-02-26  3:03 ` Ruediger Meier
  2016-02-26  3:03 ` [PATCH 04/14] libsmartcols: fix uninitialized variable Ruediger Meier
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:03 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 disk-utils/fdisk.c | 4 ++--
 libfdisk/src/gpt.c | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c
index 0b22983..2f95fe8 100644
--- a/disk-utils/fdisk.c
+++ b/disk-utils/fdisk.c
@@ -276,7 +276,7 @@ static int ask_offset(struct fdisk_context *cxt,
 		snprintf(prompt, sizeof(prompt), _("%s (%ju-%ju): "), q, low, high);
 
 	do {
-		uint64_t num = 0;
+		uintmax_t num = 0;
 		char sig = 0, *p;
 		int pwr = 0;
 
@@ -313,7 +313,7 @@ static int ask_offset(struct fdisk_context *cxt,
 		if (num >= low && num <= high) {
 			if (sig && pwr)
 				fdisk_ask_number_set_relative(ask, 1);
-			return fdisk_ask_number_set_result(ask, num);
+			return fdisk_ask_number_set_result(ask, (uint64_t)num);
 		}
 		fdisk_warnx(cxt, _("Value out of range."));
 	} while (1);
diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c
index f795b35..cd88df0 100644
--- a/libfdisk/src/gpt.c
+++ b/libfdisk/src/gpt.c
@@ -2564,7 +2564,8 @@ static int gpt_toggle_partition_flag(
 		unsigned long flag)
 {
 	struct fdisk_gpt_label *gpt;
-	uint64_t attrs, tmp;
+	uint64_t attrs;
+	uintmax_t tmp;
 	char *bits;
 	const char *name = NULL;
 	int bit = -1, rc;
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 04/14] libsmartcols: fix uninitialized variable
  2016-02-26  3:03 [PATCH 00/14] some fixes, cleanup and portability issues Ruediger Meier
                   ` (2 preceding siblings ...)
  2016-02-26  3:03 ` [PATCH 03/14] fdisk: fix warning, incompatible pointer types passing 'uint64_t *' Ruediger Meier
@ 2016-02-26  3:03 ` Ruediger Meier
  2016-02-26  3:03 ` [PATCH 05/14] misc: fix some includes Ruediger Meier
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:03 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

This got broken in 2a750b4c.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 libsmartcols/src/table_print.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c
index d1dcb65..510386e 100644
--- a/libsmartcols/src/table_print.c
+++ b/libsmartcols/src/table_print.c
@@ -1281,7 +1281,7 @@ static int initialize_printting(struct libscols_table *tb, struct libscols_buffe
 
 		scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
 
-		while (rc == 0 && scols_table_next_column(tb, &itr, &cl) == 0) {
+		while (scols_table_next_column(tb, &itr, &cl) == 0) {
 			if (scols_column_is_hidden(cl))
 				continue;
 			extra_bufsz += strlen(scols_cell_get_data(&cl->header));	/* data */
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 05/14] misc: fix some includes
  2016-02-26  3:03 [PATCH 00/14] some fixes, cleanup and portability issues Ruediger Meier
                   ` (3 preceding siblings ...)
  2016-02-26  3:03 ` [PATCH 04/14] libsmartcols: fix uninitialized variable Ruediger Meier
@ 2016-02-26  3:03 ` Ruediger Meier
  2016-02-26 11:37   ` Ruediger Meier
  2016-02-26  3:03 ` [PATCH 06/14] newgrp: rename memset_s() Ruediger Meier
                   ` (4 subsequent siblings)
  9 siblings, 1 reply; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:03 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

features.h:  any glibc header includes this already
libgen.h:    was unused there
sys/uio.h:   for writev(3p)
sys/queue.h  seems like it was never used

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 configure.ac            | 1 -
 lib/blkdev.c            | 7 -------
 misc-utils/logger.c     | 1 +
 misc-utils/test_uuidd.c | 1 -
 sys-utils/ipcs.c        | 1 -
 sys-utils/lsipc.c       | 1 -
 sys-utils/rtcwake.c     | 1 -
 text-utils/pg.c         | 1 -
 8 files changed, 1 insertion(+), 13 deletions(-)

diff --git a/configure.ac b/configure.ac
index b8b0074..e3b0bf5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -242,7 +242,6 @@ AC_CHECK_HEADERS([ \
 	sys/mount.h \
 	sys/param.h \
 	sys/prctl.h \
-	sys/queue.h \
 	sys/resource.h \
 	sys/socket.h \
 	sys/sockio.h \
diff --git a/lib/blkdev.c b/lib/blkdev.c
index 403e9e4..ddd6e9b 100644
--- a/lib/blkdev.c
+++ b/lib/blkdev.c
@@ -18,13 +18,6 @@
 #include <sys/disklabel.h>
 #endif
 
-#ifdef HAVE_SYS_DISK_H
-# ifdef HAVE_SYS_QUEUE_H
-#  include <sys/queue.h>	/* for LIST_HEAD */
-# endif
-# include <sys/disk.h>
-#endif
-
 #ifndef EBADFD
 # define EBADFD 77		/* File descriptor in bad state */
 #endif
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index d9aee0e..896d1b8 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -53,6 +53,7 @@
 #include <pwd.h>
 #include <sys/types.h>
 #include <signal.h>
+#include <sys/uio.h>
 
 #include "all-io.h"
 #include "c.h"
diff --git a/misc-utils/test_uuidd.c b/misc-utils/test_uuidd.c
index 1262e35..78399b9 100644
--- a/misc-utils/test_uuidd.c
+++ b/misc-utils/test_uuidd.c
@@ -24,7 +24,6 @@
  *	make uuidd uuidgen localstatedir=/var
  */
 #include <error.h>
-#include <libgen.h>
 #include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/sys-utils/ipcs.c b/sys-utils/ipcs.c
index 5877c46..0ebcc4c 100644
--- a/sys-utils/ipcs.c
+++ b/sys-utils/ipcs.c
@@ -16,7 +16,6 @@
  */
 
 #include <errno.h>
-#include <features.h>
 #include <getopt.h>
 
 #include "c.h"
diff --git a/sys-utils/lsipc.c b/sys-utils/lsipc.c
index 9e34e6b..938728a 100644
--- a/sys-utils/lsipc.c
+++ b/sys-utils/lsipc.c
@@ -25,7 +25,6 @@
  */
 
 #include <errno.h>
-#include <features.h>
 #include <getopt.h>
 #include <sys/time.h>
 #include <unistd.h>
diff --git a/sys-utils/rtcwake.c b/sys-utils/rtcwake.c
index 842ea50..7c748dc 100644
--- a/sys-utils/rtcwake.c
+++ b/sys-utils/rtcwake.c
@@ -22,7 +22,6 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
-#include <libgen.h>
 #include <linux/rtc.h>
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/text-utils/pg.c b/text-utils/pg.c
index 998010a..9ef00f0 100644
--- a/text-utils/pg.c
+++ b/text-utils/pg.c
@@ -58,7 +58,6 @@
 #include <unistd.h>
 #include <signal.h>
 #include <setjmp.h>
-#include <libgen.h>
 
 #ifdef HAVE_NCURSES_H
 # include <ncurses.h>
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 06/14] newgrp: rename memset_s()
  2016-02-26  3:03 [PATCH 00/14] some fixes, cleanup and portability issues Ruediger Meier
                   ` (4 preceding siblings ...)
  2016-02-26  3:03 ` [PATCH 05/14] misc: fix some includes Ruediger Meier
@ 2016-02-26  3:03 ` Ruediger Meier
  2016-02-26 12:29   ` Yuriy M. Kaminskiy
  2016-02-26  3:03 ` [PATCH 07/14] build-sys: build_init should check for flock Ruediger Meier
                   ` (3 subsequent siblings)
  9 siblings, 1 reply; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:03 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

It's already defined in OSX standard c library.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 login-utils/newgrp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/login-utils/newgrp.c b/login-utils/newgrp.c
index 141b41b..56639d1 100644
--- a/login-utils/newgrp.c
+++ b/login-utils/newgrp.c
@@ -62,7 +62,7 @@ static char *xgetpass(FILE *input, const char *prompt)
 
 /* Ensure memory is set to value c without compiler optimization getting
  * into way that could happen with memset(3). */
-static int memset_s(void *v, size_t sz, const int c)
+static int xmemset_s(void *v, size_t sz, const int c)
 {
 	volatile unsigned char *p = v;
 
@@ -148,7 +148,7 @@ static int allow_setgid(const struct passwd *pe, const struct group *ge)
 	if (pwd && *pwd && (xpwd = xgetpass(stdin, _("Password: ")))) {
 		char *cbuf = crypt(xpwd, pwd);
 
-		memset_s(xpwd, strlen(xpwd), 0);
+		xmemset_s(xpwd, strlen(xpwd), 0);
 		free(xpwd);
 		if (!cbuf)
 			warn(_("crypt failed"));
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 07/14] build-sys: build_init should check for flock
  2016-02-26  3:03 [PATCH 00/14] some fixes, cleanup and portability issues Ruediger Meier
                   ` (5 preceding siblings ...)
  2016-02-26  3:03 ` [PATCH 06/14] newgrp: rename memset_s() Ruediger Meier
@ 2016-02-26  3:03 ` Ruediger Meier
  2016-02-26  3:03 ` [PATCH 08/14] login-utils: minor utmp cleanup Ruediger Meier
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:03 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

To let a plain ./configure work on systems without create_timer().
see 254743e4

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 configure.ac | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index e3b0bf5..80123d9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1408,7 +1408,7 @@ UL_REQUIRES_SYSCALL_CHECK([pivot_root], [UL_CHECK_SYSCALL([pivot_root])])
 AM_CONDITIONAL([BUILD_PIVOT_ROOT], [test "x$build_pivot_root" = xyes])
 
 
-UL_BUILD_INIT([flock], [yes])
+UL_BUILD_INIT([flock], [check])
 UL_REQUIRES_HAVE([flock], [timer], [timer_create function])
 AM_CONDITIONAL([BUILD_FLOCK], [test "x$build_flock" = xyes])
 
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 08/14] login-utils: minor utmp cleanup
  2016-02-26  3:03 [PATCH 00/14] some fixes, cleanup and portability issues Ruediger Meier
                   ` (6 preceding siblings ...)
  2016-02-26  3:03 ` [PATCH 07/14] build-sys: build_init should check for flock Ruediger Meier
@ 2016-02-26  3:03 ` Ruediger Meier
  2016-02-26  3:03 ` [PATCH 09/14] build-sys: disable login-utils if shadow.h or utmp.h is missing Ruediger Meier
  2016-02-26  3:08 ` [PATCH 10/14] build-sys: add --disable-ipcrm --disable-ipcs Ruediger Meier
  9 siblings, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:03 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

- consistently use ut->ut_user instead of ut->ut_name
- don't include obsolete lastlog.h BSD header

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 login-utils/last.c  | 2 +-
 login-utils/login.c | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/login-utils/last.c b/login-utils/last.c
index dbfa8ae..38b92ef 100644
--- a/login-utils/last.c
+++ b/login-utils/last.c
@@ -586,7 +586,7 @@ static int is_phantom(const struct last_control *ctl, struct utmp *ut)
 
 	if (ut->UL_UT_TIME < ctl->boot_time.tv_sec)
 		return 1;
-	pw = getpwnam(ut->ut_name);
+	pw = getpwnam(ut->ut_user);
 	if (!pw)
 		return 1;
 	sprintf(path, "/proc/%u/loginuid", ut->ut_pid);
diff --git a/login-utils/login.c b/login-utils/login.c
index e1f11eb..c5ce188 100644
--- a/login-utils/login.c
+++ b/login-utils/login.c
@@ -55,7 +55,6 @@
 # include <linux/major.h>
 #endif
 #include <netdb.h>
-#include <lastlog.h>
 #include <security/pam_appl.h>
 #ifdef HAVE_SECURITY_PAM_MISC_H
 # include <security/pam_misc.h>
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 09/14] build-sys: disable login-utils if shadow.h or utmp.h is missing
  2016-02-26  3:03 [PATCH 00/14] some fixes, cleanup and portability issues Ruediger Meier
                   ` (7 preceding siblings ...)
  2016-02-26  3:03 ` [PATCH 08/14] login-utils: minor utmp cleanup Ruediger Meier
@ 2016-02-26  3:03 ` Ruediger Meier
  2016-02-26  3:08 ` [PATCH 10/14] build-sys: add --disable-ipcrm --disable-ipcs Ruediger Meier
  9 siblings, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:03 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

Actually we could have also used UL_REQUIRES_LINUX because our
utmp usage and the shadow.h header is unlikely to be portable.
However only requiring these headers may help others who are
curious what needs to be done to port something.

Note, we could easily make the utmp stuff more portable by using
utmpx which is POSIX standard and on LINUX (glibc) basically just
renaming work. See getutxent(3).

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 configure.ac | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/configure.ac b/configure.ac
index 80123d9..2f1a687 100644
--- a/configure.ac
+++ b/configure.ac
@@ -256,6 +256,8 @@ AC_CHECK_HEADERS([ \
 	sys/ucred.h \
 	sys/un.h \
 	unistd.h \
+	shadow.h \
+	utmp.h \
 ])
 
 AC_CHECK_HEADERS([security/pam_misc.h],
@@ -285,6 +287,8 @@ have_linux_watchdog_h=$ac_cv_header_linux_watchdog_h
 have_security_pam_appl_h=$ac_cv_header_security_pam_appl_h
 have_security_pam_misc_h=$ac_cv_header_security_pam_misc_h
 have_security_openpam_h=$ac_cv_header_security_openpam_h
+have_shadow_h=$ac_cv_header_shadow_h
+have_utmp_h=$ac_cv_header_utmp_h
 
 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
 #include <time.h>
@@ -1204,9 +1208,10 @@ AM_CONDITIONAL([BUILD_EJECT], [test "x$build_eject" = xyes])
 
 AC_ARG_ENABLE([agetty],
   AS_HELP_STRING([--disable-agetty], [do not build agetty]),
-  [], [UL_DEFAULT_ENABLE([agetty], [yes])]
+  [], [UL_DEFAULT_ENABLE([agetty], [check])]
 )
 UL_BUILD_INIT([agetty])
+UL_REQUIRES_HAVE([agetty], [utmp_h], [utmp.h header])
 AM_CONDITIONAL([BUILD_AGETTY], [test "x$build_agetty" = xyes])
 AS_IF([test "x$have_futimens" = xyes -a "x$have_inotify_init1" = xyes ], [
   AC_DEFINE([AGETTY_RELOAD], [1], [Enable agetty --reload feature])
@@ -1307,6 +1312,8 @@ AM_CONDITIONAL([BUILD_LSCPU], [test "x$build_lscpu" = xyes])
 
 UL_BUILD_INIT([lslogins], [check])
 UL_REQUIRES_BUILD([lslogins], [libsmartcols])
+UL_REQUIRES_HAVE([lslogins], [shadow_h], [shadow.h header])
+UL_REQUIRES_HAVE([lslogins], [utmp_h], [utmp.h header])
 AM_CONDITIONAL([BUILD_LSLOGINS], [test "x$build_lslogins" = xyes])
 
 
@@ -1526,14 +1533,16 @@ AC_ARG_ENABLE([last],
   [], [UL_DEFAULT_ENABLE([last], [check])]
 )
 UL_BUILD_INIT([last])
+UL_REQUIRES_HAVE([last], [utmp_h], [utmp.h header])
 AM_CONDITIONAL([BUILD_LAST], [test "x$build_last" = xyes])
 
 
 AC_ARG_ENABLE([utmpdump],
   AS_HELP_STRING([--disable-utmpdump], [do not build utmpdump]),
-  [], [UL_DEFAULT_ENABLE([utmpdump], [yes])]
+  [], [UL_DEFAULT_ENABLE([utmpdump], [check])]
 )
 UL_BUILD_INIT([utmpdump])
+UL_REQUIRES_HAVE([utmpdump], [utmp_h], [utmp.h header])
 AM_CONDITIONAL([BUILD_UTMPDUMP], [test "x$build_utmpdump" = xyes])
 
 
@@ -1584,6 +1593,7 @@ AC_ARG_ENABLE([vipw],
   [], [UL_DEFAULT_ENABLE([vipw], [no])]
 )
 UL_BUILD_INIT([vipw])
+UL_REQUIRES_HAVE([vipw], [shadow_h], [shadow.h header])
 AM_CONDITIONAL([BUILD_VIPW], [test "x$build_vipw" = xyes])
 
 
@@ -1625,6 +1635,7 @@ AC_ARG_ENABLE([chfn-chsh],
   [], [UL_DEFAULT_ENABLE([chfn_chsh], [check])]
 )
 UL_BUILD_INIT([chfn_chsh])
+UL_REQUIRES_HAVE([chfn_chsh], [shadow_h], [shadow.h header])
 
 AS_IF([test "x$enable_chfn_chsh_password" = xyes -o "x$have_user" = xyes], [
   UL_REQUIRES_HAVE([chfn_chsh], [security_pam_appl_h], [PAM header file])
@@ -1650,6 +1661,7 @@ AC_ARG_ENABLE([login],
 UL_BUILD_INIT([login])
 UL_REQUIRES_HAVE([login], [security_pam_appl_h], [PAM header file])
 UL_REQUIRES_HAVE([login], [security_pam_misc_h, security_openpam_h], [PAM conversation functions])
+UL_REQUIRES_HAVE([login], [utmp_h], [utmp.h header])
 AM_CONDITIONAL([BUILD_LOGIN], [test "x$build_login" = xyes])
 
 AC_ARG_ENABLE([login-chown-vcs],
@@ -1681,9 +1693,10 @@ AM_CONDITIONAL([BUILD_NOLOGIN], [test "x$build_nologin" = xyes])
 
 AC_ARG_ENABLE([sulogin],
   AS_HELP_STRING([--disable-sulogin], [do not build sulogin]),
-  [], [UL_DEFAULT_ENABLE([sulogin], [yes])]
+  [], [UL_DEFAULT_ENABLE([sulogin], [check])]
 )
 UL_BUILD_INIT([sulogin])
+UL_REQUIRES_HAVE([sulogin], [shadow_h], [shadow.h header])
 AM_CONDITIONAL([BUILD_SULOGIN], [test "x$build_sulogin" = xyes])
 
 
@@ -1693,6 +1706,7 @@ AC_ARG_ENABLE([su],
 )
 UL_BUILD_INIT([su])
 UL_REQUIRES_HAVE([su], [security_pam_appl_h], [PAM header file])
+UL_REQUIRES_HAVE([su], [utmp_h], [utmp.h header])
 AM_CONDITIONAL([BUILD_SU], [test "x$build_su" = xyes])
 
 
@@ -1702,6 +1716,7 @@ AC_ARG_ENABLE([runuser],
 )
 UL_BUILD_INIT([runuser])
 UL_REQUIRES_HAVE([runuser], [security_pam_appl_h], [PAM header file])
+UL_REQUIRES_HAVE([runuser], [utmp_h], [utmp.h header])
 AM_CONDITIONAL([BUILD_RUNUSER], [test "x$build_runuser" = xyes])
 
 
@@ -1796,9 +1811,10 @@ AS_IF([test "x$build_chrt" = xyes], [
 
 AC_ARG_ENABLE([wall],
   AS_HELP_STRING([--disable-wall], [do not build wall]),
-  [], [UL_DEFAULT_ENABLE([wall], [yes])]
+  [], [UL_DEFAULT_ENABLE([wall], [check])]
 )
 UL_BUILD_INIT([wall])
+UL_REQUIRES_HAVE([wall], [utmp_h], [utmp.h header])
 AM_CONDITIONAL([BUILD_WALL], [test "x$build_wall" = xyes])
 
 
@@ -1807,6 +1823,7 @@ AC_ARG_ENABLE([write],
   [], [UL_DEFAULT_ENABLE([write], [no])]
 )
 UL_BUILD_INIT([write])
+UL_REQUIRES_HAVE([write], [utmp_h], [utmp.h header])
 AM_CONDITIONAL([BUILD_WRITE], [test "x$build_write" = xyes])
 
 
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 10/14] build-sys: add --disable-ipcrm --disable-ipcs
  2016-02-26  3:03 [PATCH 00/14] some fixes, cleanup and portability issues Ruediger Meier
                   ` (8 preceding siblings ...)
  2016-02-26  3:03 ` [PATCH 09/14] build-sys: disable login-utils if shadow.h or utmp.h is missing Ruediger Meier
@ 2016-02-26  3:08 ` Ruediger Meier
  2016-02-26  3:08   ` [PATCH 11/14] build-sys: chrt requires a sched_set* function Ruediger Meier
                     ` (3 more replies)
  9 siblings, 4 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:08 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

ipcs's source history looks like some people are using it on BSD
but it won't build on most non-Linux systems. That's why it's nice
let "./configure --disable-ipcrm --disable-ipcs" work.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 configure.ac | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/configure.ac b/configure.ac
index 2f1a687..d6c0c05 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1422,10 +1422,18 @@ AM_CONDITIONAL([BUILD_FLOCK], [test "x$build_flock" = xyes])
 UL_BUILD_INIT([ipcmk], [yes])
 AM_CONDITIONAL([BUILD_IPCMK], [test "x$build_ipcmk" = xyes])
 
-UL_BUILD_INIT([ipcrm], [yes])
+AC_ARG_ENABLE([ipcrm],
+  AS_HELP_STRING([--disable-ipcrm], [do not build ipcrm]),
+  [], [UL_DEFAULT_ENABLE([ipcrm], [yes])]
+)
+UL_BUILD_INIT([ipcrm])
 AM_CONDITIONAL([BUILD_IPCRM], [test "x$build_ipcrm" = xyes])
 
-UL_BUILD_INIT([ipcs], [yes])
+AC_ARG_ENABLE([ipcs],
+  AS_HELP_STRING([--disable-ipcs], [do not build ipcs]),
+  [], [UL_DEFAULT_ENABLE([ipcs], [yes])]
+)
+UL_BUILD_INIT([ipcs])
 AM_CONDITIONAL([BUILD_IPCS], [test "x$build_ipcs" = xyes])
 
 UL_BUILD_INIT([lsipc], [check])
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 11/14] build-sys: chrt requires a sched_set* function
  2016-02-26  3:08 ` [PATCH 10/14] build-sys: add --disable-ipcrm --disable-ipcs Ruediger Meier
@ 2016-02-26  3:08   ` Ruediger Meier
  2016-02-26  3:08   ` [PATCH 12/14] lib: provide mkostemp fallback function Ruediger Meier
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:08 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 configure.ac | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/configure.ac b/configure.ac
index d6c0c05..727a875 100644
--- a/configure.ac
+++ b/configure.ac
@@ -379,6 +379,7 @@ AC_CHECK_FUNCS([ \
 	setresgid \
 	setresuid \
 	sched_setattr \
+	sched_setscheduler \
 	sigqueue \
 	srandom \
 	strnchr \
@@ -1808,8 +1809,14 @@ UL_REQUIRES_SYSCALL_CHECK([taskset],
 	[sched_getaffinity])
 AM_CONDITIONAL([BUILD_TASKSET], [test "x$build_taskset" = xyes])
 
+
+have_schedsetter=no
+AS_IF([test "x$ac_cv_func_sched_setscheduler" = xyes], [have_schedsetter=yes],
+      [test "x$ac_cv_func_sched_setattr" = xyes], [have_schedsetter=yes])
+
 UL_BUILD_INIT([chrt], [check])
 UL_REQUIRES_BUILD([chrt], [schedutils])
+UL_REQUIRES_HAVE([chrt], [schedsetter], [sched_set functions])
 AM_CONDITIONAL([BUILD_CHRT], [test "x$build_chrt" = xyes])
 
 AS_IF([test "x$build_chrt" = xyes], [
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 12/14] lib: provide mkostemp fallback function
  2016-02-26  3:08 ` [PATCH 10/14] build-sys: add --disable-ipcrm --disable-ipcs Ruediger Meier
  2016-02-26  3:08   ` [PATCH 11/14] build-sys: chrt requires a sched_set* function Ruediger Meier
@ 2016-02-26  3:08   ` Ruediger Meier
  2016-02-26 12:51     ` Yuriy M. Kaminskiy
  2016-02-27 20:02     ` [PATCH v2 12/14] lib: provide fallback if mkostemp(3) missing Ruediger Meier
  2016-02-26  3:08   ` [PATCH 13/14] build-sys: remove duplicate cal sources Ruediger Meier
  2016-02-26  3:08   ` [PATCH 14/14] lib: include strutils.h for mempcpy() Ruediger Meier
  3 siblings, 2 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:08 UTC (permalink / raw)
  To: util-linux

From: Ruediger Meier <ruediger.meier@ga-group.nl>

It's missing on OSX.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 configure.ac        |  1 +
 include/fileutils.h |  4 ++++
 lib/fileutils.c     | 27 +++++++++++++++++++++++++++
 libblkid/src/save.c |  1 +
 4 files changed, 33 insertions(+)

diff --git a/configure.ac b/configure.ac
index 727a875..64a16ff 100644
--- a/configure.ac
+++ b/configure.ac
@@ -368,6 +368,7 @@ AC_CHECK_FUNCS([ \
 	llseek \
 	lseek64 \
 	mempcpy \
+	mkostemp \
 	nanosleep \
 	ntp_gettime \
 	personality \
diff --git a/include/fileutils.h b/include/fileutils.h
index ba8da7f..7c5594e 100644
--- a/include/fileutils.h
+++ b/include/fileutils.h
@@ -8,6 +8,10 @@
 
 #include "c.h"
 
+#ifndef HAVE_MKOSTEMP
+extern int mkostemp(char *template, int flags);
+#endif
+
 extern int xmkstemp(char **tmpname, const char *dir, const char *prefix);
 
 static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefix)
diff --git a/lib/fileutils.c b/lib/fileutils.c
index bf8e60a..05a8c02 100644
--- a/lib/fileutils.c
+++ b/lib/fileutils.c
@@ -13,6 +13,33 @@
 #include "fileutils.h"
 #include "pathnames.h"
 
+#ifndef HAVE_MKOSTEMP
+int mkostemp(char *template, int flags)
+{
+	int fd, old_flags, errno_save;
+
+	fd = mkstemp(template);
+	if (fd < 0)
+		return fd;
+
+	old_flags = fcntl(fd, F_GETFD);
+	if (old_flags < 0)
+		goto unwind;
+	if (fcntl(fd, F_SETFD, old_flags | flags) < 0)
+		goto unwind;
+
+	return fd;
+
+unwind:
+	errno_save = errno;
+	unlink(template);
+	close(fd);
+	errno = errno_save;
+
+	return -1;
+}
+#endif
+
 /* Create open temporary file in safe way.  Please notice that the
  * file permissions are -rw------- by default. */
 int xmkstemp(char **tmpname, const char *dir, const char *prefix)
diff --git a/libblkid/src/save.c b/libblkid/src/save.c
index 5e8bbee..b9f447a 100644
--- a/libblkid/src/save.c
+++ b/libblkid/src/save.c
@@ -23,6 +23,7 @@
 #endif
 
 #include "closestream.h"
+#include "fileutils.h"
 
 #include "blkidP.h"
 
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 13/14] build-sys: remove duplicate cal sources
  2016-02-26  3:08 ` [PATCH 10/14] build-sys: add --disable-ipcrm --disable-ipcs Ruediger Meier
  2016-02-26  3:08   ` [PATCH 11/14] build-sys: chrt requires a sched_set* function Ruediger Meier
  2016-02-26  3:08   ` [PATCH 12/14] lib: provide mkostemp fallback function Ruediger Meier
@ 2016-02-26  3:08   ` Ruediger Meier
  2016-02-26  3:08   ` [PATCH 14/14] lib: include strutils.h for mempcpy() Ruediger Meier
  3 siblings, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:08 UTC (permalink / raw)
  To: util-linux; +Cc: Sami Kerola

From: Ruediger Meier <ruediger.meier@ga-group.nl>

These extra sources should have been removed since cal is
linked against libcommon (see 7b353df2).

CC: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 misc-utils/Makemodule.am | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/misc-utils/Makemodule.am b/misc-utils/Makemodule.am
index aa54919..120aadf 100644
--- a/misc-utils/Makemodule.am
+++ b/misc-utils/Makemodule.am
@@ -1,15 +1,7 @@
 if BUILD_CAL
 usrbin_exec_PROGRAMS += cal
 dist_man_MANS += misc-utils/cal.1
-cal_SOURCES = \
-	misc-utils/cal.c \
-	lib/mbsalign.c \
-	lib/strutils.c
-
-if !HAVE_LANGINFO
-cal_SOURCES += lib/langinfo.c
-endif
-
+cal_SOURCES = misc-utils/cal.c
 cal_CFLAGS = $(AM_CFLAGS) $(NCURSES_CFLAGS)
 cal_LDADD = $(LDADD) libcommon.la libtcolors.la $(NCURSES_LIBS)
 endif # BUILD_CAL
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH 14/14] lib: include strutils.h for mempcpy()
  2016-02-26  3:08 ` [PATCH 10/14] build-sys: add --disable-ipcrm --disable-ipcs Ruediger Meier
                     ` (2 preceding siblings ...)
  2016-02-26  3:08   ` [PATCH 13/14] build-sys: remove duplicate cal sources Ruediger Meier
@ 2016-02-26  3:08   ` Ruediger Meier
  3 siblings, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26  3:08 UTC (permalink / raw)
  To: util-linux; +Cc: Daniel Trebbien

From: Ruediger Meier <ruediger.meier@ga-group.nl>

We have that mempcpy fallback since 2013 (02887b73) but forgot to
include it.

This fixes a segfault of cal(1) on FreeBSD and OSX.

Compiler warning was:
lib/mbsalign.c:468:14: warning: implicit declaration of function 'mempcpy' is invalid in C99 [-Wimplicit-function-declaration]
      dest = mempcpy (dest, str_to_print, min (n_used_bytes, space_left));
             ^
lib/mbsalign.c:468:12: warning: incompatible integer to pointer conversion assigning to 'char *' from 'int' [-Wint-conversion]
      dest = mempcpy (dest, str_to_print, min (n_used_bytes, space_left));

CC: Daniel Trebbien <dtrebbien@gmail.com>
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 lib/mbsalign.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lib/mbsalign.c b/lib/mbsalign.c
index 2a8de2f..f9babba 100644
--- a/lib/mbsalign.c
+++ b/lib/mbsalign.c
@@ -27,6 +27,7 @@
 
 #include "c.h"
 #include "mbsalign.h"
+#include "strutils.h"
 #include "widechar.h"
 
 #ifdef HAVE_WIDECHAR
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* Re: [PATCH 05/14] misc: fix some includes
  2016-02-26  3:03 ` [PATCH 05/14] misc: fix some includes Ruediger Meier
@ 2016-02-26 11:37   ` Ruediger Meier
  0 siblings, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26 11:37 UTC (permalink / raw)
  To: util-linux

On Friday 26 February 2016, Ruediger Meier wrote:
> From: Ruediger Meier <ruediger.meier@ga-group.nl>
>
> features.h:  any glibc header includes this already
> libgen.h:    was unused there
> sys/uio.h:   for writev(3p)
> sys/queue.h  seems like it was never used
>
> [...]
> diff --git a/lib/blkdev.c b/lib/blkdev.c
> index 403e9e4..ddd6e9b 100644
> --- a/lib/blkdev.c
> +++ b/lib/blkdev.c
> @@ -18,13 +18,6 @@
>  #include <sys/disklabel.h>
>  #endif
>
> -#ifdef HAVE_SYS_DISK_H
> -# ifdef HAVE_SYS_QUEUE_H
> -#  include <sys/queue.h>	/* for LIST_HEAD */
> -# endif
> -# include <sys/disk.h>
> -#endif
> -

It was a mistake to remove <sys/disk.h> too, already fixed on github. 

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 06/14] newgrp: rename memset_s()
  2016-02-26  3:03 ` [PATCH 06/14] newgrp: rename memset_s() Ruediger Meier
@ 2016-02-26 12:29   ` Yuriy M. Kaminskiy
  2016-02-26 12:47     ` Ruediger Meier
  0 siblings, 1 reply; 24+ messages in thread
From: Yuriy M. Kaminskiy @ 2016-02-26 12:29 UTC (permalink / raw)
  To: util-linux

Ruediger Meier <sweet_f_a@gmx.de> writes:

> From: Ruediger Meier <ruediger.meier@ga-group.nl>
>
> It's already defined in OSX standard c library.

Would not it be better to just use system-provided memset_s instead by
then? (And it is well possible glibc will provide memset_s in future
too).

> Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
> ---
>  login-utils/newgrp.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/login-utils/newgrp.c b/login-utils/newgrp.c
> index 141b41b..56639d1 100644
> --- a/login-utils/newgrp.c
> +++ b/login-utils/newgrp.c
> @@ -62,7 +62,7 @@ static char *xgetpass(FILE *input, const char *prompt)
>  
>  /* Ensure memory is set to value c without compiler optimization getting
>   * into way that could happen with memset(3). */
> -static int memset_s(void *v, size_t sz, const int c)
> +static int xmemset_s(void *v, size_t sz, const int c)
>  {
>  	volatile unsigned char *p = v;
>  
> @@ -148,7 +148,7 @@ static int allow_setgid(const struct passwd *pe, const struct group *ge)
>  	if (pwd && *pwd && (xpwd = xgetpass(stdin, _("Password: ")))) {
>  		char *cbuf = crypt(xpwd, pwd);
>  
> -		memset_s(xpwd, strlen(xpwd), 0);
> +		xmemset_s(xpwd, strlen(xpwd), 0);
>  		free(xpwd);
>  		if (!cbuf)
>  			warn(_("crypt failed"));


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 06/14] newgrp: rename memset_s()
  2016-02-26 12:29   ` Yuriy M. Kaminskiy
@ 2016-02-26 12:47     ` Ruediger Meier
  0 siblings, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26 12:47 UTC (permalink / raw)
  To: Yuriy M. Kaminskiy; +Cc: util-linux

On Friday 26 February 2016, Yuriy M. Kaminskiy wrote:
> Ruediger Meier <sweet_f_a@gmx.de> writes:
> > From: Ruediger Meier <ruediger.meier@ga-group.nl>
> >
> > It's already defined in OSX standard c library.
>
> Would not it be better to just use system-provided memset_s instead
> by then? (And it is well possible glibc will provide memset_s in
> future too).

I think we should do this when there are existing Linux systems with 
memset_s. For now I don't want to do bigger changes just to be portable 
because portability is not our primary goal.

BTW it's also well possible that the affected newgrp sources may 
disappear soon anyways, see Documentation/TODO.

> > Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
> > ---
> >  login-utils/newgrp.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/login-utils/newgrp.c b/login-utils/newgrp.c
> > index 141b41b..56639d1 100644
> > --- a/login-utils/newgrp.c
> > +++ b/login-utils/newgrp.c
> > @@ -62,7 +62,7 @@ static char *xgetpass(FILE *input, const char
> > *prompt)
> >
> >  /* Ensure memory is set to value c without compiler optimization
> > getting * into way that could happen with memset(3). */
> > -static int memset_s(void *v, size_t sz, const int c)
> > +static int xmemset_s(void *v, size_t sz, const int c)
> >  {
> >  	volatile unsigned char *p = v;
> >
> > @@ -148,7 +148,7 @@ static int allow_setgid(const struct passwd
> > *pe, const struct group *ge) if (pwd && *pwd && (xpwd =
> > xgetpass(stdin, _("Password: ")))) { char *cbuf = crypt(xpwd, pwd);
> >
> > -		memset_s(xpwd, strlen(xpwd), 0);
> > +		xmemset_s(xpwd, strlen(xpwd), 0);
> >  		free(xpwd);
> >  		if (!cbuf)
> >  			warn(_("crypt failed"));
>
> --
> To unsubscribe from this list: send the line "unsubscribe util-linux"
> in the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 12/14] lib: provide mkostemp fallback function
  2016-02-26  3:08   ` [PATCH 12/14] lib: provide mkostemp fallback function Ruediger Meier
@ 2016-02-26 12:51     ` Yuriy M. Kaminskiy
  2016-02-26 13:50       ` Ruediger Meier
  2016-02-26 14:51       ` [PATCH] Proper fallback for systems that lacks O_CLOEXEC (was: [PATCH 12/14] lib: provide mkostemp fallback function) Yuriy M. Kaminskiy
  2016-02-27 20:02     ` [PATCH v2 12/14] lib: provide fallback if mkostemp(3) missing Ruediger Meier
  1 sibling, 2 replies; 24+ messages in thread
From: Yuriy M. Kaminskiy @ 2016-02-26 12:51 UTC (permalink / raw)
  To: util-linux

Ruediger Meier <sweet_f_a@gmx.de> writes:

> From: Ruediger Meier <ruediger.meier@ga-group.nl>
>
> It's missing on OSX.
>
> Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
> ---
>  configure.ac        |  1 +
>  include/fileutils.h |  4 ++++
>  lib/fileutils.c     | 27 +++++++++++++++++++++++++++
>  libblkid/src/save.c |  1 +
>  4 files changed, 33 insertions(+)
>
> diff --git a/configure.ac b/configure.ac
> index 727a875..64a16ff 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -368,6 +368,7 @@ AC_CHECK_FUNCS([ \
>  	llseek \
>  	lseek64 \
>  	mempcpy \
> +	mkostemp \
>  	nanosleep \
>  	ntp_gettime \
>  	personality \
> diff --git a/include/fileutils.h b/include/fileutils.h
> index ba8da7f..7c5594e 100644
> --- a/include/fileutils.h
> +++ b/include/fileutils.h
> @@ -8,6 +8,10 @@
>  
>  #include "c.h"
>  
> +#ifndef HAVE_MKOSTEMP
> +extern int mkostemp(char *template, int flags);
> +#endif
> +
>  extern int xmkstemp(char **tmpname, const char *dir, const char *prefix);
>  
>  static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefix)
> diff --git a/lib/fileutils.c b/lib/fileutils.c
> index bf8e60a..05a8c02 100644
> --- a/lib/fileutils.c
> +++ b/lib/fileutils.c
> @@ -13,6 +13,33 @@
>  #include "fileutils.h"
>  #include "pathnames.h"
>  
> +#ifndef HAVE_MKOSTEMP
> +int mkostemp(char *template, int flags)
> +{
> +	int fd, old_flags, errno_save;
> +
> +	fd = mkstemp(template);
> +	if (fd < 0)
> +		return fd;
> +
> +	old_flags = fcntl(fd, F_GETFD);

This cannot be right. `flags` in `mkostemp` is **open** flags (such as `O_RDWR`,
`O_CREAT`, etc; most notable, `O_CLOEXEC`; however, if `O_CLOEXEC` is missing
on system, "c.h" defines it as 0, so it is silently ignored on those
systems, instead of being emulated; so, whenever it matters, callers
must call **both** `open(O_CLOEXEC)` and `fcntl(F_SETFD,FD_CLOEXEC)`]).

fcntl(F_SETFD) sets only FD_CLOEXEC (bit 0), which certainly different.

(Note that `F_SETFL` is not proper replacement either, as many `O_*` flags make
sense in open(), but not in `fcntl(F_SETFL)` [e.g. O_CREAT or O_EXCL];
and it is *not* correct to `or` old flags and new ones; e.g. if old
flags contains `O_RDWR`, and new flags is O_RDONLY, `old_flags | flags`
may be nonsense).

> +	if (old_flags < 0)
> +		goto unwind;
> +	if (fcntl(fd, F_SETFD, old_flags | flags) < 0)
> +		goto unwind;
> +
> +	return fd;
> +
> +unwind:
> +	errno_save = errno;
> +	unlink(template);
> +	close(fd);
> +	errno = errno_save;
> +
> +	return -1;
> +}
> +#endif
> +
>  /* Create open temporary file in safe way.  Please notice that the
>   * file permissions are -rw------- by default. */
>  int xmkstemp(char **tmpname, const char *dir, const char *prefix)
> diff --git a/libblkid/src/save.c b/libblkid/src/save.c
> index 5e8bbee..b9f447a 100644
> --- a/libblkid/src/save.c
> +++ b/libblkid/src/save.c
> @@ -23,6 +23,7 @@
>  #endif
>  
>  #include "closestream.h"
> +#include "fileutils.h"
>  
>  #include "blkidP.h"


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH 12/14] lib: provide mkostemp fallback function
  2016-02-26 12:51     ` Yuriy M. Kaminskiy
@ 2016-02-26 13:50       ` Ruediger Meier
  2016-02-26 14:51       ` [PATCH] Proper fallback for systems that lacks O_CLOEXEC (was: [PATCH 12/14] lib: provide mkostemp fallback function) Yuriy M. Kaminskiy
  1 sibling, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-26 13:50 UTC (permalink / raw)
  To: Yuriy M. Kaminskiy; +Cc: util-linux

On Friday 26 February 2016, Yuriy M. Kaminskiy wrote:
> Ruediger Meier <sweet_f_a@gmx.de> writes:
> > From: Ruediger Meier <ruediger.meier@ga-group.nl>
> >
> > It's missing on OSX.
> >
> > Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
> > ---
> >  configure.ac        |  1 +
> >  include/fileutils.h |  4 ++++
> >  lib/fileutils.c     | 27 +++++++++++++++++++++++++++
> >  libblkid/src/save.c |  1 +
> >  4 files changed, 33 insertions(+)
> >
> > diff --git a/configure.ac b/configure.ac
> > index 727a875..64a16ff 100644
> > --- a/configure.ac
> > +++ b/configure.ac
> > @@ -368,6 +368,7 @@ AC_CHECK_FUNCS([ \
> >  	llseek \
> >  	lseek64 \
> >  	mempcpy \
> > +	mkostemp \
> >  	nanosleep \
> >  	ntp_gettime \
> >  	personality \
> > diff --git a/include/fileutils.h b/include/fileutils.h
> > index ba8da7f..7c5594e 100644
> > --- a/include/fileutils.h
> > +++ b/include/fileutils.h
> > @@ -8,6 +8,10 @@
> >
> >  #include "c.h"
> >
> > +#ifndef HAVE_MKOSTEMP
> > +extern int mkostemp(char *template, int flags);
> > +#endif
> > +
> >  extern int xmkstemp(char **tmpname, const char *dir, const char
> > *prefix);
> >
> >  static inline FILE *xfmkstemp(char **tmpname, const char *dir,
> > const char *prefix) diff --git a/lib/fileutils.c b/lib/fileutils.c
> > index bf8e60a..05a8c02 100644
> > --- a/lib/fileutils.c
> > +++ b/lib/fileutils.c
> > @@ -13,6 +13,33 @@
> >  #include "fileutils.h"
> >  #include "pathnames.h"
> >
> > +#ifndef HAVE_MKOSTEMP
> > +int mkostemp(char *template, int flags)
> > +{
> > +	int fd, old_flags, errno_save;
> > +
> > +	fd = mkstemp(template);
> > +	if (fd < 0)
> > +		return fd;
> > +
> > +	old_flags = fcntl(fd, F_GETFD);
>
> This cannot be right. `flags` in `mkostemp` is **open** flags (such

Oops, this was really stupid.

> as `O_RDWR`, `O_CREAT`, etc; most notable, `O_CLOEXEC`; however, if
> `O_CLOEXEC` is missing on system, "c.h" defines it as 0, so it is
> silently ignored on those systems, instead of being emulated; so,
> whenever it matters, callers must call **both** `open(O_CLOEXEC)` and
> `fcntl(F_SETFD,FD_CLOEXEC)`]).

I see, but then defining O_CLOEXEC to 0 at all is the big mistake. We 
are using (ignoring) it in whole util-linux. To fix that we have to add 
fcntl(F_SETFD,FD_CLOEXEC) everywhere.

> fcntl(F_SETFD) sets only FD_CLOEXEC (bit 0), which certainly
> different.
>
> (Note that `F_SETFL` is not proper replacement either, as many `O_*`
> flags make sense in open(), but not in `fcntl(F_SETFL)` [e.g. O_CREAT
> or O_EXCL]; and it is *not* correct to `or` old flags and new ones;
> e.g. if old flags contains `O_RDWR`, and new flags is O_RDONLY,
> `old_flags | flags` may be nonsense).

Ok, to have it most simple I would now only implement a fallback for 
exactly this usage:
  mkostemp(localtmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);

like this

#if defined HAVE_MKOSTEMP && defined O_CLOEXEC
mkostemp(localtmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
#else
fd = mkstemp(template);
old_flags = fcntl(fd, F_GETFD);
fcntl(fd, F_SETFD, old_flags | FD_CLOEXEC)
#endif


> > +	if (old_flags < 0)
> > +		goto unwind;
> > +	if (fcntl(fd, F_SETFD, old_flags | flags) < 0)
> > +		goto unwind;
> > +
> > +	return fd;
> > +
> > +unwind:
> > +	errno_save = errno;
> > +	unlink(template);
> > +	close(fd);
> > +	errno = errno_save;
> > +
> > +	return -1;
> > +}
> > +#endif
> > +
> >  /* Create open temporary file in safe way.  Please notice that the
> >   * file permissions are -rw------- by default. */
> >  int xmkstemp(char **tmpname, const char *dir, const char *prefix)
> > diff --git a/libblkid/src/save.c b/libblkid/src/save.c
> > index 5e8bbee..b9f447a 100644
> > --- a/libblkid/src/save.c
> > +++ b/libblkid/src/save.c
> > @@ -23,6 +23,7 @@
> >  #endif
> >
> >  #include "closestream.h"
> > +#include "fileutils.h"
> >
> >  #include "blkidP.h"
>
> --
> To unsubscribe from this list: send the line "unsubscribe util-linux"
> in the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH] Proper fallback for systems that lacks O_CLOEXEC (was: [PATCH 12/14] lib: provide mkostemp fallback function)
  2016-02-26 12:51     ` Yuriy M. Kaminskiy
  2016-02-26 13:50       ` Ruediger Meier
@ 2016-02-26 14:51       ` Yuriy M. Kaminskiy
  2016-02-27 19:40         ` Rüdiger Meier
  2016-03-07 13:50         ` Karel Zak
  1 sibling, 2 replies; 24+ messages in thread
From: Yuriy M. Kaminskiy @ 2016-02-26 14:51 UTC (permalink / raw)
  To: util-linux

[-- Attachment #1: Type: text/plain, Size: 555 bytes --]

yumkam@gmail.com (Yuriy M. Kaminskiy)
writes:

> Ruediger Meier <sweet_f_a@gmx.de> writes:
>
>> From: Ruediger Meier <ruediger.meier@ga-group.nl>
>>
[...]
> `O_CREAT`, etc; most notable, `O_CLOEXEC`; however, if `O_CLOEXEC` is missing
> on system, "c.h" defines it as 0, so it is silently ignored on those
> systems, instead of being emulated; so, whenever it matters, callers
> must call **both** `open(O_CLOEXEC)` and `fcntl(F_SETFD,FD_CLOEXEC)`]).

... something like attached below. In some cases it is maybe
unnecessary, but better safe than sorry.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Proper-fallback-for-systems-that-lacks-O_CLOEXEC.patch --]
[-- Type: text/x-diff, Size: 13815 bytes --]

>From 510a63401fbc251600648c803d9867bb27a9ce64 Mon Sep 17 00:00:00 2001
From: "Yuriy M. Kaminskiy" <yumkam@gmail.com>
Date: Fri, 26 Feb 2016 17:34:16 +0300
Subject: [PATCH] Proper fallback for systems that lacks O_CLOEXEC

... and fixes one missing O_CLOEXEC in open() and two missing fcntl(F_SETFD)
after dup().

Signed-off-by: "Yuriy M. Kaminskiy" <yumkam@gmail.com>
---
Note: I intentionally ommited few cases when fd closed right away after open.

 disk-utils/fsck.c              | 1 +
 lib/blkdev.c                   | 1 +
 lib/fileutils.c                | 1 +
 lib/loopdev.c                  | 5 ++++-
 libblkid/src/evaluate.c        | 1 +
 libblkid/src/probe.c           | 2 ++
 libblkid/src/read.c            | 1 +
 libblkid/src/save.c            | 1 +
 libblkid/src/topology/md.c     | 1 +
 libblkid/src/verify.c          | 1 +
 libfdisk/src/context.c         | 1 +
 libmount/src/lock.c            | 2 ++
 libmount/src/monitor.c         | 1 +
 libmount/src/tab_parse.c       | 3 +++
 libmount/src/utils.c           | 1 +
 libsmartcols/samples/tree.c    | 1 +
 libuuid/src/gen_uuid.c         | 1 +
 login-utils/sulogin-consoles.c | 5 +++++
 misc-utils/blkid.c             | 1 +
 sys-utils/rtcwake.c            | 1 +
 sys-utils/wdctl.c              | 2 ++
 term-utils/agetty.c            | 3 +++
 22 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/disk-utils/fsck.c b/disk-utils/fsck.c
index b8a47a1..f10b08e 100644
--- a/disk-utils/fsck.c
+++ b/disk-utils/fsck.c
@@ -375,6 +375,7 @@ static void lock_disk(struct fsck_instance *inst)
 	if (inst->lock >= 0) {
 		int rc = -1;
 
+		if (O_CLOEXEC == 0) fcntl(inst->lock, F_SETFD, FD_CLOEXEC);
 		/* inform users that we're waiting on the lock */
 		if (verbose &&
 		    (rc = flock(inst->lock, LOCK_EX | LOCK_NB)) != 0 &&
diff --git a/lib/blkdev.c b/lib/blkdev.c
index a57b367..66a472e 100644
--- a/lib/blkdev.c
+++ b/lib/blkdev.c
@@ -378,6 +378,7 @@ main(int argc, char **argv)
 
 	if ((fd = open(argv[1], O_RDONLY|O_CLOEXEC)) < 0)
 		err(EXIT_FAILURE, "open %s failed", argv[1]);
+	if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 
 	if (blkdev_get_size(fd, &bytes) < 0)
 		err(EXIT_FAILURE, "blkdev_get_size() failed");
diff --git a/lib/fileutils.c b/lib/fileutils.c
index bf8e60a..897e3ce 100644
--- a/lib/fileutils.c
+++ b/lib/fileutils.c
@@ -35,6 +35,7 @@ int xmkstemp(char **tmpname, const char *dir, const char *prefix)
 	old_mode = umask(077);
 	fd = mkostemp(localtmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
 	umask(old_mode);
+	if (O_CLOEXEC == 0 && fd != -1) fcntl(fd, F_SETFD, FD_CLOEXEC);
 	if (fd == -1) {
 		free(localtmp);
 		localtmp = NULL;
diff --git a/lib/loopdev.c b/lib/loopdev.c
index 54c6200..dcee458 100644
--- a/lib/loopdev.c
+++ b/lib/loopdev.c
@@ -280,6 +280,8 @@ int loopcxt_get_fd(struct loopdev_cxt *lc)
 		lc->fd = open(lc->device, lc->mode | O_CLOEXEC);
 		DBG(CXT, ul_debugobj(lc, "open %s [%s]: %m", lc->device,
 				lc->flags & LOOPDEV_FL_RDWR ? "rw" : "ro"));
+		if (O_CLOEXEC == 0 && lc->fd != -1)
+			fcntl(lc->fd, F_SETFD, FD_CLOEXEC);
 	}
 	return lc->fd;
 }
@@ -1230,7 +1232,7 @@ int loopcxt_setup_device(struct loopdev_cxt *lc)
 
 	if ((file_fd = open(lc->filename, mode | O_CLOEXEC)) < 0) {
 		if (mode != O_RDONLY && (errno == EROFS || errno == EACCES))
-			file_fd = open(lc->filename, mode = O_RDONLY);
+			file_fd = open(lc->filename, (mode = O_RDONLY) | O_CLOEXEC);
 
 		if (file_fd < 0) {
 			DBG(SETUP, ul_debugobj(lc, "open backing file failed: %m"));
@@ -1238,6 +1240,7 @@ int loopcxt_setup_device(struct loopdev_cxt *lc)
 		}
 	}
 	DBG(SETUP, ul_debugobj(lc, "backing file open: OK"));
+	if (O_CLOEXEC == 0 && file_fd >= 0) fcntl(file_fd, F_SETFD, FD_CLOEXEC);
 
 	if (lc->fd != -1 && lc->mode != mode) {
 		DBG(SETUP, ul_debugobj(lc, "closing already open device (mode mismatch)"));
diff --git a/libblkid/src/evaluate.c b/libblkid/src/evaluate.c
index ffbe097..e3c2424 100644
--- a/libblkid/src/evaluate.c
+++ b/libblkid/src/evaluate.c
@@ -75,6 +75,7 @@ static int verify_tag(const char *devname, const char *name, const char *value)
 		errsv = errno;
 		goto done;
 	}
+	if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 	if (blkid_probe_set_device(pr, fd, 0, 0))
 		goto done;
 	rc = blkid_do_safeprobe(pr);
diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c
index 4efa2ba..e4c1102 100644
--- a/libblkid/src/probe.c
+++ b/libblkid/src/probe.c
@@ -221,6 +221,7 @@ blkid_probe blkid_new_probe_from_filename(const char *filename)
 	fd = open(filename, O_RDONLY|O_CLOEXEC);
 	if (fd < 0)
 		return NULL;
+	if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 
 	pr = blkid_new_probe();
 	if (!pr)
@@ -1156,6 +1157,7 @@ int blkid_do_probe(blkid_probe pr)
  *  <title>wipe all filesystems or raids from the device</title>
  *   <programlisting>
  *      fd = open(devname, O_RDWR|O_CLOEXEC);
+ *      if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
  *      blkid_probe_set_device(pr, fd, 0, 0);
  *
  *      blkid_probe_enable_superblocks(pr, 1);
diff --git a/libblkid/src/read.c b/libblkid/src/read.c
index 41e564f..c5403fd 100644
--- a/libblkid/src/read.c
+++ b/libblkid/src/read.c
@@ -404,6 +404,7 @@ void blkid_read_cache(blkid_cache cache)
 	 */
 	if ((fd = open(cache->bic_filename, O_RDONLY|O_CLOEXEC)) < 0)
 		return;
+	if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 	if (fstat(fd, &st) < 0)
 		goto errout;
 	if ((st.st_mtime == cache->bic_ftime) ||
diff --git a/libblkid/src/save.c b/libblkid/src/save.c
index 5e8bbee..d7955f3 100644
--- a/libblkid/src/save.c
+++ b/libblkid/src/save.c
@@ -135,6 +135,7 @@ int blkid_flush_cache(blkid_cache cache)
 			sprintf(tmp, "%s-XXXXXX", filename);
 			fd = mkostemp(tmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
 			if (fd >= 0) {
+				if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 				if (fchmod(fd, 0644) != 0)
 					DBG(SAVE, ul_debug("%s: fchmod failed", filename));
 				else if ((file = fdopen(fd, "w" UL_CLOEXECSTR)))
diff --git a/libblkid/src/topology/md.c b/libblkid/src/topology/md.c
index 5eba947..39b104a 100644
--- a/libblkid/src/topology/md.c
+++ b/libblkid/src/topology/md.c
@@ -102,6 +102,7 @@ static int probe_md_tp(blkid_probe pr,
 
                 if (fd == -1)
 			goto nothing;
+		if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 	}
 
 	memset(&md, 0, sizeof(md));
diff --git a/libblkid/src/verify.c b/libblkid/src/verify.c
index 06f6d53..bdb6551 100644
--- a/libblkid/src/verify.c
+++ b/libblkid/src/verify.c
@@ -133,6 +133,7 @@ blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
 					dev->bid_name));
 		goto open_err;
 	}
+	if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 
 	if (blkid_probe_set_device(cache->probe, fd, 0, 0)) {
 		/* failed to read the device */
diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c
index 1f6118c..b4c796e 100644
--- a/libfdisk/src/context.c
+++ b/libfdisk/src/context.c
@@ -541,6 +541,7 @@ int fdisk_assign_device(struct fdisk_context *cxt,
 	fd = open(fname, (readonly ? O_RDONLY : O_RDWR ) | O_CLOEXEC);
 	if (fd < 0)
 		return -errno;
+	if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 
 	cxt->readonly = readonly;
 	cxt->dev_fd = fd;
diff --git a/libmount/src/lock.c b/libmount/src/lock.c
index 0d94329..ca218eb 100644
--- a/libmount/src/lock.c
+++ b/libmount/src/lock.c
@@ -220,6 +220,7 @@ static int lock_simplelock(struct libmnt_lock *ml)
 		rc = -errno;
 		goto err;
 	}
+	if (O_CLOEXEC == 0) fcntl(ml->lockfile_fd, F_SETFD, FD_CLOEXEC);
 
 	while (flock(ml->lockfile_fd, LOCK_EX) < 0) {
 		int errsv;
@@ -449,6 +450,7 @@ static int lock_mtab(struct libmnt_lock *ml)
 				rc = -errsv;
 			goto failed;
 		}
+		if (O_CLOEXEC == 0) fcntl(ml->lockfile_fd, F_SETFD, FD_CLOEXEC);
 
 		flock.l_type = F_WRLCK;
 		flock.l_whence = SEEK_SET;
diff --git a/libmount/src/monitor.c b/libmount/src/monitor.c
index 9bc7ea3..b274c6c 100644
--- a/libmount/src/monitor.c
+++ b/libmount/src/monitor.c
@@ -458,6 +458,7 @@ static int kernel_monitor_get_fd(struct libmnt_monitor *mn,
 	me->fd = open(me->path, O_RDONLY|O_CLOEXEC);
 	if (me->fd < 0)
 		goto err;
+	if (O_CLOEXEC == 0) fcntl(me->fd, F_SETFD, FD_CLOEXEC);
 
 	return me->fd;
 err:
diff --git a/libmount/src/tab_parse.c b/libmount/src/tab_parse.c
index 4c47390..b3219cb 100644
--- a/libmount/src/tab_parse.c
+++ b/libmount/src/tab_parse.c
@@ -741,6 +741,7 @@ static int __mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname)
 	dd = open(dirname, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
 	if (dd < 0)
 	        return -errno;
+	if (O_CLOEXEC == 0) fcntl(dd, F_SETFD, FD_CLOEXEC);
 
 	n = scandirat(dd, ".", &namelist, mnt_table_parse_dir_filter, versionsort);
 	if (n <= 0) {
@@ -759,6 +760,7 @@ static int __mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname)
 
 		f = fopen_at(dd, ".", d->d_name, O_RDONLY|O_CLOEXEC, "r" UL_CLOEXECSTR);
 		if (f) {
+			if (O_CLOEXEC == 0) fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
 			mnt_table_parse_stream(tb, f, d->d_name);
 			fclose(f);
 		}
@@ -800,6 +802,7 @@ static int __mnt_table_parse_dir(struct libmnt_table *tb, const char *dirname)
 		f = fopen_at(dirfd(dir), _PATH_MNTTAB_DIR, d->d_name,
 				O_RDONLY|O_CLOEXEC, "r" UL_CLOEXECSTR);
 		if (f) {
+			if (O_CLOEXEC == 0) fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
 			mnt_table_parse_stream(tb, f, d->d_name);
 			fclose(f);
 		}
diff --git a/libmount/src/utils.c b/libmount/src/utils.c
index 39f6c85..98820fd 100644
--- a/libmount/src/utils.c
+++ b/libmount/src/utils.c
@@ -943,6 +943,7 @@ int mnt_open_uniq_filename(const char *filename, char **name)
 		fd = -errno;
 	umask(oldmode);
 
+	if (O_CLOEXEC == 0 && fd >= 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 	if (fd >= 0 && name)
 		*name = n;
 	else
diff --git a/libsmartcols/samples/tree.c b/libsmartcols/samples/tree.c
index 7f41f9e..1e262c6 100644
--- a/libsmartcols/samples/tree.c
+++ b/libsmartcols/samples/tree.c
@@ -94,6 +94,7 @@ static int add_line_from_stat(struct libscols_table *tb,
 		else
 			fd = open(name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC);
 		if (fd >= 0) {
+			if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 			rc = add_children(tb, ln, fd);
 			close(fd);
 		}
diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c
index 4d60997..c8c1e18 100644
--- a/libuuid/src/gen_uuid.c
+++ b/libuuid/src/gen_uuid.c
@@ -236,6 +236,7 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low,
 		state_fd = open(LIBUUID_CLOCK_FILE, O_RDWR|O_CREAT|O_CLOEXEC, 0660);
 		(void) umask(save_umask);
 		if (state_fd != -1) {
+			if (O_CLOEXEC == 0) fcntl(state_fd, F_SETFD, FD_CLOEXEC);
 			state_f = fdopen(state_fd, "r+" UL_CLOEXECSTR);
 			if (!state_f) {
 				close(state_fd);
diff --git a/login-utils/sulogin-consoles.c b/login-utils/sulogin-consoles.c
index 1b05b38..36c2f58 100644
--- a/login-utils/sulogin-consoles.c
+++ b/login-utils/sulogin-consoles.c
@@ -476,6 +476,7 @@ static int detect_consoles_from_cmdline(struct list_head *consoles)
 			continue;
 		}
 		free(name);
+		if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 #ifdef TIOCGDEV
 		if (ioctl (fd, TIOCGDEV, &devnum) < 0) {
 			close(fd);
@@ -537,6 +538,8 @@ static int detect_consoles_from_tiocgdev(struct list_head *consoles,
 
 	if (fd < 0)
 		goto done;
+	/* XXX dup() clears close-on-exec */
+	/*if (O_CLOEXEC == 0) */fcntl(fd, F_SETFD, FD_CLOEXEC);
 	if (ioctl (fd, TIOCGDEV, &devnum) < 0)
 		goto done;
 
@@ -620,6 +623,8 @@ int detect_consoles(const char *device, int fallback, struct list_head *consoles
 		close(fd);
 		goto fallback;
 #endif
+		/* XXX dup() clears close-on-exec */
+		/*if (O_CLOEXEC == 0) */fcntl(fd, F_SETFD, FD_CLOEXEC);
 		DBG(dbgprint("trying device/fallback file descriptor"));
 
 		if (fstat(fd, &st) < 0) {
diff --git a/misc-utils/blkid.c b/misc-utils/blkid.c
index 4ecbb5d..a4f05df 100644
--- a/misc-utils/blkid.c
+++ b/misc-utils/blkid.c
@@ -489,6 +489,7 @@ static int lowprobe_device(blkid_probe pr, const char *devname,
 		fprintf(stderr, "error: %s: %m\n", devname);
 		return BLKID_EXIT_NOTFOUND;
 	}
+	if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 	if (blkid_probe_set_device(pr, fd, offset, size))
 		goto done;
 
diff --git a/sys-utils/rtcwake.c b/sys-utils/rtcwake.c
index 842ea50..a89220c 100644
--- a/sys-utils/rtcwake.c
+++ b/sys-utils/rtcwake.c
@@ -374,6 +374,7 @@ static int open_dev_rtc(const char *devname)
 	fd = open(devpath, O_RDONLY | O_CLOEXEC);
 	if (fd < 0)
 		err(EXIT_FAILURE, _("%s: unable to find device"), devpath);
+	if (O_CLOEXEC == 0) fcntl(fd, F_SETFD, FD_CLOEXEC);
 	free(devpath);
 	return fd;
 }
diff --git a/sys-utils/wdctl.c b/sys-utils/wdctl.c
index 095d8ed..e09f031 100644
--- a/sys-utils/wdctl.c
+++ b/sys-utils/wdctl.c
@@ -313,6 +313,7 @@ static int set_watchdog(struct wdinfo *wd, int timeout)
 	sigprocmask(SIG_BLOCK, &sigs, &oldsigs);
 
 	fd = open(wd->device, O_WRONLY|O_CLOEXEC);
+	/* Static_assert(O_CLOEXEC != 0); */
 
 	if (fd < 0) {
 		if (errno == EBUSY)
@@ -369,6 +370,7 @@ static int read_watchdog(struct wdinfo *wd)
 	sigprocmask(SIG_BLOCK, &sigs, &oldsigs);
 
 	fd = open(wd->device, O_WRONLY|O_CLOEXEC);
+	/* Static_assert(O_CLOEXEC != 0); */
 
 	if (fd < 0) {
 		if (errno == EBUSY)
diff --git a/term-utils/agetty.c b/term-utils/agetty.c
index cd57c67..812875a 100644
--- a/term-utils/agetty.c
+++ b/term-utils/agetty.c
@@ -1623,6 +1623,8 @@ static int wait_for_term_input(int fd)
 					O_CREAT|O_CLOEXEC|O_RDONLY,
 					S_IRUSR|S_IWUSR);
 
+		/* Static_assert(O_CLOEXEC != 0); */
+
 		/* initialize reload trigger inotify stuff */
 		if (reload_fd >= 0) {
 			inotify_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
@@ -2623,6 +2625,7 @@ static void reload_agettys(void)
 #ifdef AGETTY_RELOAD
 	int fd = open(AGETTY_RELOAD_FILENAME, O_CREAT|O_CLOEXEC|O_WRONLY,
 					      S_IRUSR|S_IWUSR);
+	/* Static_assert(O_CLOEXEC != 0); */
 	if (fd < 0)
 		err(EXIT_FAILURE, _("cannot open %s"), AGETTY_RELOAD_FILENAME);
 
-- 
2.1.4


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* Re: [PATCH] Proper fallback for systems that lacks O_CLOEXEC (was: [PATCH 12/14] lib: provide mkostemp fallback function)
  2016-02-26 14:51       ` [PATCH] Proper fallback for systems that lacks O_CLOEXEC (was: [PATCH 12/14] lib: provide mkostemp fallback function) Yuriy M. Kaminskiy
@ 2016-02-27 19:40         ` Rüdiger Meier
  2016-03-07 13:50         ` Karel Zak
  1 sibling, 0 replies; 24+ messages in thread
From: Rüdiger Meier @ 2016-02-27 19:40 UTC (permalink / raw)
  To: Yuriy M. Kaminskiy; +Cc: util-linux

On Friday 26 February 2016 15:51:28 Yuriy M. Kaminskiy wrote:
> yumkam@gmail.com (Yuriy M. Kaminskiy)
>
> writes:
> > Ruediger Meier <sweet_f_a@gmx.de> writes:
> >> From: Ruediger Meier <ruediger.meier@ga-group.nl>
>
> [...]
>
> > `O_CREAT`, etc; most notable, `O_CLOEXEC`; however, if `O_CLOEXEC` is
> > missing on system, "c.h" defines it as 0, so it is silently ignored on
> > those systems, instead of being emulated; so, whenever it matters,
> > callers must call **both** `open(O_CLOEXEC)` and
> > `fcntl(F_SETFD,FD_CLOEXEC)`]).
>
> ... something like attached below. In some cases it is maybe
> unnecessary, but better safe than sorry.

I'm not sure whether we should add all these fcntl lines just for a few old 
non-Linux systems. That's probably why Karel simply added quick and dirty 
this unsafe fallback defining O_CLOEXEC to 0.

We are using this fallback already since 2011 and and now 2016 we have even 
less systems where O_CLOEXEC is not available. BTW we are using mkostemp 
(which is a real showstopper for the build!) since 3 years and nobody 
complained. I assume any system with mkostemp will also have O_CLOEXEC.

I'm going to add another hopefully correct patch for case mkostemp only.

cu,
Rudi

^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH v2 12/14] lib: provide fallback if mkostemp(3) missing
  2016-02-26  3:08   ` [PATCH 12/14] lib: provide mkostemp fallback function Ruediger Meier
  2016-02-26 12:51     ` Yuriy M. Kaminskiy
@ 2016-02-27 20:02     ` Ruediger Meier
  1 sibling, 0 replies; 24+ messages in thread
From: Ruediger Meier @ 2016-02-27 20:02 UTC (permalink / raw)
  To: util-linux; +Cc: Yuriy M. Kaminskiy

From: Ruediger Meier <ruediger.meier@ga-group.nl>

It's missing on OSX.

CC: Yuriy M. Kaminskiy <yumkam@gmail.com>
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
 configure.ac        |  1 +
 include/fileutils.h |  2 ++
 lib/fileutils.c     | 31 ++++++++++++++++++++++++++++++-
 libblkid/src/save.c |  3 ++-
 4 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/configure.ac b/configure.ac
index 727a875..64a16ff 100644
--- a/configure.ac
+++ b/configure.ac
@@ -368,6 +368,7 @@ AC_CHECK_FUNCS([ \
 	llseek \
 	lseek64 \
 	mempcpy \
+	mkostemp \
 	nanosleep \
 	ntp_gettime \
 	personality \
diff --git a/include/fileutils.h b/include/fileutils.h
index ba8da7f..79dd012 100644
--- a/include/fileutils.h
+++ b/include/fileutils.h
@@ -8,6 +8,8 @@
 
 #include "c.h"
 
+extern int mkstemp_cloexec(char *template);
+
 extern int xmkstemp(char **tmpname, const char *dir, const char *prefix);
 
 static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefix)
diff --git a/lib/fileutils.c b/lib/fileutils.c
index bf8e60a..15cc925 100644
--- a/lib/fileutils.c
+++ b/lib/fileutils.c
@@ -13,6 +13,35 @@
 #include "fileutils.h"
 #include "pathnames.h"
 
+int mkstemp_cloexec(char *template)
+{
+#ifdef HAVE_MKOSTEMP
+	return mkostemp(template, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
+#else
+	int fd, old_flags, errno_save;
+
+	fd = mkstemp(template);
+	if (fd < 0)
+		return fd;
+
+	old_flags = fcntl(fd, F_GETFD, 0);
+	if (old_flags < 0)
+		goto unwind;
+	if (fcntl(fd, F_SETFD, old_flags | O_CLOEXEC) < 0)
+		goto unwind;
+
+	return fd;
+
+unwind:
+	errno_save = errno;
+	unlink(template);
+	close(fd);
+	errno = errno_save;
+
+	return -1;
+#endif
+}
+
 /* Create open temporary file in safe way.  Please notice that the
  * file permissions are -rw------- by default. */
 int xmkstemp(char **tmpname, const char *dir, const char *prefix)
@@ -33,7 +62,7 @@ int xmkstemp(char **tmpname, const char *dir, const char *prefix)
 		return -1;
 
 	old_mode = umask(077);
-	fd = mkostemp(localtmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
+	fd = mkstemp_cloexec(localtmp);
 	umask(old_mode);
 	if (fd == -1) {
 		free(localtmp);
diff --git a/libblkid/src/save.c b/libblkid/src/save.c
index 5e8bbee..3070530 100644
--- a/libblkid/src/save.c
+++ b/libblkid/src/save.c
@@ -23,6 +23,7 @@
 #endif
 
 #include "closestream.h"
+#include "fileutils.h"
 
 #include "blkidP.h"
 
@@ -133,7 +134,7 @@ int blkid_flush_cache(blkid_cache cache)
 		tmp = malloc(strlen(filename) + 8);
 		if (tmp) {
 			sprintf(tmp, "%s-XXXXXX", filename);
-			fd = mkostemp(tmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
+			fd = mkstemp_cloexec(tmp);
 			if (fd >= 0) {
 				if (fchmod(fd, 0644) != 0)
 					DBG(SAVE, ul_debug("%s: fchmod failed", filename));
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* Re: [PATCH] Proper fallback for systems that lacks O_CLOEXEC (was: [PATCH 12/14] lib: provide mkostemp fallback function)
  2016-02-26 14:51       ` [PATCH] Proper fallback for systems that lacks O_CLOEXEC (was: [PATCH 12/14] lib: provide mkostemp fallback function) Yuriy M. Kaminskiy
  2016-02-27 19:40         ` Rüdiger Meier
@ 2016-03-07 13:50         ` Karel Zak
  1 sibling, 0 replies; 24+ messages in thread
From: Karel Zak @ 2016-03-07 13:50 UTC (permalink / raw)
  To: Yuriy M. Kaminskiy; +Cc: util-linux

On Fri, Feb 26, 2016 at 05:51:28PM +0300, Yuriy M. Kaminskiy wrote:
> +		if (O_CLOEXEC == 0) fcntl(inst->lock, F_SETFD, FD_CLOEXEC);

Thanks, but I think this is overkill. It really seems enough to
provide mkostemp fallback than promise O_CLOEXEC functionality on
systems where it's not implemented.

Maybe if we want to be friendly to non-linux and old libs than we can
add check to ./configure.ac and print warning on systems without
O_CLOEXEC.

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2016-03-07 13:50 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-26  3:03 [PATCH 00/14] some fixes, cleanup and portability issues Ruediger Meier
2016-02-26  3:03 ` [PATCH 01/14] logger: use SCM_CREDENTIALS on LINUX only Ruediger Meier
2016-02-26  3:03 ` [PATCH 02/14] build-sys: add openat build conditional Ruediger Meier
2016-02-26  3:03 ` [PATCH 03/14] fdisk: fix warning, incompatible pointer types passing 'uint64_t *' Ruediger Meier
2016-02-26  3:03 ` [PATCH 04/14] libsmartcols: fix uninitialized variable Ruediger Meier
2016-02-26  3:03 ` [PATCH 05/14] misc: fix some includes Ruediger Meier
2016-02-26 11:37   ` Ruediger Meier
2016-02-26  3:03 ` [PATCH 06/14] newgrp: rename memset_s() Ruediger Meier
2016-02-26 12:29   ` Yuriy M. Kaminskiy
2016-02-26 12:47     ` Ruediger Meier
2016-02-26  3:03 ` [PATCH 07/14] build-sys: build_init should check for flock Ruediger Meier
2016-02-26  3:03 ` [PATCH 08/14] login-utils: minor utmp cleanup Ruediger Meier
2016-02-26  3:03 ` [PATCH 09/14] build-sys: disable login-utils if shadow.h or utmp.h is missing Ruediger Meier
2016-02-26  3:08 ` [PATCH 10/14] build-sys: add --disable-ipcrm --disable-ipcs Ruediger Meier
2016-02-26  3:08   ` [PATCH 11/14] build-sys: chrt requires a sched_set* function Ruediger Meier
2016-02-26  3:08   ` [PATCH 12/14] lib: provide mkostemp fallback function Ruediger Meier
2016-02-26 12:51     ` Yuriy M. Kaminskiy
2016-02-26 13:50       ` Ruediger Meier
2016-02-26 14:51       ` [PATCH] Proper fallback for systems that lacks O_CLOEXEC (was: [PATCH 12/14] lib: provide mkostemp fallback function) Yuriy M. Kaminskiy
2016-02-27 19:40         ` Rüdiger Meier
2016-03-07 13:50         ` Karel Zak
2016-02-27 20:02     ` [PATCH v2 12/14] lib: provide fallback if mkostemp(3) missing Ruediger Meier
2016-02-26  3:08   ` [PATCH 13/14] build-sys: remove duplicate cal sources Ruediger Meier
2016-02-26  3:08   ` [PATCH 14/14] lib: include strutils.h for mempcpy() Ruediger Meier

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.