blob: b050e267befe879da3382de2aa9d8914dae3f671 [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#include "mode.h"
13
14struct dog_data {
15 int stdin;
16 int stdout;
17 int close_me[2];
18};
19
20static void pre_exec(void *d)
21{
22 struct dog_data *data = d;
23
24 dup2(data->stdin, 0);
25 dup2(data->stdout, 1);
26 dup2(data->stdout, 2);
27 os_close_file(data->stdin);
28 os_close_file(data->stdout);
29 os_close_file(data->close_me[0]);
30 os_close_file(data->close_me[1]);
31}
32
33int start_watchdog(int *in_fd_ret, int *out_fd_ret, char *sock)
34{
35 struct dog_data data;
36 int in_fds[2], out_fds[2], pid, n, err;
37 char pid_buf[sizeof("nnnnn\0")], c;
38 char *pid_args[] = { "/usr/bin/uml_watchdog", "-pid", pid_buf, NULL };
Jeff Dike5bbcbec2007-02-10 01:43:58 -080039 char *mconsole_args[] = { "/usr/bin/uml_watchdog", "-mconsole", NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 NULL };
41 char **args = NULL;
42
43 err = os_pipe(in_fds, 1, 0);
44 if(err < 0){
45 printk("harddog_open - os_pipe failed, err = %d\n", -err);
46 goto out;
47 }
48
49 err = os_pipe(out_fds, 1, 0);
50 if(err < 0){
51 printk("harddog_open - os_pipe failed, err = %d\n", -err);
52 goto out_close_in;
53 }
54
55 data.stdin = out_fds[0];
56 data.stdout = in_fds[1];
57 data.close_me[0] = out_fds[1];
58 data.close_me[1] = in_fds[0];
59
60 if(sock != NULL){
61 mconsole_args[2] = sock;
62 args = mconsole_args;
63 }
64 else {
65 /* XXX The os_getpid() is not SMP correct */
Jeff Dike6aa802c2007-10-16 01:26:56 -070066 sprintf(pid_buf, "%d", os_getpid());
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 args = pid_args;
68 }
69
Jeff Dikec4399012007-07-15 23:38:56 -070070 pid = run_helper(pre_exec, &data, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 os_close_file(out_fds[0]);
73 os_close_file(in_fds[1]);
74
75 if(pid < 0){
76 err = -pid;
77 printk("harddog_open - run_helper failed, errno = %d\n", -err);
78 goto out_close_out;
79 }
80
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070081 n = os_read_file(in_fds[0], &c, sizeof(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if(n == 0){
83 printk("harddog_open - EOF on watchdog pipe\n");
84 helper_wait(pid);
85 err = -EIO;
86 goto out_close_out;
87 }
88 else if(n < 0){
89 printk("harddog_open - read of watchdog pipe failed, "
90 "err = %d\n", -n);
91 helper_wait(pid);
92 err = n;
93 goto out_close_out;
94 }
95 *in_fd_ret = in_fds[0];
96 *out_fd_ret = out_fds[1];
Jeff Dike5bbcbec2007-02-10 01:43:58 -080097 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 out_close_in:
100 os_close_file(in_fds[0]);
101 os_close_file(in_fds[1]);
102 out_close_out:
103 os_close_file(out_fds[0]);
104 os_close_file(out_fds[1]);
105 out:
Jeff Dike5bbcbec2007-02-10 01:43:58 -0800106 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109void stop_watchdog(int in_fd, int out_fd)
110{
111 os_close_file(in_fd);
112 os_close_file(out_fd);
113}
114
115int ping_watchdog(int fd)
116{
117 int n;
118 char c = '\n';
119
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700120 n = os_write_file(fd, &c, sizeof(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if(n != sizeof(c)){
122 printk("ping_watchdog - write failed, err = %d\n", -n);
123 if(n < 0)
Jeff Dike5bbcbec2007-02-10 01:43:58 -0800124 return n;
125 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127 return 1;
128
129}