| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/cocoa/authorization_util.h" |
| 6 |
| 7 #include <sys/wait.h> |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/eintr_wrapper.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/string_util.h" |
| 14 |
| 15 namespace authorization_util { |
| 16 |
| 17 OSStatus ExecuteWithPrivilegesAndGetPID(AuthorizationRef authorization, |
| 18 const char* tool_path, |
| 19 AuthorizationFlags options, |
| 20 const char** arguments, |
| 21 FILE** pipe, |
| 22 pid_t* pid) { |
| 23 // pipe may be NULL, but this function needs one. In that case, use a local |
| 24 // pipe. |
| 25 FILE* local_pipe; |
| 26 FILE** pipe_pointer; |
| 27 if (pipe) { |
| 28 pipe_pointer = pipe; |
| 29 } else { |
| 30 pipe_pointer = &local_pipe; |
| 31 } |
| 32 |
| 33 // AuthorizationExecuteWithPrivileges wants |char* const*| for |arguments|, |
| 34 // but it doesn't actually modify the arguments, and that type is kind of |
| 35 // silly and callers probably aren't dealing with that. Put the cast here |
| 36 // to make things a little easier on callers. |
| 37 OSStatus status = AuthorizationExecuteWithPrivileges(authorization, |
| 38 tool_path, |
| 39 options, |
| 40 (char* const*)arguments, |
| 41 pipe_pointer); |
| 42 if (status != errAuthorizationSuccess) { |
| 43 return status; |
| 44 } |
| 45 |
| 46 int line_pid = -1; |
| 47 size_t line_length = 0; |
| 48 char* line_c = fgetln(*pipe_pointer, &line_length); |
| 49 if (line_c) { |
| 50 if (line_length > 0 && line_c[line_length - 1] == '\n') { |
| 51 // line_c + line_length is the start of the next line if there is one. |
| 52 // Back up one character. |
| 53 --line_length; |
| 54 } |
| 55 std::string line(line_c, line_length); |
| 56 if (!StringToInt(line, &line_pid)) { |
| 57 // StringToInt may have set line_pid to something, but if the conversion |
| 58 // was imperfect, use -1. |
| 59 LOG(ERROR) << "ExecuteWithPrivilegesAndGetPid: funny line: " << line; |
| 60 line_pid = -1; |
| 61 } |
| 62 } else { |
| 63 LOG(ERROR) << "ExecuteWithPrivilegesAndGetPid: no line"; |
| 64 } |
| 65 |
| 66 if (!pipe) { |
| 67 fclose(*pipe_pointer); |
| 68 } |
| 69 |
| 70 if (pid) { |
| 71 *pid = line_pid; |
| 72 } |
| 73 |
| 74 return status; |
| 75 } |
| 76 |
| 77 OSStatus ExecuteWithPrivilegesAndWait(AuthorizationRef authorization, |
| 78 const char* tool_path, |
| 79 AuthorizationFlags options, |
| 80 const char** arguments, |
| 81 FILE** pipe, |
| 82 int* exit_status) { |
| 83 pid_t pid; |
| 84 OSStatus status = ExecuteWithPrivilegesAndGetPID(authorization, |
| 85 tool_path, |
| 86 options, |
| 87 arguments, |
| 88 pipe, |
| 89 &pid); |
| 90 if (status != errAuthorizationSuccess) { |
| 91 return status; |
| 92 } |
| 93 |
| 94 // exit_status may be NULL, but this function needs it. In that case, use a |
| 95 // local version. |
| 96 int local_exit_status; |
| 97 int* exit_status_pointer; |
| 98 if (exit_status) { |
| 99 exit_status_pointer = exit_status; |
| 100 } else { |
| 101 exit_status_pointer = &local_exit_status; |
| 102 } |
| 103 |
| 104 if (pid != -1) { |
| 105 pid_t wait_result; |
| 106 HANDLE_EINTR(wait_result = waitpid(pid, exit_status_pointer, 0)); |
| 107 if (wait_result != pid) { |
| 108 PLOG(ERROR) << "waitpid"; |
| 109 *exit_status_pointer = -1; |
| 110 } |
| 111 } else { |
| 112 *exit_status_pointer = -1; |
| 113 } |
| 114 |
| 115 return status; |
| 116 } |
| 117 |
| 118 } // namespace authorization_util |
| OLD | NEW |