void OSSim::setPriority(Pid_t pid, int32_t newPrio) { ProcessId *proc = ProcessId::getProcessId(pid); I(proc); int32_t oldPrio=proc->getPriority(); if(newPrio==oldPrio) return; // Set the new priority of the process ProcessId *otherProc=proc->setPriority(newPrio); if(newPrio>oldPrio) { // Priority is better now, check if still running if(proc->getState()==RunningState) { // Is there a process we need to swap with if(otherProc) { // Get the cpu where the demoted process is running CPU_t cpu=proc->getCPU(); // Switch the demoted process out cpus.switchOut(cpu,proc); // Switch the new process in cpus.switchIn(cpu,otherProc); } } } else { // Priority is worse now, check if ready but not already running if(proc->getState()==ReadyState) { // Is there a process we need to swap with if(otherProc) { // Get the cpu where the other process is running CPU_t cpu=otherProc->getCPU(); // Switch the victim process out cpus.switchOut(cpu,otherProc); // Switch the promoted process in cpus.switchIn(cpu,proc); } } } }
void OSSim::eventSetPPid(Pid_t pid, Pid_t ppid) { ProcessId *proc = ProcessId::getProcessId(pid); I(proc); proc->setPPid(ppid); }
Pid_t OSSim::eventGetPPid(Pid_t pid) { ProcessId *proc = ProcessId::getProcessId(pid); I(proc); return proc->getPPid(); }
inline void kill_wrapper( ProcessId pid, int sig, int port, const BSONObj& opt ) { #ifdef _WIN32 if (sig == SIGKILL || port == 0) { verify( registry._handles.count(pid) ); TerminateProcess(registry._handles[pid], 1); // returns failure for "zombie" processes. return; } std::string eventName = getShutdownSignalName(pid.asUInt32()); HANDLE event = OpenEventA(EVENT_MODIFY_STATE, FALSE, eventName.c_str()); if (event == NULL) { int gle = GetLastError(); if (gle != ERROR_FILE_NOT_FOUND) { warning() << "kill_wrapper OpenEvent failed: " << errnoWithDescription(); } else { log() << "kill_wrapper OpenEvent failed to open event to the process " << pid.asUInt32() << ". It has likely died already or server is running an older version." << " Attempting to shutdown through admin command."; // Back-off to the old way of shutting down the server on Windows, in case we // are managing a pre-2.6.0rc0 service, which did not have the event. // try { DBClientConnection conn; conn.connect("127.0.0.1:" + BSONObjBuilder::numStr(port)); BSONElement authObj = opt["auth"]; if (!authObj.eoo()){ string errMsg; conn.auth("admin", authObj["user"].String(), authObj["pwd"].String(), errMsg); if (!errMsg.empty()) { cout << "Failed to authenticate before shutdown: " << errMsg << endl; } } BSONObj info; BSONObjBuilder b; b.append("shutdown", 1); b.append("force", 1); conn.runCommand("admin", b.done(), info); } catch (...) { // Do nothing. This command never returns data to the client and the driver // doesn't like that. // } } return; } ON_BLOCK_EXIT(CloseHandle, event); bool result = SetEvent(event); if (!result) { error() << "kill_wrapper SetEvent failed: " << errnoWithDescription(); return; } #else int x = kill( pid.toNative(), sig ); if ( x ) { if ( errno == ESRCH ) { } else { log() << "killFailed: " << errnoWithDescription() << endl; verify( x == 0 ); } } #endif }
LinuxProc(ProcessId pid) { char name[128]; sprintf(name, "/proc/%d/stat", pid.asUInt32()); FILE* f = fopen(name, "r"); if (!f) { stringstream ss; ss << "couldn't open [" << name << "] " << errnoWithDescription(); string s = ss.str(); msgassertedNoTrace(13538, s.c_str()); } int found = fscanf(f, "%d %127s %c " "%d %d %d %d %d " "%lu %lu %lu %lu %lu " "%lu %lu %ld %ld " /* utime stime cutime cstime */ "%ld %ld " "%ld " "%ld " "%lu " /* start_time */ "%lu " "%ld " // rss "%lu %" KLF "u %" KLF "u %" KLF "u %" KLF "u %" KLF "u " /* "%*s %*s %*s %*s " "%"KLF"u %*lu %*lu " "%d %d " "%lu %lu" */ , &_pid, _comm, &_state, &_ppid, &_pgrp, &_session, &_tty, &_tpgid, &_flags, &_min_flt, &_cmin_flt, &_maj_flt, &_cmaj_flt, &_utime, &_stime, &_cutime, &_cstime, &_priority, &_nice, &_nlwp, &_alarm, &_start_time, &_vsize, &_rss, &_rss_rlim, &_start_code, &_end_code, &_start_stack, &_kstk_esp, &_kstk_eip /* &_wchan, &_exit_signal, &_processor, &_rtprio, &_sched */ ); if (found == 0) { cout << "system error: reading proc info" << endl; } fclose(f); }
int util_create_lock_file(const char *lockFileName, bool abortDuplicates) { int result = 0; FILE *fp = safe_fopen_wrapper_follow( lockFileName, "w" ); if ( fp == NULL ) { debug_printf( DEBUG_QUIET, "ERROR: could not open lock file %s for writing.\n", lockFileName); result = -1; } // // Create the ProcessId object. // ProcessId *procId = NULL; if ( result == 0 && abortDuplicates ) { int status; int precision_range = 1; if ( ProcAPI::createProcessId( daemonCore->getpid(), procId, status, &precision_range ) != PROCAPI_SUCCESS ) { debug_printf( DEBUG_QUIET, "ERROR: ProcAPI::createProcessId() " "failed; %d\n", status ); result = -1; } } // // Write out the ProcessId object. // if ( result == 0 && abortDuplicates ) { if ( procId->write( fp ) != ProcessId::SUCCESS ) { debug_printf( DEBUG_QUIET, "ERROR: ProcessId::write() failed\n"); result = -1; } } // // Sleep to ensure uniqueness of the ProcessId object. // if ( result == 0 && abortDuplicates ) { const int maxSleepTime = 60; // seconds; arbitrarily chosen int sleepTime = procId->computeWaitTime(); if ( sleepTime > maxSleepTime ) { debug_printf( DEBUG_QUIET, "Warning: ProcessId computed sleep " "time (%d) exceeds maximum (%d); skipping sleep/" "confirm step\n", sleepTime, maxSleepTime ); check_warning_strictness( DAG_STRICT_3 ); } else { debug_printf( DEBUG_NORMAL, "Sleeping for %d seconds to " "ensure ProcessId uniqueness\n", sleepTime ); #if defined(WIN32) sleep( sleepTime ); #else while( (sleepTime = sleep( sleepTime ) ) != 0 ) { } #endif // // Confirm the ProcessId object's uniqueness. // int status; if ( ProcAPI::confirmProcessId( *procId, status ) != PROCAPI_SUCCESS ) { debug_printf( DEBUG_QUIET, "Warning: ProcAPI::" "confirmProcessId() failed; %d\n", status ); check_warning_strictness( DAG_STRICT_3 ); } else { if ( !procId->isConfirmed() ) { debug_printf( DEBUG_QUIET, "Warning: ProcessId not " "confirmed unique\n" ); check_warning_strictness( DAG_STRICT_3 ); } else { // // Write out the confirmation. // if ( procId->writeConfirmationOnly( fp ) != ProcessId::SUCCESS ) { debug_printf( DEBUG_QUIET, "ERROR: ProcessId::" "writeConfirmationOnly() failed\n"); result = -1; } } } } } delete procId; if ( fp != NULL ) { if ( fclose( fp ) != 0 ) { debug_printf( DEBUG_QUIET, "ERROR: closing lock " "file failed with errno %d (%s)\n", errno, strerror( errno ) ); } } return result; }
int util_check_lock_file(const char *lockFileName) { int result = 0; FILE *fp = safe_fopen_wrapper_follow( lockFileName, "r" ); if ( fp == NULL ) { debug_printf( DEBUG_QUIET, "ERROR: could not open lock file %s for reading.\n", lockFileName ); result = -1; } ProcessId *procId = NULL; if ( result != -1 ) { int status; procId = new ProcessId( fp, status ); if ( status != ProcessId::SUCCESS ) { debug_printf( DEBUG_QUIET, "ERROR: unable to create ProcessId " "object from lock file %s\n", lockFileName ); result = -1; } } if ( result != -1 ) { int status; int aliveResult = ProcAPI::isAlive( *procId, status ); if ( aliveResult != PROCAPI_SUCCESS ) { debug_printf( DEBUG_QUIET, "ERROR: failed to determine " "whether DAGMan that wrote lock file is alive\n" ); result = -1; } else { if ( status == PROCAPI_ALIVE ) { debug_printf( DEBUG_NORMAL, "Duplicate DAGMan PID %d is alive; this DAGMan " "should abort.\n", procId->getPid() ); result = 1; } else if ( status == PROCAPI_DEAD ) { debug_printf( DEBUG_NORMAL, "Duplicate DAGMan PID %d is no longer alive; " "this DAGMan should continue.\n", procId->getPid() ); result = 0; } else if ( status == PROCAPI_UNCERTAIN ) { debug_printf( DEBUG_NORMAL, "Duplicate DAGMan PID %d *may* be alive; this " "DAGMan is continuing, but this will cause " "problems if the duplicate DAGMan is alive.\n", procId->getPid() ); result = 0; } else { EXCEPT( "Illegal ProcAPI::isAlive() status value: %d", status ); } } } delete procId; if ( fp != NULL ) { if ( fclose( fp ) != 0 ) { debug_printf( DEBUG_QUIET, "ERROR: closing lock " "file failed with errno %d (%s)\n", errno, strerror( errno ) ); } } return result; }