/* $Id: threaded_errno.c,v 1.6 2014/12/19 13:56:49 kostik Exp kostik $ */ #include #include #include #include #include #include #include #include static void test1(void) { static const char path[] = "/"; static int serrno; int error, xerrno; error = mkdir(path, 0777); if (error != -1) err(1, "mkdir(\"%s\") succeeded", path); if (serrno == 0) { serrno = errno; printf("mkdir(\"%s\") errno %d (%s)\n", path, serrno, strerror(serrno)); } else { xerrno = errno; if (xerrno != serrno) { err(1, "mkdir(\"%s\") errno %d->%d", path, serrno, xerrno); } } } static void test2(void) { static char longpath[2048]; static int serrno; int error, xerrno; if (longpath[0] == '\0') { longpath[0] = '/'; memset(longpath + 1, 'x', sizeof(longpath) - 2); longpath[sizeof(longpath) - 1] = '\0'; } error = mkdir(longpath, 0777); if (error != -1) err(1, "mkdir(\"long\") succeeded"); if (serrno == 0) { serrno = errno; printf("mkdir(\"long\") errno %d (%s)\n", serrno, strerror(serrno)); } else { xerrno = errno; if (xerrno != serrno) { err(1, "mkdir(\"long\") errno %d->%d", serrno, xerrno); } } } static void * thread1(void *arg __unused) { printf("thread1\n"); while (true) test1(); return (NULL); } static void * thread2(void *arg __unused) { printf("thread2\n"); while (true) test2(); return (NULL); } int main(void) { pthread_t t1, t2; int (*pthread_create_d)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *); int (*pthread_join_d)(pthread_t, void **); void *handle; int error; test1(); test2(); test1(); test2(); handle = dlopen("libpthread.so", RTLD_LAZY | RTLD_GLOBAL); if (handle == NULL) errx(1, "dlopen(\"libpthread.so\"): %s\n", dlerror()); pthread_create_d = (int (*)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *))dlfunc(handle, "pthread_create"); if (pthread_create_d == NULL) errx(1, "dlfunc(\"pthread_create\"): %s\n", dlerror()); pthread_join_d = (int (*)(pthread_t, void **))dlfunc(handle, "pthread_join"); if (pthread_join_d == NULL) errx(1, "dlfunc(\"pthread_join\"): %s\n", dlerror()); printf("Starting threads\n"); error = pthread_create_d(&t1, NULL, thread1, NULL); if (error != 0) errc(1, error, "pthread_create 1"); error = pthread_create_d(&t2, NULL, thread2, NULL); if (error != 0) errc(1, error, "pthread_create 2"); error = pthread_join_d(t1, NULL); if (error != 0) errc(1, error, "pthread_join 1"); error = pthread_join_d(t2, NULL); if (error != 0) errc(1, error, "pthread_join 2"); return (0); }