Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 1 | /* |
Bill Richardson | 6f39615 | 2014-07-15 12:52:19 -0700 | [diff] [blame] | 2 | * Copyright 2013 The Chromium OS Authors. All rights reserved. |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <limits.h> |
| 10 | #include <stdint.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <unistd.h> |
| 16 | |
| 17 | #include "futility.h" |
| 18 | |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 19 | |
| 20 | /******************************************************************************/ |
| 21 | /* Logging stuff */ |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 22 | |
| 23 | /* File to use for logging, if present */ |
| 24 | #define LOGFILE "/tmp/futility.log" |
| 25 | |
| 26 | /* Normally logging will only happen if the logfile already exists. Uncomment |
| 27 | * this to force log file creation (and thus logging) always. */ |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 28 | |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 29 | /* #define FORCE_LOGGING_ON */ |
| 30 | |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 31 | static int log_fd = -1; |
| 32 | |
| 33 | /* Write the string and a newline. Silently give up on errors */ |
Bill Richardson | ee53d65 | 2014-09-04 16:18:15 -0700 | [diff] [blame] | 34 | static void log_str(char *prefix, char *str) |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 35 | { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 36 | int len, done, n; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 37 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 38 | if (log_fd < 0) |
| 39 | return; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 40 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 41 | if (!str) |
| 42 | str = "(NULL)"; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 43 | |
Bill Richardson | ee53d65 | 2014-09-04 16:18:15 -0700 | [diff] [blame] | 44 | if (prefix && *prefix) { |
| 45 | len = strlen(prefix); |
| 46 | for (done = 0; done < len; done += n) { |
| 47 | n = write(log_fd, prefix + done, len - done); |
| 48 | if (n < 0) |
| 49 | return; |
| 50 | } |
| 51 | } |
| 52 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 53 | len = strlen(str); |
| 54 | if (len == 0) { |
| 55 | str = "(EMPTY)"; |
| 56 | len = strlen(str); |
| 57 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 58 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 59 | for (done = 0; done < len; done += n) { |
| 60 | n = write(log_fd, str + done, len - done); |
| 61 | if (n < 0) |
| 62 | return; |
| 63 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 64 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 65 | if (write(log_fd, "\n", 1) < 0) |
| 66 | return; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static void log_close(void) |
| 70 | { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 71 | struct flock lock; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 72 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 73 | if (log_fd >= 0) { |
| 74 | memset(&lock, 0, sizeof(lock)); |
| 75 | lock.l_type = F_UNLCK; |
| 76 | lock.l_whence = SEEK_SET; |
| 77 | if (fcntl(log_fd, F_SETLKW, &lock)) |
| 78 | perror("Unable to unlock log file"); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 79 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 80 | close(log_fd); |
| 81 | log_fd = -1; |
| 82 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static void log_open(void) |
| 86 | { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 87 | struct flock lock; |
| 88 | int ret; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 89 | |
| 90 | #ifdef FORCE_LOGGING_ON |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 91 | log_fd = open(LOGFILE, O_WRONLY | O_APPEND | O_CREAT, 0666); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 92 | #else |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 93 | log_fd = open(LOGFILE, O_WRONLY | O_APPEND); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 94 | #endif |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 95 | if (log_fd < 0) { |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 96 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 97 | if (errno != EACCES) |
| 98 | return; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 99 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 100 | /* Permission problems should improve shortly ... */ |
| 101 | sleep(1); |
| 102 | log_fd = open(LOGFILE, O_WRONLY | O_APPEND | O_CREAT, 0666); |
| 103 | if (log_fd < 0) /* Nope, they didn't */ |
| 104 | return; |
| 105 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 106 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 107 | /* Let anyone have a turn */ |
| 108 | fchmod(log_fd, 0666); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 109 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 110 | /* But only one at a time */ |
| 111 | memset(&lock, 0, sizeof(lock)); |
| 112 | lock.l_type = F_WRLCK; |
| 113 | lock.l_whence = SEEK_END; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 114 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 115 | ret = fcntl(log_fd, F_SETLKW, &lock); /* this blocks */ |
| 116 | if (ret < 0) |
| 117 | log_close(); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 120 | static void log_args(int argc, char *argv[]) |
| 121 | { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 122 | int i; |
| 123 | ssize_t r; |
| 124 | pid_t parent; |
| 125 | char buf[80]; |
Bill Richardson | ee53d65 | 2014-09-04 16:18:15 -0700 | [diff] [blame] | 126 | FILE *fp; |
| 127 | char caller_buf[PATH_MAX]; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 128 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 129 | log_open(); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 130 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 131 | /* delimiter */ |
Bill Richardson | 6df3e33 | 2014-10-02 18:50:33 -0700 | [diff] [blame] | 132 | log_str(NULL, "##### LOG #####"); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 133 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 134 | /* Can we tell who called us? */ |
| 135 | parent = getppid(); |
| 136 | snprintf(buf, sizeof(buf), "/proc/%d/exe", parent); |
Bill Richardson | ee53d65 | 2014-09-04 16:18:15 -0700 | [diff] [blame] | 137 | r = readlink(buf, caller_buf, sizeof(caller_buf) - 1); |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 138 | if (r >= 0) { |
Bill Richardson | ee53d65 | 2014-09-04 16:18:15 -0700 | [diff] [blame] | 139 | caller_buf[r] = '\0'; |
| 140 | log_str("CALLER:", caller_buf); |
| 141 | } |
| 142 | |
| 143 | /* From where? */ |
| 144 | snprintf(buf, sizeof(buf), "/proc/%d/cwd", parent); |
| 145 | r = readlink(buf, caller_buf, sizeof(caller_buf) - 1); |
| 146 | if (r >= 0) { |
| 147 | caller_buf[r] = '\0'; |
| 148 | log_str("DIR:", caller_buf); |
| 149 | } |
| 150 | |
| 151 | /* And maybe the args? */ |
| 152 | snprintf(buf, sizeof(buf), "/proc/%d/cmdline", parent); |
| 153 | fp = fopen(buf, "r"); |
| 154 | if (fp) { |
| 155 | memset(caller_buf, 0, sizeof(caller_buf)); |
| 156 | r = fread(caller_buf, 1, sizeof(caller_buf) - 1, fp); |
| 157 | if (r > 0) { |
| 158 | char *s = caller_buf; |
| 159 | for (i = 0; i < r && *s; ) { |
| 160 | log_str("CMDLINE:", s); |
| 161 | while (i < r && *s) |
| 162 | i++, s++; |
| 163 | i++, s++; |
| 164 | } |
| 165 | } |
| 166 | fclose(fp); |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 167 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 168 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 169 | /* Now log the stuff about ourselves */ |
| 170 | for (i = 0; i < argc; i++) |
Bill Richardson | ee53d65 | 2014-09-04 16:18:15 -0700 | [diff] [blame] | 171 | log_str(NULL, argv[i]); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 172 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 173 | log_close(); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 176 | /******************************************************************************/ |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 177 | |
| 178 | static const char *const usage = "\n" |
| 179 | "Usage: " MYNAME " COMMAND [args...]\n" |
| 180 | "\n" |
| 181 | "This is the unified firmware utility, which will eventually replace\n" |
| 182 | "most of the distinct verified boot tools formerly produced by the\n" |
| 183 | "vboot_reference package.\n" |
| 184 | "\n" |
| 185 | "When symlinked under the name of one of those previous tools, it should\n" |
| 186 | "fully implement the original behavior. It can also be invoked directly\n" |
| 187 | "as " MYNAME ", followed by the original name as the first argument.\n" |
| 188 | "\n"; |
| 189 | |
| 190 | static void print_help(const char *cmd) |
| 191 | { |
| 192 | puts(usage); |
| 193 | } |
| 194 | |
| 195 | static const struct futil_cmd_t *find_command(const char *name) |
| 196 | { |
| 197 | const struct futil_cmd_t *const *cmd; |
| 198 | |
| 199 | for (cmd = futil_cmds; *cmd; cmd++) |
| 200 | if (0 == strcmp((*cmd)->name, name)) |
| 201 | return *cmd; |
| 202 | |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | static void list_commands(void) |
| 207 | { |
| 208 | const struct futil_cmd_t *const *cmd; |
| 209 | |
| 210 | for (cmd = futil_cmds; *cmd; cmd++) |
| 211 | printf(" %-20s %s\n", (*cmd)->name, (*cmd)->shorthelp); |
| 212 | } |
| 213 | |
| 214 | static int do_help(int argc, char *argv[]) |
| 215 | { |
| 216 | const struct futil_cmd_t *cmd; |
| 217 | |
| 218 | if (argc >= 2) { |
| 219 | cmd = find_command(argv[1]); |
| 220 | if (cmd) { |
| 221 | printf("\n%s - %s\n", argv[1], cmd->shorthelp); |
| 222 | cmd->longhelp(argv[1]); |
| 223 | return 0; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | fputs(usage, stdout); |
| 228 | |
| 229 | printf("The following commands are built-in:\n\n"); |
| 230 | list_commands(); |
| 231 | printf("\nUse \"" MYNAME " help COMMAND\" for more information.\n\n"); |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | DECLARE_FUTIL_COMMAND(help, do_help, |
| 237 | "Show a bit of help (you're looking at it)", |
| 238 | print_help); |
| 239 | |
| 240 | /* |
| 241 | * These are built-in functions that we'd like to abandon completely someday. |
| 242 | * TODO: If no one complains, get rid of them. |
| 243 | */ |
| 244 | static const char *const dep_cmds[] = { |
| 245 | "dev_sign_file", |
| 246 | }; |
| 247 | |
| 248 | static const char *const dep_usage = "\n" |
| 249 | "The program \"%s\" is deprecated and may go away soon.\n" |
| 250 | "\n" |
| 251 | "If you feel this is in error, please open a bug at\n" |
| 252 | "\n" |
| 253 | " http://dev.chromium.org/for-testers/bug-reporting-guidelines\n" |
| 254 | "\n" |
| 255 | "In the meantime, you may continue to use the program by invoking it as\n" |
| 256 | "\n" MYNAME " %s [...]\n" |
| 257 | "\n"; |
| 258 | |
Bill Richardson | 3638625 | 2014-10-14 20:58:41 -0700 | [diff] [blame] | 259 | static int deprecated(const char *depname) |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 260 | { |
| 261 | fprintf(stderr, dep_usage, depname, depname); |
Bill Richardson | 3638625 | 2014-10-14 20:58:41 -0700 | [diff] [blame] | 262 | return 1; |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Bill Richardson | 3638625 | 2014-10-14 20:58:41 -0700 | [diff] [blame] | 265 | int run_command(const struct futil_cmd_t *cmd, int argc, char *argv[]) |
| 266 | { |
| 267 | /* Handle the "CMD --help" case ourselves */ |
| 268 | if (2 == argc && 0 == strcmp(argv[1], "--help")) { |
| 269 | char *fake_argv[] = {"help", |
| 270 | (char *)cmd->name, |
| 271 | NULL}; |
| 272 | return do_help(2, fake_argv); |
| 273 | } |
| 274 | |
| 275 | return cmd->handler(argc, argv); |
| 276 | } |
| 277 | |
| 278 | static char *simple_basename(char *str) |
| 279 | { |
| 280 | char *s = strrchr(str, '/'); |
| 281 | if (s) |
| 282 | s++; |
| 283 | else |
| 284 | s = str; |
| 285 | return s; |
| 286 | } |
| 287 | |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 288 | /* Here we go */ |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 289 | int main(int argc, char *argv[], char *envp[]) |
| 290 | { |
Bill Richardson | 3638625 | 2014-10-14 20:58:41 -0700 | [diff] [blame] | 291 | char *progname; |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 292 | const struct futil_cmd_t *cmd; |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 293 | int i; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 294 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 295 | log_args(argc, argv); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 296 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 297 | /* How were we invoked? */ |
Bill Richardson | 3638625 | 2014-10-14 20:58:41 -0700 | [diff] [blame] | 298 | progname = simple_basename(argv[0]); |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 299 | |
Bill Richardson | 3638625 | 2014-10-14 20:58:41 -0700 | [diff] [blame] | 300 | /* See if the program name is a command we recognize */ |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 301 | cmd = find_command(progname); |
| 302 | if (cmd) { |
Bill Richardson | 3638625 | 2014-10-14 20:58:41 -0700 | [diff] [blame] | 303 | /* Block any deprecated functions invoked directly. */ |
| 304 | for (i = 0; i < ARRAY_SIZE(dep_cmds); i++) |
| 305 | if (0 == strcmp(dep_cmds[i], progname)) |
| 306 | return deprecated(progname); |
| 307 | |
| 308 | return run_command(cmd, argc, argv); |
Bill Richardson | 779796f | 2014-09-23 11:47:40 -0700 | [diff] [blame] | 309 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 310 | |
Bill Richardson | 3638625 | 2014-10-14 20:58:41 -0700 | [diff] [blame] | 311 | /* The program name means nothing, so we require an argument. */ |
| 312 | if (argc < 2) { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 313 | do_help(0, 0); |
Bill Richardson | 3638625 | 2014-10-14 20:58:41 -0700 | [diff] [blame] | 314 | return 1; |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 315 | } |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 316 | |
Bill Richardson | 3638625 | 2014-10-14 20:58:41 -0700 | [diff] [blame] | 317 | /* The first arg should be a command we recognize */ |
| 318 | argc--; |
| 319 | argv++; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 320 | |
Bill Richardson | 3638625 | 2014-10-14 20:58:41 -0700 | [diff] [blame] | 321 | /* For reasons I've forgotten, treat /blah/blah/CMD the same as CMD */ |
| 322 | progname = simple_basename(argv[0]); |
| 323 | /* Oh, and treat "--foo" the same as "foo" */ |
| 324 | while (*progname == '-') |
| 325 | progname++; |
| 326 | |
| 327 | /* Do we recognize the command? */ |
| 328 | cmd = find_command(progname); |
| 329 | if (cmd) |
| 330 | return run_command(cmd, argc, argv); |
| 331 | |
| 332 | /* Nope. We've no clue what we're being asked to do. */ |
| 333 | do_help(0, 0); |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 334 | return 1; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 335 | } |