CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/ipwn/cmd_proc.c
Views: 11766
1
/*
2
* Copyright (c) 2004-2005 vlad902 <vlad902 [at] gmail.com>
3
* Copyright (c) 2007 H D Moore <hdm [at] metasploit.com>
4
* This file is part of the Metasploit Framework.
5
* $Revision$
6
*/
7
8
9
/* The process listing code was taken wholesale from the following file:
10
http://www.psychofx.com/psi/trac/browser/psi/trunk/src/arch/macosx/macosx_processtable.c
11
12
This code was provided under the following license:
13
14
The MIT License
15
16
Copyright (c) 2007 Chris Miles
17
18
Permission is hereby granted, free of charge, to any person obtaining a copy
19
of this software and associated documentation files (the "Software"), to deal
20
in the Software without restriction, including without limitation the rights
21
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22
copies of the Software, and to permit persons to whom the Software is
23
furnished to do so, subject to the following conditions:
24
25
The above copyright notice and this permission notice shall be included in
26
all copies or substantial portions of the Software.
27
28
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34
THE SOFTWARE.
35
*/
36
37
#include <errno.h>
38
#include <stdlib.h>
39
#include <stdio.h>
40
#include <string.h>
41
#include <unistd.h>
42
#include <signal.h>
43
#include <pwd.h>
44
#include <grp.h>
45
46
#include <sys/sysctl.h>
47
#include <sys/fcntl.h>
48
#include <sys/types.h>
49
50
#ifdef MACOSX
51
#include <sys/proc.h>
52
#include <mach/mach_traps.h> /* for task_for_pid() */
53
#include <mach/shared_memory_server.h>
54
#include <mach/mach_init.h>
55
#include <mach/task.h>
56
#endif
57
58
#include "cmd.h"
59
60
61
void cmd_kill(int argc, char * argv[])
62
{
63
int killsig = 9;
64
65
if(argc > 1)
66
killsig = atoi(argv[2]);
67
68
if(kill(atoi(argv[1]), killsig) == -1)
69
perror("kill");
70
}
71
72
void cmd_getpid(int argc, char * argv[])
73
{
74
printf("%i\n", getpid());
75
}
76
77
void cmd_getppid(int argc, char * argv[])
78
{
79
printf("%i\n", getppid());
80
}
81
82
void cmd_ps(int argc, char * argv[])
83
{
84
#ifdef MACOSX
85
int process_count;
86
int err;
87
int i;
88
int done;
89
size_t length;
90
static const int name_getprocs[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
91
register struct kinfo_proc *p;
92
struct kinfo_proc * result;
93
94
// We start by calling sysctl with result == NULL and length == 0.
95
// That will succeed, and set length to the appropriate length.
96
// We then allocate a buffer of that size and call sysctl again
97
// with that buffer. If that succeeds, we're done. If that fails
98
// with ENOMEM, we have to throw away our buffer and loop. Note
99
// that the loop causes us to call sysctl with NULL again; this
100
// is necessary because the ENOMEM failure case sets length to
101
// the amount of data returned, not the amount of data that
102
// could have been returned.
103
104
result = NULL;
105
done = 0;
106
do {
107
// Call sysctl with a NULL buffer.
108
109
length = 0;
110
err = sysctl( (int *) name_getprocs, (sizeof(name_getprocs) / sizeof(*name_getprocs)) - 1,
111
NULL, &length,
112
NULL, 0);
113
if (err == -1) {
114
err = errno;
115
}
116
117
// Allocate an appropriately sized buffer based on the results
118
// from the previous call.
119
120
if (err == 0) {
121
result = malloc(length);
122
if (result == NULL) {
123
err = ENOMEM;
124
}
125
}
126
127
// Call sysctl again with the new buffer. If we get an ENOMEM
128
// error, toss away our buffer and start again.
129
130
if (err == 0) {
131
err = sysctl( (int *) name_getprocs, (sizeof(name_getprocs) / sizeof(*name_getprocs)) - 1,
132
result, &length,
133
NULL, 0);
134
if (err == -1) {
135
err = errno;
136
}
137
if (err == 0) {
138
done = 1;
139
} else if (err == ENOMEM) {
140
free(result);
141
result = NULL;
142
err = 0;
143
}
144
}
145
} while (err == 0 && ! done);
146
147
// Clean up and establish post conditions.
148
149
if (err != 0 && result != NULL) {
150
free(result);
151
result = NULL;
152
}
153
154
if (err == 0) {
155
process_count = length / sizeof(struct kinfo_proc);
156
}
157
158
printf("Process table:\n");
159
for(p = result, i = 0; i < process_count; p++, i++)
160
{
161
printf("%5d\t%s\n", p->kp_proc.p_pid, p->kp_proc.p_comm);
162
}
163
#else
164
printf("This command is not supported on this operating system.\n");
165
#endif
166
}
167
168