int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: %s <num-processes>\n", argv[0]); return -1; } int num_processes = atoi(argv[1]); fprintf(stderr, "Creating %d %s\n", num_processes, num_processes == 1 ? "process" : "processes"); for (int i = 0; i < num_processes; i++) { pid_t pid; if ((pid = Fork()) == 0) { fprintf(stderr, "Child (%d) delaying\n", GetPid()); Delay(2 * i); fprintf(stderr, "Child (%d) exiting\n", GetPid()); Exit(100 + GetPid()); } else { fprintf(stderr, "Child (%d) forked\n", pid); } } while (true) { int status; pid_t pid = Wait(&status); if (pid == ERROR) { break; } else { fprintf(stderr, "PID %d exited with status %d\n", pid, status); } } fprintf(stderr, "parent PID %d is now exiting\n", GetPid()); Exit(0); }
/* PROGRAM: fibo_task This program will repeatedly receive synchronously a positive value k and compute the k-th fibonacci number. Each computed fibonacci number is sent to the "printer" message queue. It exits if it receives k==-1 */ int fibo_task(int argl, void* args) { unsigned int k,f; long msg; char *prbuf = malloc(1024); Pid_t pid; sprintf(prbuf,"Hello from fibo_task process %d.\n",GetPid()); print_string(prbuf); while(1) { // Wait for a request to compute a Fibonacci number. pid = ReceivePort(&msg, 1); assert(pid == GetPPid()); if(msg<0) { sprintf(prbuf, "fibo_task exiting (pid = %d). Bye!\n", GetPid()); print_string(prbuf); free(prbuf); Exit(0); /* Test Exit! */ } k = msg; sprintf(prbuf,"I will compute the %d-th Fibonacci number\n",k); print_string(prbuf); f = fibo(k); sprintf(prbuf,"The %d-th Fibonacci number is %d.\n",k,f); print_string(prbuf); } return 0; }
void PipedProcess::Terminate() { #ifdef __WXGTK__ wxString cmd; wxFileName exePath(wxStandardPaths::Get().GetExecutablePath()); wxFileName script(exePath.GetPath(), wxT("codelite_kill_children")); cmd << wxT("/bin/sh -f ") << script.GetFullPath() << wxT(" ") << GetPid(); wxExecute(cmd, wxEXEC_ASYNC); #else wxKillError rc; wxKill(GetPid(), wxSIGKILL, &rc, wxKILL_CHILDREN); #endif }
int main(int argc, char **argv) { int i; TracePrintf(-1, "In process %d\n", GetPid()); printf("In process %d\n", GetPid()); TracePrintf(-1, "%d arguments passed in\n", argc); printf("%d arguments passed in\n", argc); for (i = 0; i < argc; i++) { TracePrintf(-1, "\targ %d is %s\n", i, argv[i]); printf("\targ %d is %s\n", i, argv[i]); } return 0; }
void UnixProcessImpl::Terminate() { wxKill(GetPid(), GetHardKill() ? wxSIGKILL : wxSIGTERM, NULL, wxKILL_CHILDREN); int status(0); // The real cleanup is done inside ::ChildTerminatedSingalHandler() signal handler (app.cpp) waitpid(-1, &status, WNOHANG); }
TError TContext::CreateDaemonCgs() { DaemonCgs[memorySubsystem] = memorySubsystem->GetRootCgroup()->GetChild(PORTO_DAEMON_CGROUP); DaemonCgs[cpuacctSubsystem] = cpuacctSubsystem->GetRootCgroup()->GetChild(PORTO_DAEMON_CGROUP); for (auto cg : DaemonCgs) { TError error = cg.second->Create(); if (error) return error; // portod-slave error = cg.second->Attach(GetPid()); if (error) return error; // portod master error = cg.second->Attach(GetPPid()); if (error) return error; } if (!config().daemon().debug()) { TError error = memorySubsystem->SetLimit(DaemonCgs[memorySubsystem], config().daemon().memory_limit()); if (error) return error; } return TError::Success(); }
void PrintDriver(){ int i, code; char *p; msg_t msg; msg.recipient = 2; printing_semaphore = SemGet(0); outportb(LPT1_BASE+LPT_CONTROL, PC_SLCTIN); code = inportb(LPT1_BASE+LPT_STATUS); for(i=0; i<50; i++)IO_DELAY(); outportb(LPT1_BASE+LPT_CONTROL, PC_INIT|PC_SLCTIN|PC_IRQEN); Sleep(1); for(;;){ MsgRcv(&msg); cons_printf("PrintDriver (PID %d) now prints...\n",GetPid()); p = msg.data; while(*p){ SemWait(printing_semaphore); outportb(LPT1_BASE+LPT_DATA, *p); code = inportb(LPT1_BASE+LPT_CONTROL); outportb(LPT1_BASE+LPT_CONTROL, code|PC_STROBE); for(i=0; i<50; i++)IO_DELAY(); outportb(LPT1_BASE+LPT_CONTROL, code); p++; } } }
void Fuzzer::AlarmCallback() { assert(Options.UnitTimeoutSec > 0); if (!CurrentUnitSize) return; // We have not started running units yet. size_t Seconds = duration_cast<seconds>(system_clock::now() - UnitStartTime).count(); if (Seconds == 0) return; if (Options.Verbosity >= 2) Printf("AlarmCallback %zd\n", Seconds); if (Seconds >= (size_t)Options.UnitTimeoutSec) { Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds); Printf(" and the timeout value is %d (use -timeout=N to change)\n", Options.UnitTimeoutSec); if (CurrentUnitSize <= kMaxUnitSizeToPrint) { PrintHexArray(CurrentUnitData, CurrentUnitSize, "\n"); PrintASCII(CurrentUnitData, CurrentUnitSize, "\n"); } WriteUnitToFileWithPrefix( {CurrentUnitData, CurrentUnitData + CurrentUnitSize}, "timeout-"); Printf("==%d== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(), Seconds); if (__sanitizer_print_stack_trace) __sanitizer_print_stack_trace(); Printf("SUMMARY: libFuzzer: timeout\n"); if (Options.AbortOnTimeout) abort(); exit(Options.TimeoutExitCode); } }
void PipedProcessCtrl::KillProcess() { if(m_dead) return; // if(m_killlevel==0) //some process will complete if we send EOF. TODO: make sending EOF a separate option // { // m_proc->CloseOutput(); // m_killlevel=1; // return; // } long pid=GetPid(); #ifdef __WXGTK__ if(m_killlevel==0) { m_killlevel=1; if(wxProcess::Exists(pid)) wxProcess::Kill(pid,wxSIGTERM); return; } if(m_killlevel==1) { if(wxProcess::Exists(pid)) { wxProcess::Kill(pid,wxSIGKILL); } } #else if(wxProcess::Exists(pid)) { wxProcess::Kill(pid,wxSIGKILL); } #endif //__WXGTK__ }
bool WinProcessImpl::Read(wxString& buff) { DWORD le1(-1); DWORD le2(-1); buff.Clear(); if( !DoReadFromPipe(hChildStderrRdDup, buff) ) { le2 = GetLastError(); } if( !DoReadFromPipe(hChildStdoutRdDup, buff) ) { le1 = GetLastError(); } if( le1 == ERROR_NO_DATA && le2 == ERROR_NO_DATA) { if ( IsAlive() ) { wxThread::Sleep(15); return true; } } bool success = !buff.IsEmpty(); if(!success) { DWORD dwExitCode; if (GetExitCodeProcess(piProcInfo.hProcess, &dwExitCode)) { SetProcessExitCode(GetPid(), (int)dwExitCode); } } return success; }
void replace_jemalloc_stats(jemalloc_stats_t* aStats) { AutoLock lock(sLock); sFuncs->jemalloc_stats(aStats); FdPrintf(sFd, "%zu %zu jemalloc_stats()\n", GetPid(), GetTid()); }
pid_t fork() { pid_t ChildPid = OperatingSystemCloneProcess(); pid_t MyPid = GetPid(); if (MyPid == ChildPid) //is the child return 0; else //is the parent return ChildPid; }
void Kill() { wxProcess::Kill(GetPid()); // wxProcess::Kill doesn't trigger a call to OnTerminate() normally... // but we still need to call it! OnTerminate(0, -1); }
void CPmtGrabber::OnTsPacket(byte* tsPacket) { if (m_pCallback==NULL) return; int pid=((tsPacket[1] & 0x1F) <<8)+tsPacket[2]; if (pid != GetPid()) return; CEnterCriticalSection enter(m_section); CSectionDecoder::OnTsPacket(tsPacket); }
int create_bitmap(bitmap_t **pbitmap, int width, int height) { size_t size; size_t pitch; bitmap_t *bitmap; uint64_t gaddr; void *uaddr; struct radeon_device *rdev = main_device; struct radeon_bo *sobj = NULL; int r; bitmap = CreateObject(GetPid(), sizeof(bitmap_t)); if( bitmap == NULL) { *pbitmap = NULL; return -1; } pitch = radeon_align_pitch(rdev, width, 32, false) * 4; size = pitch * height; r = radeon_bo_create(rdev, size, PAGE_SIZE, true, RADEON_GEM_DOMAIN_GTT, &sobj); if (r) { goto fail; } r = radeon_bo_reserve(sobj, false); if (unlikely(r != 0)) goto fail; r = radeon_bo_pin(sobj, RADEON_GEM_DOMAIN_GTT, &gaddr); if (r) { goto fail; } r = radeon_bo_user_map(sobj, &uaddr); if (r) { goto fail; } bitmap->width = width; bitmap->height = height; bitmap->stride = pitch; bitmap->gaddr = gaddr; bitmap->uaddr = uaddr; bitmap->robj = sobj; *pbitmap = bitmap; return 0; fail: DestroyObject(bitmap); return -1; };
// copy what dir_p points to (dir_t) to what attr_p points to (attr_t) void Dir2Attr( dir_t *dir_p, attr_t *attr_p ) { attr_p->dev = GetPid(); // FileSystem() manages this i-node attr_p->inode = dir_p->inode; attr_p->mode = dir_p->mode; attr_p->nlink = ( A_ISDIR( attr_p->mode ) ) + 1; attr_p->size = dir_p->size; attr_p->data = dir_p->data; }
bool setupStdio(Pid createdPid, Pid requesterPid, File_t *outStdin, File_t *outStdout, File_t *outStderr, File_t inStdin, File_t inStdout,File_t inStderr) { uint32_t thisPid = GetPid(); if (!createPipe(thisPid, requesterPid, createdPid, inStdin, outStdin, STDIN_FILENO)) return false; if (!createPipe(thisPid, requesterPid, createdPid, inStdout, outStdout, STDOUT_FILENO)) return false; if (!createPipe(thisPid, requesterPid, createdPid, inStderr, outStderr, STDERR_FILENO)) return false; return true; }
void replace_free(void* aPtr) { AutoLock lock(sLock); if (aPtr) { FdPrintf(sFd, "%zu %zu free(%p)\n", GetPid(), GetTid(), aPtr); } sFuncs->free(aPtr); }
void UnixProcessImpl::Cleanup() { close(GetReadHandle()); close(GetWriteHandle()); if(m_thr) { // Stop the reader thread m_thr->Stop(); delete m_thr; } m_thr = NULL; if(GetPid() != wxNOT_FOUND) { wxKill(GetPid(), GetHardKill() ? wxSIGKILL : wxSIGTERM, NULL, wxKILL_CHILDREN); // The Zombie cleanup is done in app.cpp in ::ChildTerminatedSingalHandler() signal handler int status(0); waitpid(-1, &status, WNOHANG); } }
void* replace_valloc(size_t aSize) { AutoLock lock(sLock); void* ptr = sFuncs->valloc(aSize); if (ptr) { FdPrintf(sFd, "%zu %zu valloc(%zu)=%p\n", GetPid(), GetTid(), aSize, ptr); } return ptr; }
TEST(workload, success) { IOEventLoop loop; ASSERT_TRUE(loop.AddSignalEvent(SIGCHLD, [&]() { return loop.ExitLoop(); })); auto workload = Workload::CreateWorkload({"sleep", "1"}); ASSERT_TRUE(workload != nullptr); ASSERT_TRUE(workload->GetPid() != 0); ASSERT_TRUE(workload->Start()); ASSERT_TRUE(loop.RunLoop()); }
int replace_posix_memalign(void** aPtr, size_t aAlignment, size_t aSize) { AutoLock lock(sLock); int ret = sFuncs->posix_memalign(aPtr, aAlignment, aSize); if (ret == 0) { FdPrintf(sFd, "%zu %zu posix_memalign(%zu,%zu)=%p\n", GetPid(), GetTid(), aAlignment, aSize, *aPtr); } return ret; }
void* replace_realloc(void* aPtr, size_t aSize) { AutoLock lock(sLock); void* new_ptr = sFuncs->realloc(aPtr, aSize); if (new_ptr || !aSize) { FdPrintf(sFd, "%zu %zu realloc(%p,%zu)=%p\n", GetPid(), GetTid(), aPtr, aSize, new_ptr); } return new_ptr; }
void* replace_memalign(size_t aAlignment, size_t aSize) { AutoLock lock(sLock); void* ptr = sFuncs->memalign(aAlignment, aSize); if (ptr) { FdPrintf(sFd, "%zu %zu memalign(%zu,%zu)=%p\n", GetPid(), GetTid(), aAlignment, aSize, ptr); } return ptr; }
void Fuzzer::CrashCallback() { Printf("==%d== ERROR: libFuzzer: deadly signal\n", GetPid()); if (__sanitizer_print_stack_trace) __sanitizer_print_stack_trace(); Printf("NOTE: libFuzzer has rudimentary signal handlers.\n" " Combine libFuzzer with AddressSanitizer or similar for better " "crash reports.\n"); Printf("SUMMARY: libFuzzer: deadly signal\n"); DumpCurrentUnit("crash-"); PrintFinalStats(); exit(Options.ErrorExitCode); }
void IdleProc() { int i; for (;;){ cons_printf("%d ", GetPid()); for(i=0; i<1666668; i++){ IO_DELAY(); } } }
void DwMsgId::CreateDefault() { char hostname[80]; hostname[0] = 0; GetHostName(hostname, 80); hostname[79] = 0; char scratch[80]; time_t tt = time(NULL); struct tm tms = *localtime(&tt); int pos = 0; scratch[pos++] = '<'; int n = tms.tm_year; scratch[pos++] = char(n / 10 % 10 + '0'); scratch[pos++] = char(n % 10 + '0'); n = tms.tm_mon + 1; scratch[pos++] = char(n / 10 % 10 + '0'); scratch[pos++] = char(n % 10 + '0'); n = tms.tm_mday; scratch[pos++] = char(n / 10 % 10 + '0'); scratch[pos++] = char(n % 10 + '0'); n = tms.tm_hour; scratch[pos++] = char(n / 10 % 10 + '0'); scratch[pos++] = char(n % 10 + '0'); n = tms.tm_min; scratch[pos++] = char(n / 10 % 10 + '0'); scratch[pos++] = char(n % 10 + '0'); n = tms.tm_sec; scratch[pos++] = char(n / 10 % 10 + '0'); scratch[pos++] = char(n % 10 + '0'); static int counter = 0; scratch[pos++] = base35chars[counter / 35 % 35]; scratch[pos++] = base35chars[counter % 35]; ++counter; scratch[pos++] = '.'; DwUint32 pid = GetPid(); scratch[pos++] = char(pid / 10000 % 10 + '0'); scratch[pos++] = char(pid / 1000 % 10 + '0'); scratch[pos++] = char(pid / 100 % 10 + '0'); scratch[pos++] = char(pid / 10 % 10 + '0'); scratch[pos++] = char(pid % 10 + '0'); scratch[pos++] = '@'; char *cp = hostname; while(*cp && pos < 79) { scratch[pos++] = *cp++; } scratch[pos++] = '>'; scratch[pos] = 0; mString = scratch; mIsModified = 0; Parse(); }
// Tries to call lsan, and if there are leaks exits. We call this right after // the initial corpus was read because if there are leaky inputs in the corpus // further fuzzing will likely hit OOMs. void Fuzzer::CheckForMemoryLeaks() { if (!Options.DetectLeaks) return; if (!__lsan_do_recoverable_leak_check) return; if (__lsan_do_recoverable_leak_check()) { Printf("==%d== ERROR: libFuzzer: initial corpus triggers memory leaks.\n" "Exiting now. Use -detect_leaks=0 to disable leak detection here.\n" "LeakSanitizer will still check for leaks at the process exit.\n", GetPid()); PrintFinalStats(); _Exit(Options.ErrorExitCode); } }
NO_SANITIZE_MEMORY void Fuzzer::AlarmCallback() { assert(Options.UnitTimeoutSec > 0); if (InOOMState) { Printf("==%d== ERROR: libFuzzer: out-of-memory (used: %zdMb; limit: %zdMb)\n", GetPid(), GetPeakRSSMb(), Options.RssLimitMb); Printf(" To change the out-of-memory limit use -rss_limit_mb=<N>\n"); if (CurrentUnitSize && CurrentUnitData) { DumpCurrentUnit("oom-"); if (__sanitizer_print_stack_trace) __sanitizer_print_stack_trace(); } Printf("SUMMARY: libFuzzer: out-of-memory\n"); PrintFinalStats(); _Exit(Options.ErrorExitCode); // Stop right now. } if (!CurrentUnitSize) return; // We have not started running units yet. size_t Seconds = duration_cast<seconds>(system_clock::now() - UnitStartTime).count(); if (Seconds == 0) return; if (Options.Verbosity >= 2) Printf("AlarmCallback %zd\n", Seconds); if (Seconds >= (size_t)Options.UnitTimeoutSec) { Printf("ALARM: working on the last Unit for %zd seconds\n", Seconds); Printf(" and the timeout value is %d (use -timeout=N to change)\n", Options.UnitTimeoutSec); DumpCurrentUnit("timeout-"); Printf("==%d== ERROR: libFuzzer: timeout after %d seconds\n", GetPid(), Seconds); if (__sanitizer_print_stack_trace) __sanitizer_print_stack_trace(); Printf("SUMMARY: libFuzzer: timeout\n"); PrintFinalStats(); _Exit(Options.TimeoutExitCode); // Stop right now. } }
void main(int argc, char **argv) { int delay = 1; int delays[512]; int delays2[1024]; int exit_status; int* a; a = (int*)malloc(sizeof(int) * 100); int cnt = 0; user_log("Init program has PID(%d)", GetPid()); int pid = Fork(); char* tmp[] = {NULL}; if(argc >= 1) { delay = atoi(argv[0]); } user_log("My pid is %d, fork return pid %d", GetPid(), pid); if(pid != 0) { user_log("I am parent with PID(%d), user Wait() for my children...", GetPid()); int cpid = Wait(&exit_status); user_log("Wait my child(%d) done, return meaning of life %d", cpid, exit_status); while(1) { user_log("I have no children, so lonley, going to delay %d seconds", delay); Delay(delay); } } else { while(1) { user_log("I am child with PID(%d), about to exe", GetPid()); Exec("src/goexec", tmp); user_log("PID(%d) try exec fail", GetPid()); Exit(1); } } // Never reached return; }