monitoring-plugins has a program that checks available space on partitions.
Before doing this it does a stat() to check that the requested directory
exists and is accessible. In their devel tree they have moved to doing
this stat() in a thread - commit log was "don't let check_disk hang on
hanging file systems". However this code doesn't work for us.

I've attached a stripped-down test program based on their code that works
as expected on Linux but fails on OpenBSD. I can always patch to use the
non-pthread code, but I wondered if anyone has an idea what's up and
whether the bug is theirs or ours - is pthread_kill(thread, 0) working
as expected?

$ make thread LDFLAGS=-lpthread
cc -O2 -pipe   -lpthread -o thread thread.c

$ ./thread
4
child
3
2
1
0
child thread did not return within 5s

#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>

void do_something();
void *child(void *);

int
main(int argc, char **argv)
{
        do_something();
}

void do_something()
{
        pthread_t stat_thread;
        int done = 0;
        int timer = 5;
        struct timespec req, rem;

        req.tv_sec = 0;
        pthread_create(&stat_thread, NULL, child, NULL);
        while (timer-- > 0) {
                printf("%u\n", timer);
                req.tv_nsec = 10000000;
                nanosleep(&req, &rem);
                if (pthread_kill(stat_thread, 0)) {
                        done = 1;
                        break;
                } else {
                        printf("e %u\n", errno);
                        req.tv_nsec = 990000000;
                        nanosleep(&req, &rem);
                }
        }
        if (done == 1) {
                pthread_join(stat_thread, NULL);
        } else {
                pthread_detach(stat_thread);
                printf("child thread did not return within 5s\n");
        }
}

void *child(void *in)
{
        struct timespec xeq, xem;
        xeq.tv_nsec = 9590000000;
        printf("child1\n");
        nanosleep(&xeq, &xem);
        printf("child2\n");
}

Reply via email to