2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 1, or (at your option)
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #import <sys/sysctl.h>
28 @interface Process ( PrivateAPI
)
30 - (void)_setName
:(NSString
*)name
;
31 - (void)_setVersion
:(NSString
*)version
;
32 - (void)_setIcon
:(NSImage
*)icon
;
33 - (void)_setPID
:(pid_t
)pid
;
38 @implementation Process
41 - (id)initWithName
:(NSString
*)name version
:(NSString
*)version icon
:(NSImage
*)icon pid
:(pid_t
)pid
43 if ( self = [super init
] ) {
45 [self _setVersion
:version
];
52 - (id)initWithName
:(NSString
*)name version
:(NSString
*)version icon
:(NSImage
*)icon
54 return [self initWithName
:name version
:version icon
:icon pid
:0];
57 - (id)initWithName
:(NSString
*)name version
:(NSString
*)version
59 return [self initWithName
:name version
:version icon
:nil pid
:0];
64 // release local objects
73 // #############################################################################
75 // #############################################################################
77 - (id)copyWithZone
:(NSZone
*)zone
79 return [[Process allocWithZone
:zone
] initWithName
:_name version
:_version icon
:_icon pid
:_pid
];
83 // #############################################################################
85 // #############################################################################
87 - (id)initWithCoder
:(NSCoder
*)coder
89 if ( self = [super init
] )
91 [self _setName
:[coder decodeObject
]];
92 [self _setVersion
:[coder decodeObject
]];
93 //[self setIcon:[coder decodeObject]];
94 [coder decodeValueOfObjCType
:@encode(pid_t
) at
:&_pid
];
99 - (void)encodeWithCoder
:(NSCoder
*)coder
101 [coder encodeObject
:_name
];
102 [coder encodeObject
:_version
];
103 //[coder encodeObject:_icon];
104 [coder encodeValueOfObjCType
:@encode(pid_t
) at
:&_pid
];
108 #pragma mark NSObject Override
110 - (BOOL)isEqual
:(id)anObject
112 if ( [_name isEqualToString
:[(Process
*)anObject name
]] &&
113 [_version isEqualToString
:[(Process
*)anObject version
]] &&
114 _pid
== [(Process
*)anObject pid
] ) {
115 // they are the same process
123 return [[NSString stringWithFormat
:@
"%@%@%u", _name
, _version
, _pid
] hash
];
127 - (BOOL)sameApplicationAs
:(id)anObject
129 if ( [_name isEqualToString
:[(Process
*)anObject name
]] &&
130 [_version isEqualToString
:[(Process
*)anObject version
]] ) {
131 // they are the same application
137 #pragma mark Detecting Emulation
140 // http://developer.apple.com/documentation/MacOSX/Conceptual/universal_binary/universal_binary_exec_a/universal_binary_exec_a.html
141 static int sysctlbyname_with_pid (const char *name
, pid_t pid
,
142 void *oldp
, size_t *oldlenp
,
143 void *newp
, size_t newlen
)
146 if (sysctlbyname(name
, oldp
, oldlenp
, newp
, newlen
) == -1) {
147 fprintf(stderr
, "sysctlbyname_with_pid(0): sysctlbyname failed:"
148 "%s\n", strerror(errno
));
152 int mib
[CTL_MAXNAME
+1];
153 size_t len
= CTL_MAXNAME
;
154 if (sysctlnametomib(name
, mib
, &len
) == -1) {
155 fprintf(stderr
, "sysctlbyname_with_pid: sysctlnametomib failed:"
156 "%s\n", strerror(errno
));
161 if (sysctl(mib
, len
, oldp
, oldlenp
, newp
, newlen
) == -1) {
162 fprintf(stderr
, "sysctlbyname_with_pid: sysctl failed:"
163 "%s\n", strerror(errno
));
170 // http://developer.apple.com/documentation/MacOSX/Conceptual/universal_binary/universal_binary_exec_a/universal_binary_exec_a.html
171 static int is_pid_native (pid_t pid
)
174 size_t sz
= sizeof(ret
);
176 if (sysctlbyname_with_pid("sysctl.proc_native", pid
,
177 &ret
, &sz
, NULL
, 0) == -1) {
178 if (errno
== ENOENT
) {
181 fprintf(stderr
, "is_pid_native: sysctlbyname_with_pid failed:"
182 "%s\n", strerror(errno
));
191 BOOL isEmulated
= NO
;
193 if (is_pid_native(_pid
) == 0)
202 #pragma mark Accessors
209 - (NSString
*)version
225 - (void)_setName
:(NSString
*)name
228 name
= [NSString stringWithString
:@
"Unknown Process"];
235 - (void)_setVersion
:(NSString
*)version
238 version
= [NSString stringWithString
:@
"Unknown Version"];
245 - (void)_setIcon
:(NSImage
*)icon
252 [_icon setScalesWhenResized
:YES
];
253 [_icon setSize
:NSMakeSize(16,16)];
256 - (void)_setPID
:(pid_t
)pid