+
+int setns(int fd, int nstype)
+{
+ static int (*plibc_func)(int fd, int nstype) = NULL;
+ int retval;
+ int saved_errno;
+
+ if (plibc_func == NULL) {
+ plibc_func = dlsym(RTLD_NEXT, "setns");
+ if (plibc_func == NULL) {
+ fprintf(stderr, "libustfork: unable to find \"setns\" symbol\n");
+ errno = ENOSYS;
+ return -1;
+ }
+ }
+
+ /* Do the real setns */
+ retval = plibc_func(fd, nstype);
+ saved_errno = errno;
+
+ ust_after_setns();
+
+ errno = saved_errno;
+ return retval;
+}
+
+int unshare(int flags)
+{
+ static int (*plibc_func)(int flags) = NULL;
+ int retval;
+ int saved_errno;
+
+ if (plibc_func == NULL) {
+ plibc_func = dlsym(RTLD_NEXT, "unshare");
+ if (plibc_func == NULL) {
+ fprintf(stderr, "libustfork: unable to find \"unshare\" symbol\n");
+ errno = ENOSYS;
+ return -1;
+ }
+ }
+
+ /* Do the real setns */
+ retval = plibc_func(flags);
+ saved_errno = errno;
+
+ ust_after_unshare();
+
+ errno = saved_errno;
+ return retval;
+}
+
+int setresuid(uid_t ruid, uid_t euid, uid_t suid)
+{
+ static int (*plibc_func)(uid_t ruid, uid_t euid, uid_t suid) = NULL;
+ int retval;
+ int saved_errno;
+
+ if (plibc_func == NULL) {
+ plibc_func = dlsym(RTLD_NEXT, "setresuid");
+ if (plibc_func == NULL) {
+ fprintf(stderr, "libustfork: unable to find \"setresuid\" symbol\n");
+ errno = ENOSYS;
+ return -1;
+ }
+ }
+
+ /* Do the real setresuid */
+ retval = plibc_func(ruid, euid, suid);
+ saved_errno = errno;
+
+ ust_after_setresuid();
+
+ errno = saved_errno;
+ return retval;
+}
+
+int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
+{
+ static int (*plibc_func)(gid_t rgid, gid_t egid, gid_t sgid) = NULL;
+ int retval;
+ int saved_errno;
+
+ if (plibc_func == NULL) {
+ plibc_func = dlsym(RTLD_NEXT, "setresgid");
+ if (plibc_func == NULL) {
+ fprintf(stderr, "libustfork: unable to find \"setresgid\" symbol\n");
+ errno = ENOSYS;
+ return -1;
+ }
+ }
+
+ /* Do the real setresgid */
+ retval = plibc_func(rgid, egid, sgid);
+ saved_errno = errno;
+
+ ust_after_setresgid();
+
+ errno = saved_errno;
+ return retval;
+}
+
+#elif defined (__FreeBSD__)
+
+pid_t rfork(int flags)
+{
+ static pid_t (*plibc_func)(void) = NULL;
+ sigset_t sigset;
+ pid_t retval;
+ int saved_errno;
+
+ if (plibc_func == NULL) {
+ plibc_func = dlsym(RTLD_NEXT, "rfork");
+ if (plibc_func == NULL) {
+ fprintf(stderr, "libustfork: unable to find \"rfork\" symbol\n");
+ errno = ENOSYS;
+ return -1;
+ }
+ }
+
+ ust_before_fork(&sigset);
+ /* Do the real rfork */
+ retval = plibc_func();
+ saved_errno = errno;
+ if (retval == 0) {
+ /* child */
+ ust_after_fork_child(&sigset);
+ } else {
+ ust_after_fork_parent(&sigset);
+ }
+ errno = saved_errno;
+ return retval;
+}
+
+/*
+ * On BSD, no need to override vfork, because it runs in the context of
+ * the parent, with parent waiting until execve or exit is executed in
+ * the child.
+ */
+
+#else
+#warning "Unknown OS. You might want to ensure that fork/clone/vfork/fork handling is complete."
+#endif