ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / um / drivers / harddog_user.c
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include "user_util.h"
10 #include "user.h"
11 #include "helper.h"
12 #include "mconsole.h"
13 #include "os.h"
14 #include "choose-mode.h"
15 #include "mode.h"
16
17 struct dog_data {
18         int stdin;
19         int stdout;
20         int close_me[2];
21 };
22
23 static void pre_exec(void *d)
24 {
25         struct dog_data *data = d;
26
27         dup2(data->stdin, 0);
28         dup2(data->stdout, 1);
29         dup2(data->stdout, 2);
30         close(data->stdin);
31         close(data->stdout);
32         close(data->close_me[0]);
33         close(data->close_me[1]);
34 }
35
36 int start_watchdog(int *in_fd_ret, int *out_fd_ret, char *sock)
37 {
38         struct dog_data data;
39         int in_fds[2], out_fds[2], pid, n, err;
40         char pid_buf[sizeof("nnnnn\0")], c;
41         char *pid_args[] = { "/usr/bin/uml_watchdog", "-pid", pid_buf, NULL };
42         char *mconsole_args[] = { "/usr/bin/uml_watchdog", "-mconsole", NULL, 
43                                   NULL };
44         char **args = NULL;
45
46         err = os_pipe(in_fds, 1, 0);
47         if(err){
48                 printk("harddog_open - os_pipe failed, errno = %d\n", -err);
49                 return(err);
50         }
51
52         err = os_pipe(out_fds, 1, 0);
53         if(err){
54                 printk("harddog_open - os_pipe failed, errno = %d\n", -err);
55                 return(err);
56         }
57
58         data.stdin = out_fds[0];
59         data.stdout = in_fds[1];
60         data.close_me[0] = out_fds[1];
61         data.close_me[1] = in_fds[0];
62
63         if(sock != NULL){
64                 mconsole_args[2] = sock;
65                 args = mconsole_args;
66         }
67         else {
68                 /* XXX The os_getpid() is not SMP correct */
69                 sprintf(pid_buf, "%d", CHOOSE_MODE(tracing_pid, os_getpid()));
70                 args = pid_args;
71         }
72
73         pid = run_helper(pre_exec, &data, args, NULL);
74
75         close(out_fds[0]);
76         close(in_fds[1]);
77
78         if(pid < 0){
79                 err = -pid;
80                 printk("harddog_open - run_helper failed, errno = %d\n", err);
81                 goto out;
82         }
83
84         n = read(in_fds[0], &c, sizeof(c));
85         if(n == 0){
86                 printk("harddog_open - EOF on watchdog pipe\n");
87                 helper_wait(pid);
88                 err = -EIO;
89                 goto out;
90         }
91         else if(n < 0){
92                 printk("harddog_open - read of watchdog pipe failed, "
93                        "errno = %d\n", errno);
94                 helper_wait(pid);
95                 err = -errno;
96                 goto out;
97         }
98         *in_fd_ret = in_fds[0];
99         *out_fd_ret = out_fds[1];
100         return(0);
101  out:
102         close(out_fds[1]);
103         close(in_fds[0]);
104         return(err);
105 }
106
107 void stop_watchdog(int in_fd, int out_fd)
108 {
109         close(in_fd);
110         close(out_fd);
111 }
112
113 int ping_watchdog(int fd)
114 {
115         int n;
116         char c = '\n';
117
118         n = write(fd, &c, sizeof(c));
119         if(n < sizeof(c)){
120                 printk("ping_watchdog - write failed, errno = %d\n",
121                        errno);
122                 return(-errno);
123         }
124         return 1;
125
126 }
127
128 /*
129  * Overrides for Emacs so that we follow Linus's tabbing style.
130  * Emacs will notice this stuff at the end of the file and automatically
131  * adjust the settings for this buffer only.  This must remain at the end
132  * of the file.
133  * ---------------------------------------------------------------------------
134  * Local variables:
135  * c-file-style: "linux"
136  * End:
137  */