blob: f99b32a4dbff57e1a539acadfc335f218a007212 [file] [log] [blame]
Jeff Dike5bbcbec2007-02-10 01:43:58 -08001/*
Jeff Dike8ca842c2007-10-16 01:27:08 -07002 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
6#include <stdio.h>
7#include <unistd.h>
8#include <errno.h>
Al Viro37185b32012-10-08 03:27:32 +01009#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11struct dog_data {
12 int stdin;
13 int stdout;
14 int close_me[2];
15};
16
17static void pre_exec(void *d)
18{
19 struct dog_data *data = d;
20
21 dup2(data->stdin, 0);
22 dup2(data->stdout, 1);
23 dup2(data->stdout, 2);
Jeff Dike8ca842c2007-10-16 01:27:08 -070024 close(data->stdin);
25 close(data->stdout);
26 close(data->close_me[0]);
27 close(data->close_me[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
29
30int start_watchdog(int *in_fd_ret, int *out_fd_ret, char *sock)
31{
32 struct dog_data data;
33 int in_fds[2], out_fds[2], pid, n, err;
Richard Weinberger39073db2011-07-25 17:12:55 -070034 char pid_buf[sizeof("nnnnnnn\0")], c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 char *pid_args[] = { "/usr/bin/uml_watchdog", "-pid", pid_buf, NULL };
Jeff Dike5bbcbec2007-02-10 01:43:58 -080036 char *mconsole_args[] = { "/usr/bin/uml_watchdog", "-mconsole", NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 NULL };
38 char **args = NULL;
39
40 err = os_pipe(in_fds, 1, 0);
Jeff Dike8ca842c2007-10-16 01:27:08 -070041 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 printk("harddog_open - os_pipe failed, err = %d\n", -err);
43 goto out;
44 }
45
46 err = os_pipe(out_fds, 1, 0);
Jeff Dike8ca842c2007-10-16 01:27:08 -070047 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 printk("harddog_open - os_pipe failed, err = %d\n", -err);
49 goto out_close_in;
50 }
51
52 data.stdin = out_fds[0];
53 data.stdout = in_fds[1];
54 data.close_me[0] = out_fds[1];
55 data.close_me[1] = in_fds[0];
56
Jeff Dike8ca842c2007-10-16 01:27:08 -070057 if (sock != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 mconsole_args[2] = sock;
59 args = mconsole_args;
60 }
61 else {
62 /* XXX The os_getpid() is not SMP correct */
Jeff Dike6aa802c2007-10-16 01:26:56 -070063 sprintf(pid_buf, "%d", os_getpid());
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 args = pid_args;
65 }
66
Jeff Dikec4399012007-07-15 23:38:56 -070067 pid = run_helper(pre_exec, &data, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Jeff Dike8ca842c2007-10-16 01:27:08 -070069 close(out_fds[0]);
70 close(in_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jeff Dike8ca842c2007-10-16 01:27:08 -070072 if (pid < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 err = -pid;
74 printk("harddog_open - run_helper failed, errno = %d\n", -err);
75 goto out_close_out;
76 }
77
Jeff Dike8ca842c2007-10-16 01:27:08 -070078 n = read(in_fds[0], &c, sizeof(c));
79 if (n == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 printk("harddog_open - EOF on watchdog pipe\n");
Johann Felix Sodene4d06b32008-02-23 15:23:49 -080081 helper_wait(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 err = -EIO;
83 goto out_close_out;
84 }
Jeff Dike8ca842c2007-10-16 01:27:08 -070085 else if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 printk("harddog_open - read of watchdog pipe failed, "
Jeff Dike8ca842c2007-10-16 01:27:08 -070087 "err = %d\n", errno);
Johann Felix Sodene4d06b32008-02-23 15:23:49 -080088 helper_wait(pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 err = n;
90 goto out_close_out;
91 }
92 *in_fd_ret = in_fds[0];
93 *out_fd_ret = out_fds[1];
Jeff Dike5bbcbec2007-02-10 01:43:58 -080094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 out_close_in:
Jeff Dike8ca842c2007-10-16 01:27:08 -070097 close(in_fds[0]);
98 close(in_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 out_close_out:
Jeff Dike8ca842c2007-10-16 01:27:08 -0700100 close(out_fds[0]);
101 close(out_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 out:
Jeff Dike5bbcbec2007-02-10 01:43:58 -0800103 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106void stop_watchdog(int in_fd, int out_fd)
107{
Jeff Dike8ca842c2007-10-16 01:27:08 -0700108 close(in_fd);
109 close(out_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112int ping_watchdog(int fd)
113{
114 int n;
115 char c = '\n';
116
Jeff Dike8ca842c2007-10-16 01:27:08 -0700117 n = write(fd, &c, sizeof(c));
118 if (n != sizeof(c)) {
119 printk("ping_watchdog - write failed, ret = %d, err = %d\n",
120 n, errno);
121 if (n < 0)
Jeff Dike5bbcbec2007-02-10 01:43:58 -0800122 return n;
123 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125 return 1;
126
127}