10

I need to investigate/test the behavior of some code on Linux under conditions where close might be interrupted by signal handlers (either with or without SA_RESTART). What is the most convenient setup to make the close syscall sleep for a measurable window of time during which I could try to hit the process with a signal? Some ideas:

  • Intentionally slow/non-responsive NFS mount
  • Custom FUSE driver

But since these are a bit of a pain to setup, I'm wondering if there's anything more off-the-shelf I could use that could give the desired behavior.

1
  • There is an answer to your question in the code sample link in stackoverflow.com/questions/40257564/…. This example requires that you deliberately interrupt a TCP connection, such that the packets are dropped. close() can be kept blocking for minutes this way. Oct 30, 2016 at 10:47

1 Answer 1

8

If nobody else has a better idea...

You could implement your own character device driver. Start with the template from Chapter 3 in Linux Device Drivers (3rd edition), and tweak it to do nothing except block for a while on close(). (You can use msleep or msleep_interruptible from Chapter 7 to do the blocking.)

Actually, if nobody else suggests something else, I can probably whip this up pretty quickly by adapting some existing code I have. How soon do you need it?

[edit]

OK, try this...

Makefile:

ifneq ($(KERNELRELEASE),)
        obj-m := closer.o

else
        KERNELDIR ?= /lib/modules/$(shell uname -r)/build
        PWD := $(shell pwd)

default: modules

%:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) "$@"

.PHONY: default
endif

closer.c:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/fs.h>

MODULE_DESCRIPTION("Block-on-close driver");
MODULE_AUTHOR("Nemo <[email protected]>");
MODULE_LICENSE("GPL");
#define VERSION "20110705"
MODULE_VERSION(VERSION);

#define MY_NAME "closer"

int my_open(struct inode *, struct file *);
int my_release(struct inode *, struct file *);
ssize_t my_read(struct file *, char __user *, size_t, loff_t *);
ssize_t my_write(struct file *, const char __user *, size_t, loff_t *);

static struct file_operations my_fops = {
    .owner = THIS_MODULE,
    .open = my_open,
    .read = my_read,
    .write = my_write,
    .release = my_release,
};

static struct miscdevice my_dev;

int __init
my_init(void)
{
    int err = 0;

    printk(KERN_INFO "%s: loading version %s\n", MY_NAME, VERSION);

    my_dev.minor = MISC_DYNAMIC_MINOR;
    my_dev.name = MY_NAME;
    my_dev.fops = &my_fops;
    err = misc_register(&my_dev);

    if (err)
        printk(KERN_ERR "%s: misc_register failed, error %d\n", MY_NAME, err);

    return err;
}

int
my_open(struct inode *inode, struct file *filp)
{
    return 0;
}

ssize_t
my_read(struct file *file, char __user *p, size_t n, loff_t *off) {
    return 0;
}

ssize_t
my_write(struct file *file, const char __user *p, size_t n, loff_t *off) {
    return n;
}

int
my_release(struct inode *inode, struct file *filp)
{
    int err = 0;
    /* 10 second sleep, interruptible. */
    if (msleep_interruptible(10 * 1000) > 0)
        err = -EINTR;

    return err;
}

void __exit
my_exit(void)
{
    misc_deregister(&my_dev);
    printk(KERN_INFO "%s: unloaded\n", MY_NAME);
}

module_init(my_init);
module_exit(my_exit);

Load the module using "insmod closer.o". If you have a reasonably modern/complete Linux environment, udev will wake up and generate /dev/closer automatically. If not, you can create the device node yourself:

mknod /dev/closer c `tr : ' ' </sys/class/misc/closer/dev`

(That is, /sys/class/misc/closer/dev indicates the major:minor to use.)

Reads and writes work like /dev/null; i.e., EOF on any read, success on any write.

I have verified that "cat < /dev/closer" blocks in close() for 10 seconds. I have not created a test to catch SIGINT (or whatever) and verify that it actually results in EINTR.

Built against a 2.6.32 kernel. Let me know how it works for you.

6
  • It's not a hurry; I have plenty of other tasks I can work on in the mean time. Jul 25, 2011 at 19:51
  • +1 Verified on x86_64 2.6.38, Ubuntu 11.04. (Though for whatever reason the Makefile didn't work out of just /tmp for me, so I just manually ran make -C /lib/modules/$(uname -r)/build M=$(pwd))
    – user786653
    Jul 25, 2011 at 21:21
  • Thanks! Somehow I missed checking this out til now. Rather odd behavior though: when close gets interrupted by a signal, it just returns 0 (success)... Aug 7, 2011 at 2:39
  • I found the problem: the return value of release is ignored. Using the flush method instead gave the expected results. Aug 7, 2011 at 3:55
  • Interesting... Sounds like "release" ought to return "void" then? Or maybe that is just a bug. Should it really be forbidden for close to perform interruotible sleep? I will ask on the kernel list once I return from vacation.
    – Nemo
    Aug 7, 2011 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.