forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransientobj_rps.cc
More file actions
462 lines (408 loc) · 14.8 KB
/
Copy pathtransientobj_rps.cc
File metadata and controls
462 lines (408 loc) · 14.8 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/****************************************************************
* file transientobj_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* Low-level implementation of transient objects and payloads
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright (C) 2023 - 2025 The Reflective Persistent System Team
* team@refpersys.org & http://refpersys.org/
*
* License:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "refpersys.hh"
extern "C" const char rps_transientobj_gitid[];
const char rps_transientobj_gitid[]= RPS_GITID;
extern "C" const char rps_transientobj_date[];
const char rps_transientobj_date[]= __DATE__;
extern "C" const char rps_transientobj_shortgitid[];
const char rps_transientobj_shortgitid[]= RPS_SHORTGITID;
////////////////////////////////////////////////////////////////
////// trensient unix process payload
Rps_PayloadUnixProcess::Rps_PayloadUnixProcess(Rps_ObjectZone*owner) // See PaylUnixProcess
: Rps_Payload(Rps_Type::PaylUnixProcess,owner),
_unixproc_pid(0),
_unixproc_exe(),
_unixproc_argv(),
_unixproc_closure(),
_unixproc_inputclos(),
_unixproc_outputclos(),
_unixproc_pipeinputfd(-1),
_unixproc_pipeoutputfd(-1),
_unixproc_cpu_time_limit(0),
_unixproc_elapsed_time_limit(0),
_unixproc_start_time(0),
_unixproc_as_mb_limit(0),
_unixproc_fsize_mb_limit(0),
_unixproc_core_mb_limit(0),
_unixproc_forbid_core(false),
_unixproc_nofile_limit(0)
{
} // end constructor Rps_PayloadUnixProcess
/// needed but never called
Rps_PayloadUnixProcess::Rps_PayloadUnixProcess(Rps_ObjectZone*owner, Rps_Loader*ld)
: Rps_Payload(Rps_Type::PaylUnixProcess,owner),
_unixproc_pid(0),
_unixproc_exe(),
_unixproc_argv(),
_unixproc_closure(),
_unixproc_inputclos(),
_unixproc_outputclos(),
_unixproc_pipeinputfd(-1),
_unixproc_pipeoutputfd(-1),
_unixproc_cpu_time_limit(0),
_unixproc_elapsed_time_limit(0),
_unixproc_start_time(0),
_unixproc_as_mb_limit(0),
_unixproc_fsize_mb_limit(0),
_unixproc_core_mb_limit(0),
_unixproc_forbid_core(false),
_unixproc_nofile_limit(0)
{
RPS_FATALOUT("cannot load payload of unix process for owner " << owner);
} // end constructor Rps_PayloadUnixProcess
Rps_PayloadUnixProcess::~Rps_PayloadUnixProcess()
{
} // end destructor Rps_PayloadUnixProcess
void
Rps_PayloadUnixProcess::add_process_argument(const std::string& arg)
{
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
_unixproc_argv.push_back(arg);
} // end Rps_PayloadUnixProcess::add_process_argument
void
Rps_PayloadUnixProcess::forbid_core_dump(void)
{
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
pid_t pid = _unixproc_pid.load();
_unixproc_forbid_core.store(true);
if (pid >0)
{
struct rlimit newlim= {.rlim_cur=0, .rlim_max= RLIM_INFINITY};
struct rlimit oldlim= {.rlim_cur=0, .rlim_max= 0};
prlimit(pid, RLIMIT_CORE, &newlim, &oldlim);
};
} // end Rps_PayloadUnixProcess::forbid_core_dump
unsigned
Rps_PayloadUnixProcess::core_megabytes_limit(unsigned newlimit)
{
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
pid_t pid = _unixproc_pid.load();
_unixproc_forbid_core.store(false);
if (pid >0)
{
struct rlimit newlim= {.rlim_cur=((newlimit>0)?(newlimit<<20):RLIM_INFINITY),
.rlim_max= RLIM_INFINITY
};
struct rlimit oldlim= {.rlim_cur=0, .rlim_max= 0};
(void)prlimit(pid, RLIMIT_CORE, &newlim, &oldlim);
if (oldlim.rlim_cur < RLIM_INFINITY)
return oldlim.rlim_cur>>20;
}
return _unixproc_core_mb_limit.exchange(newlimit);
} // end Rps_PayloadUnixProcess::core_megabytes_limit
unsigned
Rps_PayloadUnixProcess::address_space_megabytes_limit(unsigned newlimit)
{
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
pid_t pid = _unixproc_pid.load();
if (pid >0)
{
struct rlimit newlim= {.rlim_cur=(newlimit?(newlimit<<20):RLIM_INFINITY),
.rlim_max= RLIM_INFINITY
};
struct rlimit oldlim= {.rlim_cur=0, .rlim_max= 0};
if (!prlimit(pid, RLIMIT_AS, &newlim, &oldlim))
{
_unixproc_as_mb_limit.store(oldlim.rlim_cur>>20);
return oldlim.rlim_cur>>20;
}
};
return _unixproc_as_mb_limit.exchange(newlimit);
} // end Rps_PayloadUnixProcess::address_space_megabytes_limit
unsigned
Rps_PayloadUnixProcess::file_size_megabytes_limit(unsigned newlimit)
{
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
pid_t pid = _unixproc_pid.load();
if (pid >0)
{
struct rlimit newlim= {.rlim_cur=(newlimit?(newlimit<<20):RLIM_INFINITY),
.rlim_max= RLIM_INFINITY
};
struct rlimit oldlim= {.rlim_cur=0, .rlim_max= 0};
if (!prlimit(pid, RLIMIT_FSIZE, &newlim, &oldlim))
{
_unixproc_fsize_mb_limit.store(oldlim.rlim_cur>>20);
return oldlim.rlim_cur>>20;
}
};
return _unixproc_fsize_mb_limit.exchange(newlimit);
} // end Rps_PayloadUnixProcess::address_space_megabytes_limit
unsigned
Rps_PayloadUnixProcess::nofile_limit(unsigned newlimit)
{
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
pid_t pid = _unixproc_pid.load();
if (pid >0)
{
struct rlimit newlim= {.rlim_cur=(newlimit?newlimit:RLIM_INFINITY),
.rlim_max= RLIM_INFINITY
};
struct rlimit oldlim= {.rlim_cur=0, .rlim_max= 0};
if (!prlimit(pid, RLIMIT_NOFILE, &newlim, &oldlim))
{
_unixproc_fsize_mb_limit.store(oldlim.rlim_cur);
return oldlim.rlim_cur;
}
};
return _unixproc_fsize_mb_limit.exchange(newlimit);
} // end Rps_PayloadUnixProcess::nofile_limit
void
Rps_PayloadUnixProcess::dump_scan(Rps_Dumper*du) const
{
// do nothing, this payload for unix process is transient!
RPS_ASSERT(du);
} // end Rps_PayloadUnixProcess::dump_scan
void
Rps_PayloadUnixProcess::dump_json_content(Rps_Dumper*du, Json::Value&) const
{
// do nothing, this payload for unix process is transient!
RPS_ASSERT(du);
} // end Rps_PayloadUnixProcess::dump_scan
bool
Rps_PayloadUnixProcess::is_erasable(void) const
{
return false;
} // end Rps_PayloadUnixProcess::is_erasable
void
Rps_PayloadUnixProcess::gc_mark(Rps_GarbageCollector&gc) const
{
if (_unixproc_closure)
{
RPS_ASSERT(_unixproc_closure.is_closure());
_unixproc_closure.gc_mark(gc,1);
}
} // end Rps_PayloadUnixProcess::gc_mark
const Rps_ClosureValue
Rps_PayloadUnixProcess::get_process_closure(void) const
{
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
return _unixproc_closure;
} // end Rps_PayloadUnixProcess::get_process_closure
void
Rps_PayloadUnixProcess::put_process_closure(Rps_ClosureValue closv)
{
if (!closv || !closv.is_closure()) return;
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
_unixproc_closure = closv;
} // end Rps_PayloadUnixProcess::put_process_closure
const Rps_ClosureValue
Rps_PayloadUnixProcess::get_input_closure(void) const
{
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
return _unixproc_inputclos;
} // end Rps_PayloadUnixProcess::get_input_closure
void
Rps_PayloadUnixProcess::put_input_closure(Rps_ClosureValue closv)
{
if (!closv || !closv.is_closure()) return;
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
_unixproc_inputclos = closv;
} // end Rps_PayloadUnixProcess::put_input_closure
const Rps_ClosureValue
Rps_PayloadUnixProcess::get_output_closure(void) const
{
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
return _unixproc_outputclos;
} // end Rps_PayloadUnixProcess::get_output_closure
void
Rps_PayloadUnixProcess::put_output_closure(Rps_ClosureValue closv)
{
if (!closv || !closv.is_closure()) return;
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
_unixproc_outputclos = closv;
} // end Rps_PayloadUnixProcess::put_output_closure
/// static member function to create a dormant (potential, not yet forked) unix process object
Rps_ObjectRef
Rps_PayloadUnixProcess::make_dormant_unix_process_object(Rps_CallFrame*callerframe,
const std::string& exec)
{
if (exec.empty())
throw std::runtime_error("no executable given to make_dormant_unix_process");
RPS_ASSERT(callerframe && callerframe->is_good_call_frame());
RPS_LOCALFRAME(RPS_ROOT_OB(_61uFnhRCXfe00Mir2n), //unix_process∈class
callerframe,
Rps_ObjectRef obres;
);
std::string realexestr;
if (exec.find("/") > 0)
{
char *realexepath = ::realpath(exec.c_str(), nullptr);
if (!realexepath)
throw RPS_RUNTIME_ERROR_OUT("cannot make_dormant_unix_process_object from executable " << Rps_QuotedC_String(exec)
<< " without a real path for " << exec);
realexestr = std::string{realexepath};
free (realexepath);
}
else
{
const char*curpath = getenv("PATH");
RPS_ASSERT(curpath);
const char*pc= curpath;
const char*next = nullptr;
do
{
std::string dir;
next = nullptr;
const char*colon = strchr(pc, ':');
if (colon)
{
next = colon+1;
dir = std::string(pc, colon-1);
if (dir.empty())
dir= std::string{"."};
dir += '/';
}
else
{
dir = std::string(pc);
next= nullptr;
};
std::string curexepath = dir + exec;
if (!access(curexepath.c_str(), X_OK))
{
realexestr = curexepath;
break;
};
pc = next;
}
while(realexestr.empty());
};
_f.obres = Rps_ObjectRef::make_object(&_, RPS_ROOT_OB(_61uFnhRCXfe00Mir2n)); //unix_process∈class
Rps_PayloadUnixProcess* payl = //
_f.obres->put_new_plain_payload<Rps_PayloadUnixProcess>(); //
payl->_unixproc_exe = realexestr;
payl->_unixproc_argv.push_back(exec);
return _f.obres;
} // end Rps_PayloadUnixProcess::make_dormant_unix_process_object
std::set<Rps_PayloadUnixProcess*>
Rps_PayloadUnixProcess::set_of_runnable_processes;
std::mutex
Rps_PayloadUnixProcess::mtx_of_runnable_processes;
std::deque<Rps_PayloadUnixProcess*>
Rps_PayloadUnixProcess::queue_of_runnable_processes;
void
Rps_PayloadUnixProcess::gc_mark_active_processes(Rps_GarbageCollector&gc)
{
std::lock_guard<std::mutex> gu(mtx_of_runnable_processes);
/// Both set_of_runnable_processes and queue_of_runnable_processes
/// should contain the same objects, but for sure we want to mark
/// both.
for (Rps_PayloadUnixProcess*paylup : set_of_runnable_processes)
{
paylup->owner()->gc_mark(gc);
}
for (Rps_PayloadUnixProcess*paylup : queue_of_runnable_processes)
{
paylup->owner()->gc_mark(gc);
}
} // end Rps_PayloadUnixProcess::gc_mark_active_processes
void
Rps_PayloadUnixProcess::start_process(Rps_CallFrame*callframe)
{
RPS_ASSERT(!callframe || callframe->is_good_call_frame());
std::lock_guard<std::mutex> rungu(mtx_of_runnable_processes);
std::lock_guard<std::recursive_mutex> gu(*owner()->objmtxptr());
if (_unixproc_pid.load()>0)
{
RPS_WARNOUT("already running Rps_PayloadUnixProcess owned by " << owner()
<< std::endl << Rps_ShowCallFrame(callframe));
throw std::runtime_error("already running Rps_PayloadUnixProcess");
}
queue_of_runnable_processes.push_back(this);
rps_postpone_child_process();
/// code in eventloop_rps.cc should be related.
} // end Rps_PayloadUnixProcess::start_process
void
Rps_PayloadUnixProcess::do_on_active_process_queue(std::function<void(Rps_ObjectRef, Rps_CallFrame*,void*)> fun,
Rps_CallFrame*callframe, void*client_data)
{
std::lock_guard<std::mutex> gu(mtx_of_runnable_processes);
RPS_ASSERT(!callframe || callframe->is_good_call_frame());
for (Rps_PayloadUnixProcess*paylup : queue_of_runnable_processes)
{
Rps_ObjectRef obown = paylup->owner();
std::lock_guard<std::recursive_mutex> gu(*obown->objmtxptr());
RPS_DEBUG_LOG(REPL,
"Rps_PayloadUnixProcess::do_on_active_process_queue obown="
<< obown << std::endl
<< Rps_ShowCallFrame(callframe)
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1,"Rps_PayloadUnixProcess::do_on_active_process_queue"));
fun(obown,callframe,client_data);
}
} // end Rps_PayloadUnixProcess::do_on_active_process_queue
///////////////////////////////////////
///// transient popened file payload
Rps_PayloadPopenedFile::Rps_PayloadPopenedFile(Rps_ObjectZone*owner, const std::string command, bool reading) // See PaylPopenedFile
: Rps_Payload(Rps_Type::PaylPopenedFile,owner),
_popened_cmd(command),
_popened_to_read(reading),
_popened_file(nullptr)
{
} // end constructor Rps_PayloadPopenedFile
//// needed but never called
Rps_PayloadPopenedFile::Rps_PayloadPopenedFile(Rps_ObjectZone*owner, Rps_Loader*ld)
: Rps_Payload(Rps_Type::PaylPopenedFile,owner),
_popened_cmd(),
_popened_to_read(true),
_popened_file(nullptr)
{
RPS_FATALOUT("cannot load payload of popened file for owner " << owner);
} // end constructor Rps_PayloadUnixProcess
Rps_PayloadPopenedFile::~Rps_PayloadPopenedFile()
{
} // end destructor Rps_PayloadPopenedFile
void
Rps_PayloadPopenedFile::dump_scan(Rps_Dumper*du) const
{
// do nothing, this payload for unix process is transient!
RPS_ASSERT(du);
} // end Rps_PayloadPopenedFile::dump_scan
void
Rps_PayloadPopenedFile::dump_json_content(Rps_Dumper*du, Json::Value&) const
{
// do nothing, this payload for unix process is transient!
RPS_ASSERT(du);
} // end Rps_PayloadPopenedFile::dump_scan
bool
Rps_PayloadPopenedFile::is_erasable(void) const
{
return false;
} // end Rps_PayloadPopenedFile::is_erasable
void
Rps_PayloadPopenedFile::gc_mark(Rps_GarbageCollector&gc) const
{
} // end Rps_PayloadPopenedFile::gc_mark
#warning transientobj_rps.cc is probably incomplete
/*** end of file transientobj_rps.cc ***/