Jeff Dike | 5bbcbec | 2007-02-10 01:43:58 -0800 | [diff] [blame] | 1 | /* |
Jeff Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 9 | #include <os.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
| 11 | struct dog_data { |
| 12 | int stdin; |
| 13 | int stdout; |
| 14 | int close_me[2]; |
| 15 | }; |
| 16 | |
| 17 | static 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 Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 24 | close(data->stdin); |
| 25 | close(data->stdout); |
| 26 | close(data->close_me[0]); |
| 27 | close(data->close_me[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | int 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 Weinberger | 39073db | 2011-07-25 17:12:55 -0700 | [diff] [blame] | 34 | char pid_buf[sizeof("nnnnnnn\0")], c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | char *pid_args[] = { "/usr/bin/uml_watchdog", "-pid", pid_buf, NULL }; |
Jeff Dike | 5bbcbec | 2007-02-10 01:43:58 -0800 | [diff] [blame] | 36 | char *mconsole_args[] = { "/usr/bin/uml_watchdog", "-mconsole", NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | NULL }; |
| 38 | char **args = NULL; |
| 39 | |
| 40 | err = os_pipe(in_fds, 1, 0); |
Jeff Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 41 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | printk("harddog_open - os_pipe failed, err = %d\n", -err); |
| 43 | goto out; |
| 44 | } |
| 45 | |
| 46 | err = os_pipe(out_fds, 1, 0); |
Jeff Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 47 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | 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 Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 57 | if (sock != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | mconsole_args[2] = sock; |
| 59 | args = mconsole_args; |
| 60 | } |
| 61 | else { |
| 62 | /* XXX The os_getpid() is not SMP correct */ |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 63 | sprintf(pid_buf, "%d", os_getpid()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | args = pid_args; |
| 65 | } |
| 66 | |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 67 | pid = run_helper(pre_exec, &data, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Jeff Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 69 | close(out_fds[0]); |
| 70 | close(in_fds[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Jeff Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 72 | if (pid < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | err = -pid; |
| 74 | printk("harddog_open - run_helper failed, errno = %d\n", -err); |
| 75 | goto out_close_out; |
| 76 | } |
| 77 | |
Jeff Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 78 | n = read(in_fds[0], &c, sizeof(c)); |
| 79 | if (n == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | printk("harddog_open - EOF on watchdog pipe\n"); |
Johann Felix Soden | e4d06b3 | 2008-02-23 15:23:49 -0800 | [diff] [blame] | 81 | helper_wait(pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | err = -EIO; |
| 83 | goto out_close_out; |
| 84 | } |
Jeff Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 85 | else if (n < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | printk("harddog_open - read of watchdog pipe failed, " |
Jeff Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 87 | "err = %d\n", errno); |
Johann Felix Soden | e4d06b3 | 2008-02-23 15:23:49 -0800 | [diff] [blame] | 88 | helper_wait(pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | err = n; |
| 90 | goto out_close_out; |
| 91 | } |
| 92 | *in_fd_ret = in_fds[0]; |
| 93 | *out_fd_ret = out_fds[1]; |
Jeff Dike | 5bbcbec | 2007-02-10 01:43:58 -0800 | [diff] [blame] | 94 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | out_close_in: |
Jeff Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 97 | close(in_fds[0]); |
| 98 | close(in_fds[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | out_close_out: |
Jeff Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 100 | close(out_fds[0]); |
| 101 | close(out_fds[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | out: |
Jeff Dike | 5bbcbec | 2007-02-10 01:43:58 -0800 | [diff] [blame] | 103 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void stop_watchdog(int in_fd, int out_fd) |
| 107 | { |
Jeff Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 108 | close(in_fd); |
| 109 | close(out_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int ping_watchdog(int fd) |
| 113 | { |
| 114 | int n; |
| 115 | char c = '\n'; |
| 116 | |
Jeff Dike | 8ca842c | 2007-10-16 01:27:08 -0700 | [diff] [blame] | 117 | 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 Dike | 5bbcbec | 2007-02-10 01:43:58 -0800 | [diff] [blame] | 122 | return n; |
| 123 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | return 1; |
| 126 | |
| 127 | } |