Skip to content

Commit 12d94ab

Browse files
committed
Added klog utility
1 parent 87fc6f3 commit 12d94ab

File tree

13 files changed

+329
-2
lines changed

13 files changed

+329
-2
lines changed

Nyxian.xcodeproj/project.pbxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
LindChain/ProcEnvironment/Sysctl/sysctl.m,
168168
LindChain/ProcEnvironment/tfp.m,
169169
LindChain/ProcEnvironment/Utils/fd.c,
170+
LindChain/ProcEnvironment/Utils/klog.m,
170171
LindChain/Utils/Swizzle.m,
171172
LindChain/Utils/Zip.m,
172173
);

Nyxian.xcodeproj/xcuserdata/seanistethered.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,22 @@
33
uuid = "B61CCDAE-D077-4897-B97F-2AE1CC4230C5"
44
type = "1"
55
version = "2.0">
6+
<Breakpoints>
7+
<BreakpointProxy
8+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
9+
<BreakpointContent
10+
uuid = "60ECC0A4-E850-44F4-93CC-E4387EC5B9EC"
11+
shouldBeEnabled = "No"
12+
ignoreCount = "0"
13+
continueAfterRunningActions = "No"
14+
filePath = "Nyxian/LindChain/ProcEnvironment/Surface/proc/exit.m"
15+
startingColumnNumber = "9223372036854775807"
16+
endingColumnNumber = "9223372036854775807"
17+
startingLineNumber = "34"
18+
endingLineNumber = "34"
19+
landmarkName = "proc_exit_for_pid(pid)"
20+
landmarkType = "9">
21+
</BreakpointContent>
22+
</BreakpointProxy>
23+
</Breakpoints>
624
</Bucket>

Nyxian/LindChain/Multitask/ProcessManager/LDEProcessManager.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#import <LindChain/ProcEnvironment/Surface/proc/proc.h>
2323
#import <LindChain/ProcEnvironment/panic.h>
2424
#import <Nyxian-Swift.h>
25+
#import <LindChain/ProcEnvironment/Utils/klog.h>
2526

2627
@implementation LDEProcessManager {
2728
NSTimeInterval _lastSpawnTime;
@@ -168,6 +169,7 @@ - (LDEProcess*)processForProcessIdentifier:(pid_t)pid
168169
- (void)unregisterProcessWithProcessIdentifier:(pid_t)pid
169170
{
170171
dispatch_sync(_syncQueue, ^{
172+
klog_log(@"LDEProcessManager:unregisterProcessWithProcessIdentifier", @"unregistering pid %d", pid);
171173
[self.processes removeObjectForKey:@(pid)];
172174
ksurface_error_t error = proc_exit_for_pid(pid);
173175
if(error != kSurfaceErrorSuccess)

Nyxian/LindChain/ProcEnvironment/Server/ServerSession.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import <LindChain/LaunchServices/LaunchService.h>
2929
#import <mach/mach.h>
3030
#import <LindChain/Multitask/WindowServer/Session/LDEWindowSessionApplication.h>
31+
#import <LindChain/ProcEnvironment/Utils/klog.h>
3132

3233
@implementation ServerSession
3334

@@ -79,9 +80,12 @@ - (void)proc_kill:(pid_t)pid
7980
withSignal:(int)signal
8081
withReply:(void (^)(int))reply
8182
{
83+
klog_log(@"syscall:kill", @"pid %d requested to signal pid %d with %d", _processIdentifier, pid, signal);
84+
8285
// Checking if we have necessary entitlements
8386
if(pid != _processIdentifier && (!proc_got_entitlement(_processIdentifier, PEEntitlementProcessKill) || !permitive_over_process_allowed(_processIdentifier, pid)))
8487
{
88+
klog_log(@"syscall:kill", @"pid %d not autorized to kill pid %d", _processIdentifier, pid);
8589
reply(-1);
8690
return;
8791
}
@@ -90,11 +94,13 @@ - (void)proc_kill:(pid_t)pid
9094
LDEProcess *process = [[LDEProcessManager shared] processForProcessIdentifier:pid];
9195
if(!process)
9296
{
97+
klog_log(@"syscall:kill", @"pid %d not found on high level process manager", pid);
9398
reply(1);
9499
return;
95100
}
96101

97102
[process sendSignal:signal];
103+
klog_log(@"syscall:kill", @"pid %d signaled pid %d", _processIdentifier, pid);
98104

99105
reply(0);
100106
}

Nyxian/LindChain/ProcEnvironment/Surface/proc/exit.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,23 @@
2525

2626
#ifdef HOST_ENV
2727
#import <LindChain/Multitask/ProcessManager/LDEProcessManager.h>
28+
#import <LindChain/ProcEnvironment/Utils/klog.h>
2829
#endif
2930

3031
ksurface_error_t proc_exit_for_pid(pid_t pid)
3132
{
3233
#ifdef HOST_ENV
3334
reflock_lock(&(surface->reflock));
3435

36+
klog_log(@"proc:exit", @"pid %d requested to exit", pid);
37+
3538
// Get process ptr
3639
unsigned int index = 0;
3740
ksurface_proc_t *proc = NULL;
3841
ksurface_error_t error = proc_ptr_for_pid(pid, &proc, &index);
3942
if(error != kSurfaceErrorSuccess)
4043
{
44+
klog_log(@"proc:exit", @"pid %d wasnt found", pid);
4145
reflock_unlock(&(surface->reflock));
4246
return error;
4347
}
@@ -54,6 +58,7 @@ ksurface_error_t proc_exit_for_pid(pid_t pid)
5458
{
5559
if(flagged_pid[i] == proc_getppid(surface->proc_info.proc[i]))
5660
{
61+
klog_log(@"proc:exit", @"flagging pid %d", proc_getpid(surface->proc_info.proc[i]));
5762
flagged_pid[flagged_pid_cnt++] = proc_getpid(surface->proc_info.proc[i]);
5863
break;
5964
}
@@ -67,9 +72,14 @@ ksurface_error_t proc_exit_for_pid(pid_t pid)
6772
error = proc_remove_by_pid(flagged_pid[i]);
6873
if(error != kSurfaceErrorSuccess)
6974
{
75+
klog_log(@"proc:exit", @"failed to remove process structure for pid %d", flagged_pid[i]);
7076
reflock_unlock(&(surface->reflock));
7177
return error;
7278
}
79+
else
80+
{
81+
klog_log(@"proc:exit", @"removed process structure for pid %d", flagged_pid[i]);
82+
}
7383
}
7484

7585
reflock_unlock(&(surface->reflock));
@@ -80,8 +90,13 @@ ksurface_error_t proc_exit_for_pid(pid_t pid)
8090
LDEProcess *process = [LDEProcessManager shared].processes[@(flagged_pid[i])];
8191
if(process != nil)
8292
{
93+
klog_log(@"proc:exit", @"terminating process for pid %d", flagged_pid[i]);
8394
[process terminate];
8495
}
96+
else
97+
{
98+
klog_log(@"proc:exit", @"failed to terminate process for pid %d", flagged_pid[i]);
99+
}
85100
}
86101

87102
return error;

Nyxian/LindChain/ProcEnvironment/Surface/proc/new.m

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
#import <LindChain/Services/trustd/LDETrust.h>
2626
#import <LindChain/ProcEnvironment/Server/Trust.h>
2727
#import <LindChain/ProcEnvironment/panic.h>
28+
#import <LindChain/ProcEnvironment/Utils/klog.h>
2829

2930
ksurface_error_t proc_init_kproc(void)
3031
{
3132
#ifdef HOST_ENV
3233
reflock_lock(&(surface->reflock));
3334

35+
klog_log(@"proc:kproc", @"initilizing kernel process");
36+
3437
if(surface->proc_info.proc_count != 0)
3538
{
3639
// Its not nyxian adding it self to the list... This shall never happen under no condition
@@ -49,6 +52,7 @@ ksurface_error_t proc_init_kproc(void)
4952
NSString *executablePath = [[NSBundle mainBundle] executablePath];
5053
strncpy(proc.nyx.executable_path, [executablePath UTF8String], PATH_MAX);
5154
proc_setentitlements(proc, PEEntitlementKernel);
55+
klog_log(@"proc:kproc", @"setting kernel process entitlements to %lu", PEEntitlementKernel);
5256

5357
// Set bsd process stuff
5458
if(gettimeofday(&proc.bsd.kp_proc.p_un.__p_starttime, NULL) != 0)
@@ -81,6 +85,10 @@ ksurface_error_t proc_init_kproc(void)
8185
proc.bsd.kp_eproc.e_flag = 2;
8286

8387
ksurface_error_t error = proc_append(proc);
88+
if(error == kSurfaceErrorSuccess)
89+
{
90+
klog_log(@"proc:kproc", @"successfully created kernel process in process table");
91+
}
8492

8593
// Adding/Inserting proc
8694
reflock_unlock(&(surface->reflock));
@@ -98,6 +106,8 @@ ksurface_error_t proc_new_child_proc(pid_t ppid,
98106
#ifdef HOST_ENV
99107
reflock_lock(&(surface->reflock));
100108

109+
klog_log(@"proc:new", @"pid %d requested creation of its child pid %d in the process table with executable path \"%@\"", ppid, pid, executablePath);
110+
101111
// Get the old process
102112
ksurface_proc_t proc = {};
103113
ksurface_error_t error = proc_for_pid(ppid, &proc);
@@ -107,9 +117,13 @@ ksurface_error_t proc_new_child_proc(pid_t ppid,
107117
return error;
108118
}
109119

120+
klog_log(@"proc:new", @"found process structure of pid %d in table", ppid);
121+
110122
// Check if Nyxian spawned it, if so, drop its permitives accordingly
111123
if(proc_getppid(proc) == PID_LAUNCHD)
112124
{
125+
klog_log(@"proc:new", @"dropping permitives of child process %d", pid);
126+
113127
//Its Nyxian it self and due to that we have to drop permitives to mobile user
114128
proc_setuid(proc, 501);
115129
proc_setruid(proc, 501);
@@ -122,20 +136,25 @@ ksurface_error_t proc_new_child_proc(pid_t ppid,
122136
// Inheriting entitlements or not?
123137
if(!entitlement_got_entitlement(proc_getentitlements(proc), PEEntitlementProcessSpawnInheriteEntitlements))
124138
{
139+
klog_log(@"proc:new", @"pid %d doesnt inherit entitlements of pid %d", pid, ppid);
125140
NSString *entHash = [LDETrust entHashOfExecutableAtPath:executablePath];
126141
if(entHash == nil)
127142
{
143+
klog_log(@"proc:new", @"no hash found for pid %d dropping entitlements to %lu", pid, PEEntitlementSandboxedApplication);
128144
proc_setentitlements(proc, PEEntitlementSandboxedApplication);
129145
}
130146
else
131147
{
132-
proc_setentitlements(proc, [[TrustCache shared] getEntitlementsForHash:entHash]);
148+
PEEntitlement entitlement = [[TrustCache shared] getEntitlementsForHash:entHash];
149+
klog_log(@"proc:new", @"hash found for pid %d setting entitlements to %lu", pid, entitlement);
150+
proc_setentitlements(proc, entitlement);
133151
}
134152
}
135153

136154
// Reset time to now
137155
if(gettimeofday(&proc.bsd.kp_proc.p_un.__p_starttime, NULL) != 0)
138156
{
157+
klog_log(@"proc:new", @"failed to get time of the day");
139158
reflock_unlock(&(surface->reflock));
140159
return kSurfaceErrorUndefined;
141160
}
@@ -149,6 +168,7 @@ ksurface_error_t proc_new_child_proc(pid_t ppid,
149168
proc_setpid(proc, pid);
150169

151170
// Insert it back
171+
klog_log(@"proc:new", @"Inserting process structure of pid %d", pid);
152172
error = proc_append(proc);
153173

154174
reflock_unlock(&(surface->reflock));

Nyxian/LindChain/ProcEnvironment/Surface/surface.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import <mach/mach.h>
2626
#import <sys/sysctl.h>
2727
#import <mach-o/dyld.h>
28+
#import <LindChain/ProcEnvironment/Utils/klog.h>
2829

2930
ksurface_mapping_t *surface = NULL;
3031
static MappingPortObject *surfaceMappingPortObject = NULL;
@@ -70,6 +71,7 @@
7071

7172
void kern_sethostname(NSString *hostname)
7273
{
74+
klog_log(@"surface", @"setting hostname to %@", hostname);
7375
reflock_lock(&(surface->reflock));
7476
hostname = hostname ?: @"localhost";
7577
strlcpy(surface->host_info.hostname, [hostname UTF8String], MAXHOSTNAMELEN);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright (C) 2025 cr4zyengineer
3+
4+
This file is part of Nyxian.
5+
6+
Nyxian 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 3 of the License, or
9+
(at your option) any later version.
10+
11+
Nyxian 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.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with Nyxian. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef KLOG_H
21+
#define KLOG_H
22+
23+
#import <Foundation/Foundation.h>
24+
25+
#define KLOG_ENABLED 1
26+
27+
#if KLOG_ENABLED
28+
29+
#define klog_log(system, format, ...) \
30+
klog_log_internal((system), (format), ##__VA_ARGS__)
31+
32+
#else
33+
34+
// When disabled: nothing is evaluated, nothing is called, arguments not touched.
35+
#define klog_log(system, format, ...)
36+
37+
#endif
38+
39+
void klog_log_internal(NSString *system, NSString *format, ...);
40+
NSString *klog_dump(void);
41+
42+
#endif /* KLOG_H */

0 commit comments

Comments
 (0)