blob: 19ea26f32f144dd484d53ce554e520957b768391 [file] [log] [blame]
Jeff Dike5bbcbec2007-02-10 01:43:58 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "mconsole.h"
11#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13struct dog_data {
14 int stdin;
15 int stdout;
16 int close_me[2];
17};
18
19static 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
32int 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 };
Jeff Dike5bbcbec2007-02-10 01:43:58 -080038 char *mconsole_args[] = { "/usr/bin/uml_watchdog", "-mconsole", NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 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 */
Jeff Dike6aa802c2007-10-16 01:26:56 -070065 sprintf(pid_buf, "%d", os_getpid());
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 args = pid_args;
67 }
68
Jeff Dikec4399012007-07-15 23:38:56 -070069 pid = run_helper(pre_exec, &data, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
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
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070080 n = os_read_file(in_fds[0], &c, sizeof(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 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];
Jeff Dike5bbcbec2007-02-10 01:43:58 -080096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
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:
Jeff Dike5bbcbec2007-02-10 01:43:58 -0800105 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108void stop_watchdog(int in_fd, int out_fd)
109{
110 os_close_file(in_fd);
111 os_close_file(out_fd);
112}
113
114int ping_watchdog(int fd)
115{
116 int n;
117 char c = '\n';
118
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700119 n = os_write_file(fd, &c, sizeof(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if(n != sizeof(c)){
121 printk("ping_watchdog - write failed, err = %d\n", -n);
122 if(n < 0)
Jeff Dike5bbcbec2007-02-10 01:43:58 -0800123 return n;
124 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126 return 1;
127
128}