From 5976114f628c96285c9b3f2cc55994f216afe6f2 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 30 Mar 2011 09:30:05 +0200 Subject: libmount: block signals when writing to mtab Signed-off-by: Karel Zak --- shlibs/mount/src/lock.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'shlibs/mount/src/lock.c') diff --git a/shlibs/mount/src/lock.c b/shlibs/mount/src/lock.c index 554915876..55dcf73b2 100644 --- a/shlibs/mount/src/lock.c +++ b/shlibs/mount/src/lock.c @@ -40,7 +40,11 @@ struct libmnt_lock { char *lockfile; /* path to lock file (e.g. /etc/mtab~) */ char *linkfile; /* path to link file (e.g. /etc/mtab~.) */ int lockfile_fd; /* lock file descriptor */ - int locked; /* do we own the lock? */ + + int locked :1; /* do we own the lock? */ + int sigblock: 1; /* block signals when locked */ + + sigset_t oldsigmask; }; @@ -102,6 +106,24 @@ void mnt_free_lock(struct libmnt_lock *ml) free(ml); } +/** + * mnt_lock_block_signals: + * @ml: struct libmnt_lock handler + * @enable: TRUE/FALSE + * + * Block/unblock signals when the lock is locked, the signals are not blocked + * by default. + * + * Returns: <0 on error, 0 on success. + */ +int mnt_lock_block_signals(struct libmnt_lock *ml, int enable) +{ + if (!ml) + return -EINVAL; + ml->sigblock = enable; + return 0; +} + /* * Returns path to lockfile. */ @@ -254,6 +276,11 @@ void mnt_unlock_file(struct libmnt_lock *ml) ml->locked = 0; ml->lockfile_fd = -1; + + if (ml->sigblock) + sigprocmask(SIG_SETMASK, &ml->oldsigmask, NULL); + + ml->sigblock = 0; } /** @@ -337,6 +364,20 @@ int mnt_lock_file(struct libmnt_lock *ml) if (!linkfile) return -EINVAL; + if (ml->sigblock) { + /* + * Block all signals when locked, mnt_unlock_file() will + * restore the old mask. + */ + sigset_t sigs; + + sigemptyset(&ml->oldsigmask); + sigfillset(&sigs); + sigdelset(&sigs, SIGTRAP); + sigdelset(&sigs, SIGALRM); + sigprocmask(SIG_BLOCK, &sigs, &ml->oldsigmask); + } + i = open(linkfile, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); if (i < 0) { /* linkfile does not exist (as a file) and we cannot create it. -- cgit v1.2.3-55-g7522