Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/ipwn/cmd_proc.c
Views: 11766
/*1* Copyright (c) 2004-2005 vlad902 <vlad902 [at] gmail.com>2* Copyright (c) 2007 H D Moore <hdm [at] metasploit.com>3* This file is part of the Metasploit Framework.4* $Revision$5*/678/* The process listing code was taken wholesale from the following file:9http://www.psychofx.com/psi/trac/browser/psi/trunk/src/arch/macosx/macosx_processtable.c1011This code was provided under the following license:1213The MIT License1415Copyright (c) 2007 Chris Miles1617Permission is hereby granted, free of charge, to any person obtaining a copy18of this software and associated documentation files (the "Software"), to deal19in the Software without restriction, including without limitation the rights20to use, copy, modify, merge, publish, distribute, sublicense, and/or sell21copies of the Software, and to permit persons to whom the Software is22furnished to do so, subject to the following conditions:2324The above copyright notice and this permission notice shall be included in25all copies or substantial portions of the Software.2627THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR28IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,29FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE30AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER31LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,32OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN33THE SOFTWARE.34*/3536#include <errno.h>37#include <stdlib.h>38#include <stdio.h>39#include <string.h>40#include <unistd.h>41#include <signal.h>42#include <pwd.h>43#include <grp.h>4445#include <sys/sysctl.h>46#include <sys/fcntl.h>47#include <sys/types.h>4849#ifdef MACOSX50#include <sys/proc.h>51#include <mach/mach_traps.h> /* for task_for_pid() */52#include <mach/shared_memory_server.h>53#include <mach/mach_init.h>54#include <mach/task.h>55#endif5657#include "cmd.h"585960void cmd_kill(int argc, char * argv[])61{62int killsig = 9;6364if(argc > 1)65killsig = atoi(argv[2]);6667if(kill(atoi(argv[1]), killsig) == -1)68perror("kill");69}7071void cmd_getpid(int argc, char * argv[])72{73printf("%i\n", getpid());74}7576void cmd_getppid(int argc, char * argv[])77{78printf("%i\n", getppid());79}8081void cmd_ps(int argc, char * argv[])82{83#ifdef MACOSX84int process_count;85int err;86int i;87int done;88size_t length;89static const int name_getprocs[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };90register struct kinfo_proc *p;91struct kinfo_proc * result;9293// We start by calling sysctl with result == NULL and length == 0.94// That will succeed, and set length to the appropriate length.95// We then allocate a buffer of that size and call sysctl again96// with that buffer. If that succeeds, we're done. If that fails97// with ENOMEM, we have to throw away our buffer and loop. Note98// that the loop causes us to call sysctl with NULL again; this99// is necessary because the ENOMEM failure case sets length to100// the amount of data returned, not the amount of data that101// could have been returned.102103result = NULL;104done = 0;105do {106// Call sysctl with a NULL buffer.107108length = 0;109err = sysctl( (int *) name_getprocs, (sizeof(name_getprocs) / sizeof(*name_getprocs)) - 1,110NULL, &length,111NULL, 0);112if (err == -1) {113err = errno;114}115116// Allocate an appropriately sized buffer based on the results117// from the previous call.118119if (err == 0) {120result = malloc(length);121if (result == NULL) {122err = ENOMEM;123}124}125126// Call sysctl again with the new buffer. If we get an ENOMEM127// error, toss away our buffer and start again.128129if (err == 0) {130err = sysctl( (int *) name_getprocs, (sizeof(name_getprocs) / sizeof(*name_getprocs)) - 1,131result, &length,132NULL, 0);133if (err == -1) {134err = errno;135}136if (err == 0) {137done = 1;138} else if (err == ENOMEM) {139free(result);140result = NULL;141err = 0;142}143}144} while (err == 0 && ! done);145146// Clean up and establish post conditions.147148if (err != 0 && result != NULL) {149free(result);150result = NULL;151}152153if (err == 0) {154process_count = length / sizeof(struct kinfo_proc);155}156157printf("Process table:\n");158for(p = result, i = 0; i < process_count; p++, i++)159{160printf("%5d\t%s\n", p->kp_proc.p_pid, p->kp_proc.p_comm);161}162#else163printf("This command is not supported on this operating system.\n");164#endif165}166167168