uml: remove code made redundant by CHOOSE_MODE removal
[safe/jmp/linux-2.6] / 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.h"
10 #include "mconsole.h"
11 #include "os.h"
12
13 struct dog_data {
14         int stdin;
15         int stdout;
16         int close_me[2];
17 };
18
19 static void pre_exec(void *d)
20 {
21         struct dog_data *data = d;
22
23         dup2(data->stdin, 0);
24         dup2(data->stdout, 1);
25         dup2(data->stdout, 2);
26         os_close_file(data->stdin);
27         os_close_file(data->stdout);
28         os_close_file(data->close_me[0]);
29         os_close_file(data->close_me[1]);
30 }
31
32 int start_watchdog(int *in_fd_ret, int *out_fd_ret, char *sock)
33 {
34         struct dog_data data;
35         int in_fds[2], out_fds[2], pid, n, err;
36         char pid_buf[sizeof("nnnnn\0")], c;
37         char *pid_args[] = { "/usr/bin/uml_watchdog", "-pid", pid_buf, NULL };
38         char *mconsole_args[] = { "/usr/bin/uml_watchdog", "-mconsole", NULL,
39                                   NULL };
40         char **args = NULL;
41
42         err = os_pipe(in_fds, 1, 0);
43         if(err < 0){
44                 printk("harddog_open - os_pipe failed, err = %d\n", -err);
45                 goto out;
46         }
47
48         err = os_pipe(out_fds, 1, 0);
49         if(err < 0){
50                 printk("harddog_open - os_pipe failed, err = %d\n", -err);
51                 goto out_close_in;
52         }
53
54         data.stdin = out_fds[0];
55         data.stdout = in_fds[1];
56         data.close_me[0] = out_fds[1];
57         data.close_me[1] = in_fds[0];
58
59         if(sock != NULL){
60                 mconsole_args[2] = sock;
61                 args = mconsole_args;
62         }
63         else {
64                 /* XXX The os_getpid() is not SMP correct */
65                 sprintf(pid_buf, "%d", os_getpid());
66                 args = pid_args;
67         }
68
69         pid = run_helper(pre_exec, &data, args);
70
71         os_close_file(out_fds[0]);
72         os_close_file(in_fds[1]);
73
74         if(pid < 0){
75                 err = -pid;
76                 printk("harddog_open - run_helper failed, errno = %d\n", -err);
77                 goto out_close_out;
78         }
79
80         n = os_read_file(in_fds[0], &c, sizeof(c));
81         if(n == 0){
82                 printk("harddog_open - EOF on watchdog pipe\n");
83                 helper_wait(pid);
84                 err = -EIO;
85                 goto out_close_out;
86         }
87         else if(n < 0){
88                 printk("harddog_open - read of watchdog pipe failed, "
89                        "err = %d\n", -n);
90                 helper_wait(pid);
91                 err = n;
92                 goto out_close_out;
93         }
94         *in_fd_ret = in_fds[0];
95         *out_fd_ret = out_fds[1];
96         return 0;
97
98  out_close_in:
99         os_close_file(in_fds[0]);
100         os_close_file(in_fds[1]);
101  out_close_out:
102         os_close_file(out_fds[0]);
103         os_close_file(out_fds[1]);
104  out:
105         return err;
106 }
107
108 void stop_watchdog(int in_fd, int out_fd)
109 {
110         os_close_file(in_fd);
111         os_close_file(out_fd);
112 }
113
114 int ping_watchdog(int fd)
115 {
116         int n;
117         char c = '\n';
118
119         n = os_write_file(fd, &c, sizeof(c));
120         if(n != sizeof(c)){
121                 printk("ping_watchdog - write failed, err = %d\n", -n);
122                 if(n < 0)
123                         return n;
124                 return -EIO;
125         }
126         return 1;
127
128 }