Merge branches 'hammer2' and 'master' of ssh://crater.dragonflybsd.org/repository...
[dragonfly.git] / sbin / hammer2 / subs.c
1 /*
2  * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include "hammer2.h"
37
38 /*
39  * Obtain a file descriptor that the caller can execute ioctl()'s on.
40  */
41 int
42 hammer2_ioctl_handle(const char *sel_path)
43 {
44         struct hammer2_ioc_version info;
45         int fd;
46
47         if (sel_path == NULL)
48                 sel_path = ".";
49
50         fd = open(sel_path, O_RDONLY, 0);
51         if (fd < 0) {
52                 fprintf(stderr, "hammer2: Unable to open %s: %s\n",
53                         sel_path, strerror(errno));
54                 return(-1);
55         }
56         if (ioctl(fd, HAMMER2IOC_GET_VERSION, &info) < 0) {
57                 fprintf(stderr, "hammer2: '%s' is not a hammer2 filesystem\n",
58                         sel_path);
59                 close(fd);
60                 return(-1);
61         }
62         return (fd);
63 }
64
65 void
66 hammer2_disconnect(void *(*func)(void *), void *arg)
67 {
68         pthread_t thread = NULL;
69         pid_t pid;
70         int ttyfd;
71
72         /*
73          * Do not disconnect in debug mode
74          */
75         if (DebugOpt) {
76                 pthread_create(&thread, NULL, func, arg);
77                 NormalExit = 0;
78                 return;
79         }
80
81         /*
82          * Otherwise disconnect us.  Double-fork to get rid of the ppid
83          * association and disconnect the TTY.
84          */
85         if ((pid = fork()) < 0) {
86                 fprintf(stderr, "hammer2: fork(): %s\n", strerror(errno));
87                 exit(1);
88         }
89         if (pid > 0) {
90                 while (waitpid(pid, NULL, 0) != pid)
91                         ;
92                 return;         /* parent returns */
93         }
94
95         /*
96          * Get rid of the TTY/session before double-forking to finish off
97          * the ppid.
98          */
99         ttyfd = open("/dev/null", O_RDWR);
100         if (ttyfd >= 0) {
101                 if (ttyfd != 0)
102                         dup2(ttyfd, 0);
103                 if (ttyfd != 1)
104                         dup2(ttyfd, 1);
105                 if (ttyfd != 2)
106                         dup2(ttyfd, 2);
107                 if (ttyfd > 2)
108                         close(ttyfd);
109         }
110
111         ttyfd = open("/dev/tty", O_RDWR);
112         if (ttyfd >= 0) {
113                 ioctl(ttyfd, TIOCNOTTY, 0);
114                 close(ttyfd);
115         }
116         setsid();
117
118         /*
119          * Second fork to disconnect ppid (the original parent waits for
120          * us to exit).
121          */
122         if ((pid = fork()) < 0) {
123                 _exit(2);
124         }
125         if (pid > 0)
126                 _exit(0);
127
128         /*
129          * The double child
130          */
131         setsid();
132         pthread_create(&thread, NULL, func, arg);
133         pthread_exit(NULL);
134         _exit(2);       /* NOT REACHED */
135 }