Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009-2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This class is most definitely NOT re-entrant. | 5 // This class is most definitely NOT re-entrant. |
| 6 | 6 |
| 7 #include "login_manager/child_job.h" | 7 #include "login_manager/child_job.h" |
| 8 | 8 |
| 9 #include <errno.h> | 9 #include <errno.h> |
| 10 #include <grp.h> | 10 #include <grp.h> |
| 11 #include <pwd.h> | 11 #include <pwd.h> |
| 12 #include <signal.h> | 12 #include <signal.h> |
| 13 #include <stdlib.h> | 13 #include <stdlib.h> |
| 14 #include <time.h> | |
| 14 #include <unistd.h> | 15 #include <unistd.h> |
| 15 | 16 |
| 16 #include <base/basictypes.h> | 17 #include <base/basictypes.h> |
| 17 #include <base/command_line.h> | 18 #include <base/command_line.h> |
| 18 #include <base/logging.h> | 19 #include <base/logging.h> |
| 19 #include <base/string_util.h> | 20 #include <base/string_util.h> |
| 20 | 21 |
| 21 namespace login_manager { | 22 namespace login_manager { |
| 22 // static | 23 // static |
| 23 const char SetUidExecJob::kLoginManagerFlag[] = "--login-manager"; | 24 const char SetUidExecJob::kLoginManagerFlag[] = "--login-manager"; |
| 24 | 25 |
| 25 // static | 26 // static |
| 26 const int SetUidExecJob::kCantSetuid = 127; | 27 const int SetUidExecJob::kCantSetuid = 127; |
| 27 const int SetUidExecJob::kCantSetgid = 128; | 28 const int SetUidExecJob::kCantSetgid = 128; |
| 28 const int SetUidExecJob::kCantSetgroups = 129; | 29 const int SetUidExecJob::kCantSetgroups = 129; |
| 29 const int SetUidExecJob::kCantExec = 255; | 30 const int SetUidExecJob::kCantExec = 255; |
| 30 | 31 |
| 31 SetUidExecJob::SetUidExecJob(const CommandLine* command_line, | 32 SetUidExecJob::SetUidExecJob(const CommandLine* command_line, |
| 32 FileChecker* checker, // Takes ownership. | 33 FileChecker* checker, // Takes ownership. |
| 33 const bool add_flag) | 34 const bool add_flag) |
| 34 : checker_(checker), | 35 : checker_(checker), |
| 35 argv_(NULL), | 36 argv_(NULL), |
| 36 num_args_passed_in_(0), | 37 num_args_passed_in_(0), |
| 37 desired_uid_(0), | 38 desired_uid_(0), |
| 38 include_login_flag_(add_flag), | 39 include_login_flag_(add_flag), |
| 39 set_uid_(false) { | 40 set_uid_(false), |
| 41 last_start_(0) { | |
| 40 PopulateArgv(command_line); | 42 PopulateArgv(command_line); |
| 41 } | 43 } |
| 42 | 44 |
| 43 SetUidExecJob::~SetUidExecJob() { | 45 SetUidExecJob::~SetUidExecJob() { |
| 44 if (argv_) { | 46 if (argv_) { |
| 45 // free the strings we copied from the passed-in args. | 47 // free the strings we copied from the passed-in args. |
| 46 for (uint32 i = 0; i < num_args_passed_in_; i++) { | 48 for (uint32 i = 0; i < num_args_passed_in_; i++) { |
| 47 if (argv_[i]) | 49 if (argv_[i]) |
| 48 delete [] argv_[i]; | 50 delete [] argv_[i]; |
| 49 } | 51 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 } | 106 } |
| 105 if (setsid() == -1) | 107 if (setsid() == -1) |
| 106 LOG(ERROR) << "can't setsid: " << strerror(errno); | 108 LOG(ERROR) << "can't setsid: " << strerror(errno); |
| 107 return to_return; | 109 return to_return; |
| 108 } | 110 } |
| 109 | 111 |
| 110 bool SetUidExecJob::ShouldRun() { | 112 bool SetUidExecJob::ShouldRun() { |
| 111 return !checker_->exists(); | 113 return !checker_->exists(); |
| 112 } | 114 } |
| 113 | 115 |
| 116 bool SetUidExecJob::ShouldStop() { | |
| 117 return (time(NULL) - last_start_ < 1); | |
|
DaveMoore
2010/02/27 00:56:17
Nit: could you make this a constant?
| |
| 118 } | |
| 119 | |
| 120 void SetUidExecJob::RecordTime() { | |
| 121 time(&last_start_); | |
| 122 } | |
| 123 | |
| 114 void SetUidExecJob::Run() { | 124 void SetUidExecJob::Run() { |
| 115 UseLoginManagerFlagIfNeeded(); | 125 UseLoginManagerFlagIfNeeded(); |
| 116 // We try to set our UID/GID to the desired UID, and then exec | 126 // We try to set our UID/GID to the desired UID, and then exec |
| 117 // the command passed in. | 127 // the command passed in. |
| 118 int exit_code = SetIDs(); | 128 int exit_code = SetIDs(); |
| 119 if (exit_code) | 129 if (exit_code) |
| 120 exit(exit_code); | 130 exit(exit_code); |
| 121 | 131 |
| 122 execv(argv_[0], const_cast<char * const*>(argv_)); | 132 execv(argv_[0], const_cast<char * const*>(argv_)); |
| 123 | 133 |
| 124 // Should never get here, unless we couldn't exec the command. | 134 // Should never get here, unless we couldn't exec the command. |
| 125 LOG(ERROR) << strerror(errno); | 135 LOG(ERROR) << strerror(errno); |
| 126 exit(kCantExec); | 136 exit(kCantExec); |
| 127 } | 137 } |
| 128 | 138 |
| 129 } // namespace login_manager | 139 } // namespace login_manager |
| OLD | NEW |