forked from necokeine/StreamOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.pp
397 lines (358 loc) · 13.9 KB
/
kernel.pp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// vim: ts=3:filetype=pascal
(* Copyright (C) 2004-2009 Oleksandr Natalenko aka post-factum
This program is free software; you can redistribute it and/or modify
it under the terms of the Universal Program License as published by
Oleksandr Natalenko aka post-factum; see file COPYING for details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the Universal Program
License along with this program; if not, write to
unit
kernel;
(*
this is a core of StreamOS kernel. You may find here process descriptors,
process management functions
*)
interface
uses
stypes,
dynlibs;
{$i config.inc}
type
TProcess = record (*process container type *)
processName: String; (*name *)
processUser: Longint; (*owner *)
processParentPid: Longint; (*parent *)
processChildren: TProcessChildren; (*children *)
processChildrenLock: TLockContainer; (*children array lock *)
processStartTime: Extended; (*start time *)
processParameters: TDynamicStringList; (*parameters *)
processState: TProcessState; (*state *)
processLibrary: TLibHandle; (*DLL pointer *)
processMainThreadProcedure: TProcessMainProcedure; (*main procedure *)
processOnKeyHandlerProcedure: TProcessOnKeyHandlerProcedure; (*keypress handler *)
processKey: Char; (*pressed key *)
processKeyLock: TLockContainer; (*key handler lock *)
processOnMessageHandlerProcedure: TProcessOnMessageHandlerProcedure; (*message handler *)
processMessage:TMessage; (*message *)
processMessageLock: TLockContainer; (*lock message *)
processOnSignalHandlerProcedure: TProcessOnSignalHandlerProcedure; (*signal handler *)
processSignal: Longint; (*signal number *)
processSignalSender: Longint; (*sender's PID *)
processMainThreadId: TThreadId; (*main thread ID *)
processExports: TExports; (*exported process *)
processAccessKey: TAccessKey; (*system calls access key *)
processDisplay: Longint; (*display *)
processParentDisplay: Longint; (*parent's display *)
processCurrentDirectory: String; (*current directory *)
processLock: TLockContainer; (*process struct lock *)
processTimers: array of TTimer; (*process timers *)
processTimerIdProcessing: Longint; (*ID of starting timer *)
processTimerIdProcessingLock: TLockContainer; (*timer ID lock *)
end;
var
process: array of TProcess; (*processes table *)
kernelInternalTimerInterval: Longint; (*interval of internal timer *)
kernelRoot: String; (*root drive *)
kernelHostname: String; (*hostname *)
kernelProcessQueueLock, kernelForkLock: TLockContainer; (*process queue lock *)
function KernelProcessCreate(fileName: String; parameters: TDynamicStringList; parentPid: Longint; user: Longint): Longint;
function KernelProcessDestroy(pid: Longint): Boolean;
procedure KernelOnMessageHandler(message: TMessage);
implementation
uses
syscalls,
sysutils,
errorman,
tools,
managers,
modules,
fs,
debug,
lock;
(*===creates new process===*)
function KernelProcessCreate(fileName: String; parameters: TDynamicStringList; parentPid: Longint; user: Longint): Longint;
var
pid, i: Longint;
whereToSearch, toLoad: String;
extend: Boolean;
begin
(*locks create/destroy functions*)
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, entering fork section');
{$endif}
EnterLock(kernelForkLock);
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, in fork section');
{$endif}
(*looks for free pid*)
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, looking for free PID');
{$endif}
pid := 0;
EnterLock(kernelProcessQueueLock);
repeat
inc(pid);
until (pid > Length(process) - 1) or
(process[pid].processState = tps_none);
(*if not found free cell, make a new one*)
if pid > (Length(process) - 1) then
begin
SetLength(process, Length(process) + 1);
extend := true;
end
else
extend := false;
LeaveLock(kernelProcessQueueLock);
try
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, initializing variables for [pid=' + inttostr(pid) + ', name=' + filename + ']');
{$endif}
(*assigns main process variables*)
with process[pid] do
begin
processState := tps_creating;
processName := fileName;
processParentPid := parentPid;
processAccessKey := tools.ToolsGenerateAccessKey;
processParameters := parameters;
processDisplay := -1;
processParentDisplay := process[parentpid].processDisplay;
processCurrentDirectory := process[parentpid].processCurrentDirectory;
processUser := user;
processKey := #0;
processTimerIdProcessing := 0;
InitLock(processMessageLock);
InitLock(processChildrenLock);
InitLock(processLock);
InitLock(processKeyLock);
InitLock(processTimerIdProcessingLock);
processSignal := 0;
processSignalSender := 0;
SetLength(processExports.te_pointers, 1);
SetLength(processExports.te_names, 1);
SetLength(processChildren, 1);
SetLength(processTimers, 1);
end;
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, loading library [pid=' + inttostr(pid) + ', name=' + filename + ']');
{$endif}
(*loads dll, resolvs filename*)
whereToSearch := GetEnvironmentVariable('PATH');
if ExtractFileName(fileName) = fileName then
toLoad := FileSearch(fs.StreamFSToDOS(fileName), whereToSearch)
else
toLoad := fs.StreamFSToDOS(fileName);
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, copying DLL to RAM-disk [pid=' + IntToStr(pid) + ', name=' + fileName + ']');
{$endif}
(*
copies dll to RAM-disk. It's used to avoid bug with copies of one
program. If we need to run several copies of one program, we copy
dll several times to RAM-disk under different names (pids)
*)
if (toload = '') or
(not fs.FsCopyFile(toload, 'Z:\' + IntToStr(pid))) then
raise(EAccessViolation.Create('file copying error'));
(*loads dll from RAM-disk*)
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, loading DLL from RAM-disk [pid=' + IntToStr(pid) + ', name=' + fileName + ']');
{$endif}
process[pid].processLibrary := loadlibrary('Z:\' + IntToStr(pid) + '.');
if process[pid].processLibrary = 0 then
raise(EAccessViolation.Create('library loading error'));
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, loading procedures [pid=' + IntToStr(pid) + ', name=' + fileName + ']');
{$endif}
(*sets procedures*)
pointer(process[pid].processMainThreadProcedure) := GetProcedureAddress(process[pid].processLibrary, 'lib_main');
pointer(process[pid].processOnKeyHandlerProcedure) := GetProcedureAddress(process[pid].processLibrary, 'lib_key');
pointer(process[pid].processOnMessageHandlerProcedure) := GetProcedureAddress(process[pid].processLibrary, 'lib_msg');
pointer(process[pid].processOnSignalHandlerProcedure) := GetProcedureAddress(process[pid].processLibrary, 'lib_signal');
(*adds new child to parent*)
EnterLock(process[parentpid].processChildrenLock);
i:=0;
repeat
inc(i);
until (i > Length(process[parentpid].processChildren) - 1) or
(process[parentpid].processChildren[i] = 0);
if i > Length(process[parentpid].processChildren) - 1 then
SetLength(process[parentpid].processChildren, Length(process[parentpid].processChildren) + 1);
process[parentpid].processChildren[i] := pid;
LeaveLock(process[parentpid].processChildrenLock);
(*removes temporary file from Z:\*)
DeleteFile('Z:\' + IntToStr(pid));
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, starting a new thread [pid=' + IntToStr(pid) + ', name=' + fileName + ']');
{$endif}
(*starts new thread*)
process[pid].processState := tps_running;
{$ifdef config_debug}
{$ifdef collect_lock_statistic}
debug.DebugReportEvent('Locks [i:' + IntToStr(nr_init) + ', e:' + IntToStr(nr_enter) + ', l:' + IntToStr(nr_leave) + ', d:' + IntToStr(nr_done) + ', w:' + IntToStr(nr_wait) + ', a:' + IntToStr(nr_is) + ']');
{$endif}
{$endif}
EnterLock(managersLockExternal);
BeginThread(@managers.ManagersExecuteExternal, pointer(pid));
WaitLock(managersLockExternal);
process[pid].processStartTime := now;
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, thread started [pid=' + IntToStr(pid) + ', name=' + fileName + ']');
{$endif}
KernelProcessCreate := pid;
except
on e: exception do
begin
(*clean and report about error*)
if extend then
begin
EnterLock(kernelProcessQueueLock);
SetLength(process, Length(process) - 1);
LeaveLock(kernelProcessQueueLock);
end;
errorman.ErrormanRaiseKernelError(0, e.message);
KernelProcessCreate := -1;
end;
end;
(*unlocks the function*)
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, leaving fork section');
{$endif}
LeaveLock(kernelForkLock);
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, out of fork section');
{$endif}
end;
(*===destroys given process===*)
function KernelProcessDestroy(pid: Longint): Boolean;
var
i:longint;
begin
(*locks create/destroy functions*)
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, entering fork section]');
{$endif}
EnterLock(kernelForkLock);
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, in fork section]');
{$endif}
if (pid < Length(process)) and
(pid > 0) and
(not (process[pid].processState = tps_none)) then
begin
try
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, destroying child processes [pid=' + IntToStr(pid) + ']');
{$endif}
(*marking as being destroyed*)
process[pid].processState := tps_destroying;
(*destroying timers immediately*)
for i:= 1 to Length(process[pid].processTimers) - 1 do
if process[pid].processTimers[i].timerOwner = pid then
begin
with process[pid].processTimers[i] do
begin
timerProcedure := nil;
timerInterval := 0;
timerEnabled := false;
timerLastExecuted := 0;
timerOwner := -1;
timerThreadId := 0;
end;
if i = Length(process[pid].processTimers) - 1 then
SetLength(process[pid].processTimers, Length(process[pid].processTimers) - 1);
end;
(*it probably causes deadlock, haven't tested it yet*)
for i := 1 to Length(process[pid].processChildren) - 1 do
KernelProcessDestroy(process[pid].processChildren[i]);
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, unloading library [pid=' + inttostr(pid) + ']');
{$endif}
if not UnloadLibrary(process[pid].processLibrary) then
errorman.ErrormanRaiseKernelError(1, 'Library unloading error');
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, cleaning variables [pid='+inttostr(pid)+']');
{$endif}
(*prevent bad-written application from leaking displays*)
if not (process[pid].processDisplay = -1) then
DisplayDestroyDisplay(process[pid].processDisplay);
(*clean parent's children array*)
EnterLock(process[process[pid].processParentPid].processChildrenLock);
for i := 1 to Length(process[process[pid].processParentPid].processChildren) do
if process[process[pid].processParentPid].processChildren[i] = pid then
begin
process[process[pid].processParentPid].processChildren[i] := 0;
if i = Length(process[process[pid].processParentPid].processChildren) - 1 then
SetLength(process[process[pid].processParentPid].processChildren, Length(process[process[pid].processParentPid].processChildren) - 1);
end;
LeaveLock(process[process[pid].processParentPid].processChildrenLock);
(*clean variables*)
with process[pid] do
begin
processState := tps_none;
processName := '';
processDisplay := -1;
processParentDisplay := -1;
processParentPid := 0;
processCurrentDirectory := '';
processUser := -1;
processSignal := 0;
processSignalSender := 0;
SetLength(processExports.te_pointers, 0);
SetLength(processExports.te_names, 0);
SetLength(processChildren, 0);
SetLength(processTimers, 0);
processKey := #0;
DoneLock(processMessageLock);
DoneLock(processChildrenLock);
DoneLock(processLock);
DoneLock(processKeyLock);
DoneLock(processTimerIdProcessingLock);
processStartTime := 0;
SetLength(processParameters, 0);
processLibrary := 0;
pointer(processMainThreadProcedure) := nil;
pointer(processOnKeyHandlerProcedure) := nil;
pointer(processOnMessageHandlerProcedure) := nil;
pointer(processOnSignalHandlerProcedure) := nil;
processMainThreadId := 0;
end;
(*clean process queue if needed*)
EnterLock(kernelProcessQueueLock);
if pid = Length(process) - 1 then
SetLength(process, pid);
LeaveLock(kernelProcessQueueLock);
KernelProcessDestroy := true;
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, destroyed [pid=' + IntToStr(pid) + ']');
{$endif}
except
on e: exception do
begin
errorman.ErrormanRaiseKernelError(1, e.message);
KernelProcessDestroy := false;
end;
end;
end
else
KernelProcessDestroy := false;
(*unlocks the function*)
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, leaving fork section');
{$endif}
LeaveLock(kernelForkLock);
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, out of fork section');
{$endif}
end;
(*===handles messages===*)
procedure KernelOnMessageHandler(message: TMessage);
begin
{$ifdef config_debug}
debug.DebugReportEvent('Kernel, got a message from [pid=' + inttostr(message.messageSender) + ']');
{$endif}
end;
begin
end.