Skip to contentSkip to conversation

some fixes, cleanup and portability issues

PATCH In review Ruediger Meier 3 participants 24 messages Feb 26, 2016 – Mar 7, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover some fixes, cleanup and portability issues

From: Ruediger Meier <[email protected]>

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(-)
Signature · 1 line
1.8.4.5
#1 lore raw

PATCH 1/14 logger: use SCM_CREDENTIALS on LINUX only

From: Ruediger Meier <[email protected]>

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.

---
 misc-utils/logger.c | 4 ++++
 1 file changed, 4 insertions(+)
misc-utils/logger.c +4 -0
index 4201e43..d9aee0e 100644
@@ -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"));
Signature · 1 line
1.8.4.5
#2 lore raw

PATCH 2/14 build-sys: add openat build conditional

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

Using this conditional for test progs only should be ok.

---
 configure.ac                       | 2 ++
 libsmartcols/samples/Makemodule.am | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)
configure.ac +2 -0
index 7b6a9e7..b8b0074 100644
@@ -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"])]
libsmartcols/samples/Makemodule.am +3 -1
index be74fc3..0a2d892 100644
@@ -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)
Signature · 1 line
1.8.4.5
#3 lore raw

PATCH 3/14 fdisk: fix warning, incompatible pointer types passing 'uint64_t *'

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

---
 disk-utils/fdisk.c | 4 ++--
 libfdisk/src/gpt.c | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)
disk-utils/fdisk.c +2 -2
index 0b22983..2f95fe8 100644
@@ -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);
libfdisk/src/gpt.c +2 -1
index f795b35..cd88df0 100644
@@ -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;
Signature · 1 line
1.8.4.5
#4 lore raw

PATCH 4/14 libsmartcols: fix uninitialized variable

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

This got broken in 2a750b4c.

---
 libsmartcols/src/table_print.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsmartcols/src/table_print.c +1 -1
index d1dcb65..510386e 100644
@@ -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 */
Signature · 1 line
1.8.4.5
#5 lore raw

PATCH 5/14 misc: fix some includes

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

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

---
 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(-)
configure.ac +0 -1
index b8b0074..e3b0bf5 100644
@@ -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 \
lib/blkdev.c +0 -7
index 403e9e4..ddd6e9b 100644
@@ -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
misc-utils/logger.c +1 -0
index d9aee0e..896d1b8 100644
@@ -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"
misc-utils/test_uuidd.c +0 -1
index 1262e35..78399b9 100644
@@ -24,7 +24,6 @@
  *	make uuidd uuidgen localstatedir=/var
  */
 #include <error.h>
-#include <libgen.h>
 #include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
sys-utils/ipcs.c +0 -1
index 5877c46..0ebcc4c 100644
@@ -16,7 +16,6 @@
  */
 
 #include <errno.h>
-#include <features.h>
 #include <getopt.h>
 
 #include "c.h"
sys-utils/lsipc.c +0 -1
index 9e34e6b..938728a 100644
@@ -25,7 +25,6 @@
  */
 
 #include <errno.h>
-#include <features.h>
 #include <getopt.h>
 #include <sys/time.h>
 #include <unistd.h>
sys-utils/rtcwake.c +0 -1
index 842ea50..7c748dc 100644
@@ -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>
text-utils/pg.c +0 -1
index 998010a..9ef00f0 100644
@@ -58,7 +58,6 @@
 #include <unistd.h>
 #include <signal.h>
 #include <setjmp.h>
-#include <libgen.h>
 
 #ifdef HAVE_NCURSES_H
 # include <ncurses.h>
Signature · 1 line
1.8.4.5
#6 lore raw

Re: [PATCH 05/14] misc: fix some includes

Quoted 13 lines · Ruediger Meier

From: Ruediger Meier <[email protected]>

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

[...]

lib/blkdev.c +0 -0
index 403e9e4..ddd6e9b 100644

lib/blkdev.c line 27

Quoted 10 lines
lib/blkdev.c +0 -7
@@ -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.

#7 lore raw

PATCH 6/14 newgrp: rename memset_s()

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

It's already defined in OSX standard c library.

---
 login-utils/newgrp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
login-utils/newgrp.c +2 -2
index 141b41b..56639d1 100644
@@ -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"));
Signature · 1 line
1.8.4.5
#8 lore raw

Re: [PATCH 06/14] newgrp: rename memset_s()

Quoted 3 lines · Ruediger Meier

From: Ruediger Meier <[email protected]>

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).

Quoted 27 lines
---
 login-utils/newgrp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
login-utils/newgrp.c +2 -2
index 141b41b..56639d1 100644
@@ -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"));
#9 lore raw

Re: [PATCH 06/14] newgrp: rename memset_s()

Quoted 8 lines · Yuriy M. Kaminskiy
Quoted 3 lines · Ruediger Meier

From: Ruediger Meier <[email protected]>

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.

Quoted 33 lines
Quoted 20 lines
---
 login-utils/newgrp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
login-utils/newgrp.c +0 -0
index 141b41b..56639d1 100644
@@ -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). */

login-utils/newgrp.c +1 -1
-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;

login-utils/newgrp.c static int allow_setgid(const struct passwd · line 154

Quoted 8 lines
login-utils/newgrp.c +1 -1
@@ -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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw

PATCH 7/14 build-sys: build_init should check for flock

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

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

---
 configure.ac | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
configure.ac +1 -1
index e3b0bf5..80123d9 100644
@@ -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])
 
Signature · 1 line
1.8.4.5
#11 lore raw

PATCH 8/14 login-utils: minor utmp cleanup

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

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

---
 login-utils/last.c  | 2 +-
 login-utils/login.c | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)
login-utils/last.c +1 -1
index dbfa8ae..38b92ef 100644
@@ -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);
login-utils/login.c +0 -1
index e1f11eb..c5ce188 100644
@@ -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>
Signature · 1 line
1.8.4.5
#12 lore raw

PATCH 9/14 build-sys: disable login-utils if shadow.h or utmp.h is missing

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

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).

---
 configure.ac | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
configure.ac +21 -4
index 80123d9..2f1a687 100644
@@ -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])
 
 
Signature · 1 line
1.8.4.5
#13 lore raw

PATCH 10/14 build-sys: add --disable-ipcrm --disable-ipcs

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

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.

---
 configure.ac | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
configure.ac +10 -2
index 2f1a687..d6c0c05 100644
@@ -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])
Signature · 1 line
1.8.4.5
#14 lore raw

PATCH 11/14 build-sys: chrt requires a sched_set* function

From: Ruediger Meier <[email protected]>

---
 configure.ac | 7 +++++++
 1 file changed, 7 insertions(+)
configure.ac +7 -0
index d6c0c05..727a875 100644
@@ -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], [
Signature · 1 line
1.8.4.5
#15 lore raw

PATCH 12/14 lib: provide mkostemp fallback function

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

It's missing on OSX.

---
 configure.ac        |  1 +
 include/fileutils.h |  4 ++++
 lib/fileutils.c     | 27 +++++++++++++++++++++++++++
 libblkid/src/save.c |  1 +
 4 files changed, 33 insertions(+)
configure.ac +1 -0
index 727a875..64a16ff 100644
@@ -368,6 +368,7 @@ AC_CHECK_FUNCS([ \
 	llseek \
 	lseek64 \
 	mempcpy \
+	mkostemp \
 	nanosleep \
 	ntp_gettime \
 	personality \
include/fileutils.h +4 -0
index ba8da7f..7c5594e 100644
@@ -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)
lib/fileutils.c +27 -0
index bf8e60a..05a8c02 100644
@@ -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)
libblkid/src/save.c +1 -0
index 5e8bbee..b9f447a 100644
@@ -23,6 +23,7 @@
 #endif
 
 #include "closestream.h"
+#include "fileutils.h"
 
 #include "blkidP.h"
 
Signature · 1 line
1.8.4.5
#16 lore raw

Re: [PATCH 12/14] lib: provide mkostemp fallback function

Quoted 47 lines · Ruediger Meier

From: Ruediger Meier <[email protected]>

It's missing on OSX.

---
 configure.ac        |  1 +
 include/fileutils.h |  4 ++++
 lib/fileutils.c     | 27 +++++++++++++++++++++++++++
 libblkid/src/save.c |  1 +
 4 files changed, 33 insertions(+)
configure.ac +1 -0
index 727a875..64a16ff 100644
@@ -368,6 +368,7 @@ AC_CHECK_FUNCS([ \
 	llseek \
 	lseek64 \
 	mempcpy \
+	mkostemp \
 	nanosleep \
 	ntp_gettime \
 	personality \
include/fileutils.h +4 -0
index ba8da7f..7c5594e 100644
@@ -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)
lib/fileutils.c +0 -0
index bf8e60a..05a8c02 100644
@@ -13,6 +13,33 @@
 #include "fileutils.h"
 #include "pathnames.h"
 

lib/fileutils.c line 25

Quoted 10 lines
lib/fileutils.c +10 -0
+#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).

Quoted 31 lines
lib/fileutils.c +17 -0
+	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)
libblkid/src/save.c +1 -0
index 5e8bbee..b9f447a 100644
@@ -23,6 +23,7 @@
 #endif
 
 #include "closestream.h"
+#include "fileutils.h"
 
 #include "blkidP.h"
#17 lore raw

Re: [PATCH 12/14] lib: provide mkostemp fallback function

Quoted 49 lines · Yuriy M. Kaminskiy
Quoted 48 lines · Ruediger Meier

From: Ruediger Meier <[email protected]>

It's missing on OSX.

---
 configure.ac        |  1 +
 include/fileutils.h |  4 ++++
 lib/fileutils.c     | 27 +++++++++++++++++++++++++++
 libblkid/src/save.c |  1 +
 4 files changed, 33 insertions(+)
configure.ac +1 -0
index 727a875..64a16ff 100644
@@ -368,6 +368,7 @@ AC_CHECK_FUNCS([ \
 	llseek \
 	lseek64 \
 	mempcpy \
+	mkostemp \
 	nanosleep \
 	ntp_gettime \
 	personality \
include/fileutils.h +4 -0
index ba8da7f..7c5594e 100644
@@ -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

lib/fileutils.c +0 -0
@@ -13,6 +13,33 @@
 #include "fileutils.h"
 #include "pathnames.h"
 
Quoted 12 lines

lib/fileutils.c line 25

Quoted 10 lines
lib/fileutils.c +10 -0
+#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.

Quoted 5 lines

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.

Quoted 8 lines

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

Quoted 36 lines
Quoted 25 lines
lib/fileutils.c +17 -0
+	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)
libblkid/src/save.c +0 -0
index 5e8bbee..b9f447a 100644

libblkid/src/save.c line 28

Quoted 6 lines
libblkid/src/save.c +1 -0
@@ -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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#18 lore raw

PATCH 1/1 Proper fallback for systems that lacks O_CLOEXEC (was: [PATCH 12/14] lib: provide mkostemp fallback function)

In reply to Yuriy M. Kaminskiy

[email protected] (Yuriy M. Kaminskiy)

Quoted 4 lines · writes:
Quoted 2 lines · Ruediger Meier

From: Ruediger Meier <[email protected]>

[...]

Quoted 4 lines

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.

Quoted 1 line

From 510a63401fbc251600648c803d9867bb27a9ce64 Mon Sep 17 00:00:00 2001

From: "Yuriy M. Kaminskiy" <[email protected]>
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().

---
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(-)
disk-utils/fsck.c +1 -0
index b8a47a1..f10b08e 100644
@@ -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 &&
lib/blkdev.c +1 -0
index a57b367..66a472e 100644
@@ -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");
lib/fileutils.c +1 -0
index bf8e60a..897e3ce 100644
@@ -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;
lib/loopdev.c +4 -1
index 54c6200..dcee458 100644
@@ -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)"));
libblkid/src/evaluate.c +1 -0
index ffbe097..e3c2424 100644
@@ -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);
libblkid/src/probe.c +2 -0
index 4efa2ba..e4c1102 100644
@@ -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);
libblkid/src/read.c +1 -0
index 41e564f..c5403fd 100644
@@ -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) ||
libblkid/src/save.c +1 -0
index 5e8bbee..d7955f3 100644
@@ -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)))
libblkid/src/topology/md.c +1 -0
index 5eba947..39b104a 100644
@@ -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));
libblkid/src/verify.c +1 -0
index 06f6d53..bdb6551 100644
@@ -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 */
libfdisk/src/context.c +1 -0
index 1f6118c..b4c796e 100644
@@ -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;
libmount/src/lock.c +2 -0
index 0d94329..ca218eb 100644
@@ -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;
libmount/src/monitor.c +1 -0
index 9bc7ea3..b274c6c 100644
@@ -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:
libmount/src/tab_parse.c +3 -0
index 4c47390..b3219cb 100644
@@ -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);
 		}
libmount/src/utils.c +1 -0
index 39f6c85..98820fd 100644
@@ -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
libsmartcols/samples/tree.c +1 -0
index 7f41f9e..1e262c6 100644
@@ -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);
 		}
libuuid/src/gen_uuid.c +1 -0
index 4d60997..c8c1e18 100644
@@ -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);
login-utils/sulogin-consoles.c +5 -0
index 1b05b38..36c2f58 100644
@@ -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) {
misc-utils/blkid.c +1 -0
index 4ecbb5d..a4f05df 100644
@@ -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;
 
sys-utils/rtcwake.c +1 -0
index 842ea50..a89220c 100644
@@ -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;
 }
sys-utils/wdctl.c +2 -0
index 095d8ed..e09f031 100644
@@ -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)
term-utils/agetty.c +3 -0
index cd57c67..812875a 100644
@@ -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);
 
Signature · 1 line
2.1.4
#19 lore raw

Re: [PATCH] Proper fallback for systems that lacks O_CLOEXEC (was: [PATCH 12/14] lib: provide mkostemp fallback function)

Quoted 8 lines · On Friday 26 February 2016 15:51:28 Yuriy M. Kaminskiy wrote:

[email protected] (Yuriy M. Kaminskiy)

Quoted 2 lines · writes:
Quoted 1 line · Ruediger Meier

From: Ruediger Meier <[email protected]>

[...]

Quoted 8 lines
Quoted 5 lines

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

maintainer
#20 lore raw

Re: [PATCH] Proper fallback for systems that lacks O_CLOEXEC (was: [PATCH 12/14] lib: provide mkostemp fallback function)

In reply to Yuriy M. Kaminskiy

disk-utils/fsck.c static void lock_disk(struct fsck_instance *inst) · line 378

Quoted 1 line · Yuriy M. Kaminskiy
disk-utils/fsck.c +1 -0
+		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

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#21 lore raw

PATCH v2 12/14 lib: provide fallback if mkostemp(3) missing

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

It's missing on OSX.

CC Yuriy M. Kaminskiy <[email protected]>
---
 configure.ac        |  1 +
 include/fileutils.h |  2 ++
 lib/fileutils.c     | 31 ++++++++++++++++++++++++++++++-
 libblkid/src/save.c |  3 ++-
 4 files changed, 35 insertions(+), 2 deletions(-)
configure.ac +1 -0
index 727a875..64a16ff 100644
@@ -368,6 +368,7 @@ AC_CHECK_FUNCS([ \
 	llseek \
 	lseek64 \
 	mempcpy \
+	mkostemp \
 	nanosleep \
 	ntp_gettime \
 	personality \
include/fileutils.h +2 -0
index ba8da7f..79dd012 100644
@@ -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)
lib/fileutils.c +30 -1
index bf8e60a..15cc925 100644
@@ -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);
libblkid/src/save.c +2 -1
index 5e8bbee..3070530 100644
@@ -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));
Signature · 1 line
1.9.1
#22 lore raw

PATCH 13/14 build-sys: remove duplicate cal sources

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

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

---
 misc-utils/Makemodule.am | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
misc-utils/Makemodule.am +1 -9
index aa54919..120aadf 100644
@@ -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
Signature · 1 line
1.8.4.5
#23 lore raw

PATCH 14/14 lib: include strutils.h for mempcpy()

In reply to Ruediger Meier

From: Ruediger Meier <[email protected]>

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));
---
 lib/mbsalign.c | 1 +
 1 file changed, 1 insertion(+)
lib/mbsalign.c +1 -0
index 2a8de2f..f9babba 100644
@@ -27,6 +27,7 @@
 
 #include "c.h"
 #include "mbsalign.h"
+#include "strutils.h"
 #include "widechar.h"
 
 #ifdef HAVE_WIDECHAR
Signature · 1 line
1.8.4.5