Process.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. #include "types.h"
  2. #include "Process.h"
  3. #include "kmalloc.h"
  4. #include "VGA.h"
  5. #include "StdLib.h"
  6. #include "i386.h"
  7. #include "system.h"
  8. #include <VirtualFileSystem/FileDescriptor.h>
  9. #include <VirtualFileSystem/VirtualFileSystem.h>
  10. #include <ELFLoader/ELFLoader.h>
  11. #include "MemoryManager.h"
  12. #include "errno.h"
  13. #include "i8253.h"
  14. #include "RTC.h"
  15. #include "ProcFileSystem.h"
  16. #include <AK/StdLib.h>
  17. #include <LibC/signal_numbers.h>
  18. #include "Syscall.h"
  19. #include "Scheduler.h"
  20. //#define DEBUG_IO
  21. //#define TASK_DEBUG
  22. //#define FORK_DEBUG
  23. #define SIGNAL_DEBUG
  24. #define MAX_PROCESS_GIDS 32
  25. // FIXME: Only do a single validation for accesses that don't span multiple pages.
  26. // FIXME: Some places pass strlen(arg1) as arg2. This doesn't seem entirely perfect..
  27. #define VALIDATE_USER_READ(b, s) \
  28. do { \
  29. LinearAddress laddr((dword)(b)); \
  30. if (!validate_user_read(laddr) || !validate_user_read(laddr.offset((s) - 1))) { \
  31. dbgprintf("Bad read address passed to syscall: %p +%u\n", laddr.get(), (s)); \
  32. return -EFAULT; \
  33. } \
  34. } while(0)
  35. #define VALIDATE_USER_WRITE(b, s) \
  36. do { \
  37. LinearAddress laddr((dword)(b)); \
  38. if (!validate_user_write(laddr) || !validate_user_write(laddr.offset((s) - 1))) { \
  39. dbgprintf("Bad write address passed to syscall: %p +%u\n", laddr.get(), (s)); \
  40. return -EFAULT; \
  41. } \
  42. } while(0)
  43. static const DWORD defaultStackSize = 16384;
  44. static pid_t next_pid;
  45. InlineLinkedList<Process>* g_processes;
  46. static String* s_hostname;
  47. static String& hostnameStorage(InterruptDisabler&)
  48. {
  49. ASSERT(s_hostname);
  50. return *s_hostname;
  51. }
  52. static String getHostname()
  53. {
  54. InterruptDisabler disabler;
  55. return hostnameStorage(disabler).isolatedCopy();
  56. }
  57. CoolGlobals* g_cool_globals;
  58. void Process::initialize()
  59. {
  60. #ifdef COOL_GLOBALS
  61. g_cool_globals = reinterpret_cast<CoolGlobals*>(0x1000);
  62. #endif
  63. next_pid = 0;
  64. g_processes = new InlineLinkedList<Process>;
  65. s_hostname = new String("birx");
  66. Scheduler::initialize();
  67. }
  68. Vector<Process*> Process::allProcesses()
  69. {
  70. InterruptDisabler disabler;
  71. Vector<Process*> processes;
  72. processes.ensureCapacity(g_processes->sizeSlow());
  73. for (auto* process = g_processes->head(); process; process = process->next())
  74. processes.append(process);
  75. return processes;
  76. }
  77. Region* Process::allocate_region(LinearAddress laddr, size_t size, String&& name, bool is_readable, bool is_writable)
  78. {
  79. // FIXME: This needs sanity checks. What if this overlaps existing regions?
  80. if (laddr.is_null()) {
  81. laddr = m_nextRegion;
  82. m_nextRegion = m_nextRegion.offset(size).offset(PAGE_SIZE);
  83. }
  84. laddr.mask(0xfffff000);
  85. unsigned page_count = ceilDiv(size, PAGE_SIZE);
  86. auto physical_pages = MM.allocate_physical_pages(page_count);
  87. ASSERT(physical_pages.size() == page_count);
  88. m_regions.append(adopt(*new Region(laddr, size, move(physical_pages), move(name), is_readable, is_writable)));
  89. MM.mapRegion(*this, *m_regions.last());
  90. return m_regions.last().ptr();
  91. }
  92. bool Process::deallocate_region(Region& region)
  93. {
  94. InterruptDisabler disabler;
  95. for (size_t i = 0; i < m_regions.size(); ++i) {
  96. if (m_regions[i].ptr() == &region) {
  97. MM.unmapRegion(*this, region);
  98. m_regions.remove(i);
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. Region* Process::regionFromRange(LinearAddress laddr, size_t size)
  105. {
  106. for (auto& region : m_regions) {
  107. if (region->linearAddress == laddr && region->size == size)
  108. return region.ptr();
  109. }
  110. return nullptr;
  111. }
  112. int Process::sys$set_mmap_name(void* addr, size_t size, const char* name)
  113. {
  114. VALIDATE_USER_READ(name, strlen(name));
  115. auto* region = regionFromRange(LinearAddress((dword)addr), size);
  116. if (!region)
  117. return -EINVAL;
  118. region->name = name;
  119. return 0;
  120. }
  121. void* Process::sys$mmap(void* addr, size_t size)
  122. {
  123. InterruptDisabler disabler;
  124. // FIXME: Implement mapping at a client-preferred address.
  125. ASSERT(addr == nullptr);
  126. auto* region = allocate_region(LinearAddress(), size, "mmap");
  127. if (!region)
  128. return (void*)-1;
  129. MM.mapRegion(*this, *region);
  130. return (void*)region->linearAddress.get();
  131. }
  132. int Process::sys$munmap(void* addr, size_t size)
  133. {
  134. InterruptDisabler disabler;
  135. auto* region = regionFromRange(LinearAddress((dword)addr), size);
  136. if (!region)
  137. return -1;
  138. if (!deallocate_region(*region))
  139. return -1;
  140. return 0;
  141. }
  142. int Process::sys$gethostname(char* buffer, size_t size)
  143. {
  144. VALIDATE_USER_WRITE(buffer, size);
  145. auto hostname = getHostname();
  146. if (size < (hostname.length() + 1))
  147. return -ENAMETOOLONG;
  148. memcpy(buffer, hostname.characters(), size);
  149. return 0;
  150. }
  151. Process* Process::fork(RegisterDump& regs)
  152. {
  153. auto* child = new Process(String(m_name), m_uid, m_gid, m_pid, m_ring, m_cwd.copyRef(), m_executable.copyRef(), m_tty, this);
  154. #ifdef FORK_DEBUG
  155. dbgprintf("fork: child=%p\n", child);
  156. #endif
  157. #if 0
  158. // FIXME: An honest fork() would copy these. Needs a Vector copy ctor.
  159. child->m_arguments = m_arguments;
  160. child->m_initialEnvironment = m_initialEnvironment;
  161. #endif
  162. for (auto& region : m_regions) {
  163. #ifdef FORK_DEBUG
  164. dbgprintf("fork: cloning Region{%p}\n", region.ptr());
  165. #endif
  166. auto cloned_region = region->clone();
  167. child->m_regions.append(move(cloned_region));
  168. MM.mapRegion(*child, *child->m_regions.last());
  169. }
  170. child->m_tss.eax = 0; // fork() returns 0 in the child :^)
  171. child->m_tss.ebx = regs.ebx;
  172. child->m_tss.ecx = regs.ecx;
  173. child->m_tss.edx = regs.edx;
  174. child->m_tss.ebp = regs.ebp;
  175. child->m_tss.esp = regs.esp_if_crossRing;
  176. child->m_tss.esi = regs.esi;
  177. child->m_tss.edi = regs.edi;
  178. child->m_tss.eflags = regs.eflags;
  179. child->m_tss.eip = regs.eip;
  180. child->m_tss.cs = regs.cs;
  181. child->m_tss.ds = regs.ds;
  182. child->m_tss.es = regs.es;
  183. child->m_tss.fs = regs.fs;
  184. child->m_tss.gs = regs.gs;
  185. child->m_tss.ss = regs.ss_if_crossRing;
  186. #ifdef FORK_DEBUG
  187. dbgprintf("fork: child will begin executing at %w:%x with stack %w:%x\n", child->m_tss.cs, child->m_tss.eip, child->m_tss.ss, child->m_tss.esp);
  188. #endif
  189. ProcFileSystem::the().addProcess(*child);
  190. g_processes->prepend(child);
  191. system.nprocess++;
  192. #ifdef TASK_DEBUG
  193. kprintf("Process %u (%s) forked from %u @ %p\n", child->pid(), child->name().characters(), m_pid, child->m_tss.eip);
  194. #endif
  195. return child;
  196. }
  197. pid_t Process::sys$fork(RegisterDump& regs)
  198. {
  199. auto* child = fork(regs);
  200. ASSERT(child);
  201. return child->pid();
  202. }
  203. int Process::exec(const String& path, Vector<String>&& arguments, Vector<String>&& environment)
  204. {
  205. auto parts = path.split('/');
  206. if (parts.isEmpty())
  207. return -ENOENT;
  208. int error;
  209. auto descriptor = VirtualFileSystem::the().open(path, error, 0, m_cwd ? m_cwd->inode : InodeIdentifier());
  210. if (!descriptor) {
  211. ASSERT(error != 0);
  212. return error;
  213. }
  214. if (!descriptor->metadata().mayExecute(m_euid, m_gids))
  215. return -EACCES;
  216. auto elfData = descriptor->readEntireFile();
  217. if (!elfData)
  218. return -EIO; // FIXME: Get a more detailed error from VFS.
  219. dword entry_eip = 0;
  220. PageDirectory* old_page_directory;
  221. PageDirectory* new_page_directory;
  222. {
  223. InterruptDisabler disabler;
  224. // Okay, here comes the sleight of hand, pay close attention..
  225. auto old_regions = move(m_regions);
  226. old_page_directory = m_page_directory;
  227. new_page_directory = reinterpret_cast<PageDirectory*>(kmalloc_page_aligned(sizeof(PageDirectory)));
  228. MM.populate_page_directory(*new_page_directory);
  229. m_page_directory = new_page_directory;
  230. MM.enter_process_paging_scope(*this);
  231. ELFLoader loader(move(elfData));
  232. loader.alloc_section_hook = [&] (LinearAddress laddr, size_t size, size_t alignment, bool is_readable, bool is_writable, const String& name) {
  233. ASSERT(size);
  234. size = ((size / 4096) + 1) * 4096; // FIXME: Use ceil_div?
  235. (void) allocate_region(laddr, size, String(name), is_readable, is_writable);
  236. return laddr.asPtr();
  237. };
  238. bool success = loader.load();
  239. if (!success) {
  240. m_page_directory = old_page_directory;
  241. MM.enter_process_paging_scope(*this);
  242. MM.release_page_directory(*new_page_directory);
  243. m_regions = move(old_regions);
  244. kprintf("sys$execve: Failure loading %s\n", path.characters());
  245. return -ENOEXEC;
  246. }
  247. entry_eip = (dword)loader.symbol_ptr("_start");
  248. if (!entry_eip) {
  249. m_page_directory = old_page_directory;
  250. MM.enter_process_paging_scope(*this);
  251. MM.release_page_directory(*new_page_directory);
  252. m_regions = move(old_regions);
  253. return -ENOEXEC;
  254. }
  255. }
  256. InterruptDisabler disabler;
  257. Scheduler::prepare_to_modify_tss(*this);
  258. m_name = parts.takeLast();
  259. dword old_esp0 = m_tss.esp0;
  260. memset(&m_tss, 0, sizeof(m_tss));
  261. m_tss.eflags = 0x0202;
  262. m_tss.eip = entry_eip;
  263. m_tss.cs = 0x1b;
  264. m_tss.ds = 0x23;
  265. m_tss.es = 0x23;
  266. m_tss.fs = 0x23;
  267. m_tss.gs = 0x23;
  268. m_tss.ss = 0x23;
  269. m_tss.cr3 = (dword)m_page_directory;
  270. m_stack_region = allocate_region(LinearAddress(), defaultStackSize, "stack");
  271. ASSERT(m_stack_region);
  272. m_stackTop3 = m_stack_region->linearAddress.offset(defaultStackSize).get() & 0xfffffff8;
  273. m_tss.esp = m_stackTop3;
  274. m_tss.ss0 = 0x10;
  275. m_tss.esp0 = old_esp0;
  276. m_tss.ss2 = m_pid;
  277. MM.release_page_directory(*old_page_directory);
  278. m_executable = descriptor->vnode();
  279. m_arguments = move(arguments);
  280. m_initialEnvironment = move(environment);
  281. #ifdef TASK_DEBUG
  282. kprintf("Process %u (%s) exec'd %s @ %p\n", pid(), name().characters(), path.characters(), m_tss.eip);
  283. #endif
  284. set_state(Skip1SchedulerPass);
  285. if (current == this) {
  286. bool success = Scheduler::yield();
  287. ASSERT(success);
  288. }
  289. return 0;
  290. }
  291. int Process::sys$execve(const char* filename, const char** argv, const char** envp)
  292. {
  293. VALIDATE_USER_READ(filename, strlen(filename));
  294. if (argv) {
  295. for (size_t i = 0; argv[i]; ++i) {
  296. VALIDATE_USER_READ(argv[i], strlen(argv[i]));
  297. }
  298. }
  299. if (envp) {
  300. for (size_t i = 0; envp[i]; ++i) {
  301. VALIDATE_USER_READ(envp[i], strlen(envp[i]));
  302. }
  303. }
  304. String path(filename);
  305. auto parts = path.split('/');
  306. Vector<String> arguments;
  307. if (argv) {
  308. for (size_t i = 0; argv[i]; ++i) {
  309. arguments.append(argv[i]);
  310. }
  311. } else {
  312. arguments.append(parts.last());
  313. }
  314. Vector<String> environment;
  315. if (envp) {
  316. for (size_t i = 0; envp[i]; ++i) {
  317. environment.append(envp[i]);
  318. }
  319. }
  320. int rc = exec(path, move(arguments), move(environment));
  321. ASSERT(rc < 0); // We should never continue after a successful exec!
  322. return rc;
  323. }
  324. pid_t Process::sys$spawn(const char* filename, const char** argv, const char** envp)
  325. {
  326. VALIDATE_USER_READ(filename, strlen(filename));
  327. if (argv) {
  328. for (size_t i = 0; argv[i]; ++i) {
  329. VALIDATE_USER_READ(argv[i], strlen(argv[i]));
  330. }
  331. }
  332. if (envp) {
  333. for (size_t i = 0; envp[i]; ++i) {
  334. VALIDATE_USER_READ(envp[i], strlen(envp[i]));
  335. }
  336. }
  337. String path(filename);
  338. auto parts = path.split('/');
  339. Vector<String> arguments;
  340. if (argv) {
  341. for (size_t i = 0; argv[i]; ++i) {
  342. arguments.append(argv[i]);
  343. }
  344. } else {
  345. arguments.append(parts.last());
  346. }
  347. Vector<String> environment;
  348. if (envp) {
  349. for (size_t i = 0; envp[i]; ++i) {
  350. environment.append(envp[i]);
  351. }
  352. }
  353. int error;
  354. auto* child = create_user_process(path, m_uid, m_gid, m_pid, error, move(arguments), move(environment), m_tty);
  355. if (child)
  356. return child->pid();
  357. return error;
  358. }
  359. Process* Process::create_user_process(const String& path, uid_t uid, gid_t gid, pid_t parent_pid, int& error, Vector<String>&& arguments, Vector<String>&& environment, TTY* tty)
  360. {
  361. // FIXME: Don't split() the path twice (sys$spawn also does it...)
  362. auto parts = path.split('/');
  363. if (arguments.isEmpty()) {
  364. arguments.append(parts.last());
  365. }
  366. RetainPtr<VirtualFileSystem::Node> cwd;
  367. {
  368. InterruptDisabler disabler;
  369. if (auto* parent = Process::from_pid(parent_pid))
  370. cwd = parent->m_cwd.copyRef();
  371. }
  372. if (!cwd)
  373. cwd = VirtualFileSystem::the().root();
  374. auto* process = new Process(parts.takeLast(), uid, gid, parent_pid, Ring3, move(cwd), nullptr, tty);
  375. error = process->exec(path, move(arguments), move(environment));
  376. if (error != 0)
  377. return nullptr;
  378. ProcFileSystem::the().addProcess(*process);
  379. g_processes->prepend(process);
  380. system.nprocess++;
  381. #ifdef TASK_DEBUG
  382. kprintf("Process %u (%s) spawned @ %p\n", process->pid(), process->name().characters(), process->m_tss.eip);
  383. #endif
  384. error = 0;
  385. return process;
  386. }
  387. int Process::sys$get_environment(char*** environ)
  388. {
  389. auto* region = allocate_region(LinearAddress(), PAGE_SIZE, "environ");
  390. if (!region)
  391. return -ENOMEM;
  392. MM.mapRegion(*this, *region);
  393. char* envpage = (char*)region->linearAddress.get();
  394. *environ = (char**)envpage;
  395. char* bufptr = envpage + (sizeof(char*) * (m_initialEnvironment.size() + 1));
  396. for (size_t i = 0; i < m_initialEnvironment.size(); ++i) {
  397. (*environ)[i] = bufptr;
  398. memcpy(bufptr, m_initialEnvironment[i].characters(), m_initialEnvironment[i].length());
  399. bufptr += m_initialEnvironment[i].length();
  400. *(bufptr++) = '\0';
  401. }
  402. (*environ)[m_initialEnvironment.size()] = nullptr;
  403. return 0;
  404. }
  405. int Process::sys$get_arguments(int* argc, char*** argv)
  406. {
  407. auto* region = allocate_region(LinearAddress(), PAGE_SIZE, "argv");
  408. if (!region)
  409. return -ENOMEM;
  410. MM.mapRegion(*this, *region);
  411. char* argpage = (char*)region->linearAddress.get();
  412. *argc = m_arguments.size();
  413. *argv = (char**)argpage;
  414. char* bufptr = argpage + (sizeof(char*) * m_arguments.size());
  415. for (size_t i = 0; i < m_arguments.size(); ++i) {
  416. (*argv)[i] = bufptr;
  417. memcpy(bufptr, m_arguments[i].characters(), m_arguments[i].length());
  418. bufptr += m_arguments[i].length();
  419. *(bufptr++) = '\0';
  420. }
  421. return 0;
  422. }
  423. Process* Process::create_kernel_process(void (*e)(), String&& name)
  424. {
  425. auto* process = new Process(move(name), (uid_t)0, (gid_t)0, (pid_t)0, Ring0);
  426. process->m_tss.eip = (dword)e;
  427. if (process->pid() != 0) {
  428. InterruptDisabler disabler;
  429. g_processes->prepend(process);
  430. system.nprocess++;
  431. ProcFileSystem::the().addProcess(*process);
  432. #ifdef TASK_DEBUG
  433. kprintf("Kernel process %u (%s) spawned @ %p\n", process->pid(), process->name().characters(), process->m_tss.eip);
  434. #endif
  435. }
  436. return process;
  437. }
  438. Process::Process(String&& name, uid_t uid, gid_t gid, pid_t ppid, RingLevel ring, RetainPtr<VirtualFileSystem::Node>&& cwd, RetainPtr<VirtualFileSystem::Node>&& executable, TTY* tty, Process* fork_parent)
  439. : m_name(move(name))
  440. , m_pid(next_pid++) // FIXME: RACE: This variable looks racy!
  441. , m_uid(uid)
  442. , m_gid(gid)
  443. , m_euid(uid)
  444. , m_egid(gid)
  445. , m_state(Runnable)
  446. , m_ring(ring)
  447. , m_cwd(move(cwd))
  448. , m_executable(move(executable))
  449. , m_tty(tty)
  450. , m_ppid(ppid)
  451. {
  452. m_gids.set(m_gid);
  453. if (fork_parent) {
  454. m_sid = fork_parent->m_sid;
  455. m_pgid = fork_parent->m_pgid;
  456. } else {
  457. // FIXME: Use a ProcessHandle? Presumably we're executing *IN* the parent right now though..
  458. InterruptDisabler disabler;
  459. if (auto* parent = Process::from_pid(m_ppid)) {
  460. m_sid = parent->m_sid;
  461. m_pgid = parent->m_pgid;
  462. }
  463. }
  464. m_page_directory = (PageDirectory*)kmalloc_page_aligned(sizeof(PageDirectory));
  465. MM.populate_page_directory(*m_page_directory);
  466. if (fork_parent) {
  467. m_file_descriptors.resize(fork_parent->m_file_descriptors.size());
  468. for (size_t i = 0; i < fork_parent->m_file_descriptors.size(); ++i) {
  469. if (!fork_parent->m_file_descriptors[i])
  470. continue;
  471. #ifdef FORK_DEBUG
  472. dbgprintf("fork: cloning fd %u... (%p) istty? %um\n", i, fork_parent->m_file_descriptors[i].ptr(), fork_parent->m_file_descriptors[i]->isTTY());
  473. #endif
  474. m_file_descriptors[i] = fork_parent->m_file_descriptors[i]->clone();
  475. }
  476. } else {
  477. m_file_descriptors.resize(m_max_open_file_descriptors);
  478. if (tty) {
  479. m_file_descriptors[0] = tty->open(O_RDONLY);
  480. m_file_descriptors[1] = tty->open(O_WRONLY);
  481. m_file_descriptors[2] = tty->open(O_WRONLY);
  482. }
  483. }
  484. if (fork_parent)
  485. m_nextRegion = fork_parent->m_nextRegion;
  486. else
  487. m_nextRegion = LinearAddress(0x10000000);
  488. if (fork_parent) {
  489. memcpy(&m_tss, &fork_parent->m_tss, sizeof(m_tss));
  490. } else {
  491. memset(&m_tss, 0, sizeof(m_tss));
  492. // Only IF is set when a process boots.
  493. m_tss.eflags = 0x0202;
  494. word cs, ds, ss;
  495. if (isRing0()) {
  496. cs = 0x08;
  497. ds = 0x10;
  498. ss = 0x10;
  499. } else {
  500. cs = 0x1b;
  501. ds = 0x23;
  502. ss = 0x23;
  503. }
  504. m_tss.ds = ds;
  505. m_tss.es = ds;
  506. m_tss.fs = ds;
  507. m_tss.gs = ds;
  508. m_tss.ss = ss;
  509. m_tss.cs = cs;
  510. }
  511. m_tss.cr3 = (dword)m_page_directory;
  512. if (isRing0()) {
  513. // FIXME: This memory is leaked.
  514. // But uh, there's also no kernel process termination, so I guess it's not technically leaked...
  515. dword stackBottom = (dword)kmalloc_eternal(defaultStackSize);
  516. m_stackTop0 = (stackBottom + defaultStackSize) & 0xffffff8;
  517. m_tss.esp = m_stackTop0;
  518. } else {
  519. if (fork_parent) {
  520. m_stackTop3 = fork_parent->m_stackTop3;
  521. } else {
  522. auto* region = allocate_region(LinearAddress(), defaultStackSize, "stack");
  523. ASSERT(region);
  524. m_stackTop3 = region->linearAddress.offset(defaultStackSize).get() & 0xfffffff8;
  525. m_tss.esp = m_stackTop3;
  526. }
  527. }
  528. if (isRing3()) {
  529. // Ring3 processes need a separate stack for Ring0.
  530. m_kernelStack = kmalloc(defaultStackSize);
  531. m_stackTop0 = ((DWORD)m_kernelStack + defaultStackSize) & 0xffffff8;
  532. m_tss.ss0 = 0x10;
  533. m_tss.esp0 = m_stackTop0;
  534. }
  535. // HACK: Ring2 SS in the TSS is the current PID.
  536. m_tss.ss2 = m_pid;
  537. m_farPtr.offset = 0x98765432;
  538. }
  539. Process::~Process()
  540. {
  541. InterruptDisabler disabler;
  542. ProcFileSystem::the().removeProcess(*this);
  543. system.nprocess--;
  544. gdt_free_entry(selector());
  545. if (m_kernelStack) {
  546. kfree(m_kernelStack);
  547. m_kernelStack = nullptr;
  548. }
  549. MM.release_page_directory(*m_page_directory);
  550. }
  551. void Process::dumpRegions()
  552. {
  553. kprintf("Process %s(%u) regions:\n", name().characters(), pid());
  554. kprintf("BEGIN END SIZE NAME\n");
  555. for (auto& region : m_regions) {
  556. kprintf("%x -- %x %x %s\n",
  557. region->linearAddress.get(),
  558. region->linearAddress.offset(region->size - 1).get(),
  559. region->size,
  560. region->name.characters());
  561. }
  562. }
  563. void Process::sys$exit(int status)
  564. {
  565. cli();
  566. #ifdef TASK_DEBUG
  567. kprintf("sys$exit: %s(%u) exit with status %d\n", name().characters(), pid(), status);
  568. #endif
  569. set_state(Dead);
  570. m_termination_status = status;
  571. m_termination_signal = 0;
  572. Scheduler::pick_next_and_switch_now();
  573. ASSERT_NOT_REACHED();
  574. }
  575. void Process::terminate_due_to_signal(byte signal)
  576. {
  577. ASSERT_INTERRUPTS_DISABLED();
  578. ASSERT(signal < 32);
  579. dbgprintf("terminate_due_to_signal %s(%u) <- %u\n", name().characters(), pid(), signal);
  580. m_termination_status = 0;
  581. m_termination_signal = signal;
  582. set_state(Dead);
  583. }
  584. void Process::send_signal(byte signal, Process* sender)
  585. {
  586. ASSERT_INTERRUPTS_DISABLED();
  587. ASSERT(signal < 32);
  588. m_pending_signals |= 1 << signal;
  589. if (sender)
  590. dbgprintf("signal: %s(%u) sent %d to %s(%u)\n", sender->name().characters(), sender->pid(), signal, name().characters(), pid());
  591. else
  592. dbgprintf("signal: kernel sent %d to %s(%u)\n", signal, name().characters(), pid());
  593. }
  594. bool Process::has_unmasked_pending_signals() const
  595. {
  596. return m_pending_signals & ~m_signal_mask;
  597. }
  598. void Process::dispatch_one_pending_signal()
  599. {
  600. ASSERT_INTERRUPTS_DISABLED();
  601. dword signal_candidates = m_pending_signals & ~m_signal_mask;
  602. ASSERT(signal_candidates);
  603. byte signal = 0;
  604. for (; signal < 32; ++signal) {
  605. if (signal_candidates & (1 << signal)) {
  606. break;
  607. }
  608. }
  609. dispatch_signal(signal);
  610. }
  611. void Process::dispatch_signal(byte signal)
  612. {
  613. ASSERT_INTERRUPTS_DISABLED();
  614. ASSERT(signal < 32);
  615. dbgprintf("dispatch_signal %s(%u) <- %u\n", name().characters(), pid(), signal);
  616. auto& action = m_signal_action_data[signal];
  617. // FIXME: Implement SA_SIGINFO signal handlers.
  618. ASSERT(!(action.flags & SA_SIGINFO));
  619. auto handler_laddr = action.handler_or_sigaction;
  620. if (handler_laddr.is_null()) {
  621. // FIXME: Is termination really always the appropriate action?
  622. return terminate_due_to_signal(signal);
  623. }
  624. Scheduler::prepare_to_modify_tss(*this);
  625. word ret_cs = m_tss.cs;
  626. dword ret_eip = m_tss.eip;
  627. dword ret_eflags = m_tss.eflags;
  628. bool interrupting_in_kernel = (ret_cs & 3) == 0;
  629. if (interrupting_in_kernel) {
  630. dbgprintf("dispatch_signal to %s(%u) in state=%s with return to %w:%x\n", name().characters(), pid(), toString(state()), ret_cs, ret_eip);
  631. ASSERT(is_blocked());
  632. m_tss_to_resume_kernel = m_tss;
  633. #ifdef SIGNAL_DEBUG
  634. dbgprintf("resume tss pc: %w:%x\n", m_tss_to_resume_kernel.cs, m_tss_to_resume_kernel.eip);
  635. #endif
  636. }
  637. ProcessPagingScope pagingScope(*this);
  638. if (interrupting_in_kernel) {
  639. if (!m_signal_stack_user_region) {
  640. m_signal_stack_user_region = allocate_region(LinearAddress(), defaultStackSize, "signal stack (user)");
  641. ASSERT(m_signal_stack_user_region);
  642. m_signal_stack_kernel_region = allocate_region(LinearAddress(), defaultStackSize, "signal stack (kernel)");
  643. ASSERT(m_signal_stack_user_region);
  644. }
  645. m_tss.ss = 0x23;
  646. m_tss.esp = m_signal_stack_user_region->linearAddress.offset(defaultStackSize).get() & 0xfffffff8;
  647. m_tss.ss0 = 0x10;
  648. m_tss.esp0 = m_signal_stack_kernel_region->linearAddress.offset(defaultStackSize).get() & 0xfffffff8;
  649. push_value_on_stack(ret_eflags);
  650. push_value_on_stack(ret_cs);
  651. push_value_on_stack(ret_eip);
  652. } else {
  653. push_value_on_stack(ret_cs);
  654. push_value_on_stack(ret_eip);
  655. push_value_on_stack(ret_eflags);
  656. }
  657. // PUSHA
  658. dword old_esp = m_tss.esp;
  659. push_value_on_stack(m_tss.eax);
  660. push_value_on_stack(m_tss.ecx);
  661. push_value_on_stack(m_tss.edx);
  662. push_value_on_stack(m_tss.ebx);
  663. push_value_on_stack(old_esp);
  664. push_value_on_stack(m_tss.ebp);
  665. push_value_on_stack(m_tss.esi);
  666. push_value_on_stack(m_tss.edi);
  667. m_tss.eax = (dword)signal;
  668. m_tss.cs = 0x1b;
  669. m_tss.ds = 0x23;
  670. m_tss.es = 0x23;
  671. m_tss.fs = 0x23;
  672. m_tss.gs = 0x23;
  673. m_tss.eip = handler_laddr.get();
  674. if (m_return_to_ring3_from_signal_trampoline.is_null()) {
  675. // FIXME: This should be a global trampoline shared by all processes, not one created per process!
  676. // FIXME: Remap as read-only after setup.
  677. auto* region = allocate_region(LinearAddress(), PAGE_SIZE, "signal_trampoline", true, true);
  678. m_return_to_ring3_from_signal_trampoline = region->linearAddress;
  679. byte* code_ptr = m_return_to_ring3_from_signal_trampoline.asPtr();
  680. *code_ptr++ = 0x61; // popa
  681. *code_ptr++ = 0x9d; // popf
  682. *code_ptr++ = 0xc3; // ret
  683. *code_ptr++ = 0x0f; // ud2
  684. *code_ptr++ = 0x0b;
  685. m_return_to_ring0_from_signal_trampoline = LinearAddress((dword)code_ptr);
  686. *code_ptr++ = 0x61; // popa
  687. *code_ptr++ = 0xb8; // mov eax, <dword>
  688. *(dword*)code_ptr = Syscall::SC_sigreturn;
  689. code_ptr += sizeof(dword);
  690. *code_ptr++ = 0xcd; // int 0x80
  691. *code_ptr++ = 0x80;
  692. *code_ptr++ = 0x0f; // ud2
  693. *code_ptr++ = 0x0b;
  694. // FIXME: For !SA_NODEFER, maybe we could do something like emitting an int 0x80 syscall here that
  695. // unmasks the signal so it can be received again? I guess then I would need one trampoline
  696. // per signal number if it's hard-coded, but it's just a few bytes per each.
  697. }
  698. if (interrupting_in_kernel)
  699. push_value_on_stack(m_return_to_ring0_from_signal_trampoline.get());
  700. else
  701. push_value_on_stack(m_return_to_ring3_from_signal_trampoline.get());
  702. m_pending_signals &= ~(1 << signal);
  703. // FIXME: This state is such a hack. It avoids trouble if 'current' is the process receiving a signal.
  704. set_state(Skip1SchedulerPass);
  705. #ifdef SIGNAL_DEBUG
  706. dbgprintf("signal: Okay, %s(%u) {%s} has been primed with signal handler %w:%x\n", name().characters(), pid(), toString(state()), m_tss.cs, m_tss.eip);
  707. #endif
  708. }
  709. void Process::sys$sigreturn()
  710. {
  711. InterruptDisabler disabler;
  712. Scheduler::prepare_to_modify_tss(*this);
  713. m_tss = m_tss_to_resume_kernel;
  714. #ifdef SIGNAL_DEBUG
  715. dbgprintf("sys$sigreturn in %s(%u)\n", name().characters(), pid());
  716. dbgprintf(" -> resuming execution at %w:%x\n", m_tss.cs, m_tss.eip);
  717. #endif
  718. set_state(Skip1SchedulerPass);
  719. Scheduler::yield();
  720. kprintf("sys$sigreturn failed in %s(%u)\n", name().characters(), pid());
  721. ASSERT_NOT_REACHED();
  722. }
  723. void Process::push_value_on_stack(dword value)
  724. {
  725. m_tss.esp -= 4;
  726. dword* stack_ptr = (dword*)m_tss.esp;
  727. *stack_ptr = value;
  728. }
  729. void Process::crash()
  730. {
  731. ASSERT_INTERRUPTS_DISABLED();
  732. ASSERT(state() != Dead);
  733. m_termination_signal = SIGSEGV;
  734. set_state(Dead);
  735. dumpRegions();
  736. Scheduler::pick_next_and_switch_now();
  737. ASSERT_NOT_REACHED();
  738. }
  739. void Process::for_each(Function<bool(Process&)> callback)
  740. {
  741. ASSERT_INTERRUPTS_DISABLED();
  742. for (auto* process = g_processes->head(); process;) {
  743. auto* next_process = process->next();
  744. if (!callback(*process))
  745. break;
  746. process = next_process;
  747. }
  748. }
  749. void Process::for_each_in_pgrp(pid_t pgid, Function<bool(Process&)> callback)
  750. {
  751. ASSERT_INTERRUPTS_DISABLED();
  752. for (auto* process = g_processes->head(); process;) {
  753. auto* next_process = process->next();
  754. if (process->pgid() == pgid) {
  755. if (!callback(*process))
  756. break;
  757. }
  758. process = next_process;
  759. }
  760. }
  761. void Process::for_each_in_state(State state, Function<bool(Process&)> callback)
  762. {
  763. ASSERT_INTERRUPTS_DISABLED();
  764. for (auto* process = g_processes->head(); process;) {
  765. auto* next_process = process->next();
  766. if (process->state() == state)
  767. callback(*process);
  768. process = next_process;
  769. }
  770. }
  771. void Process::for_each_not_in_state(State state, Function<bool(Process&)> callback)
  772. {
  773. ASSERT_INTERRUPTS_DISABLED();
  774. for (auto* process = g_processes->head(); process;) {
  775. auto* next_process = process->next();
  776. if (process->state() != state)
  777. callback(*process);
  778. process = next_process;
  779. }
  780. }
  781. Process* Process::from_pid(pid_t pid)
  782. {
  783. ASSERT_INTERRUPTS_DISABLED();
  784. for (auto* process = g_processes->head(); process; process = process->next()) {
  785. if (process->pid() == pid)
  786. return process;
  787. }
  788. return nullptr;
  789. }
  790. FileDescriptor* Process::file_descriptor(int fd)
  791. {
  792. if (fd < 0)
  793. return nullptr;
  794. if ((size_t)fd < m_file_descriptors.size())
  795. return m_file_descriptors[fd].ptr();
  796. return nullptr;
  797. }
  798. const FileDescriptor* Process::file_descriptor(int fd) const
  799. {
  800. if (fd < 0)
  801. return nullptr;
  802. if ((size_t)fd < m_file_descriptors.size())
  803. return m_file_descriptors[fd].ptr();
  804. return nullptr;
  805. }
  806. ssize_t Process::sys$get_dir_entries(int fd, void* buffer, size_t size)
  807. {
  808. VALIDATE_USER_WRITE(buffer, size);
  809. auto* descriptor = file_descriptor(fd);
  810. if (!descriptor)
  811. return -EBADF;
  812. return descriptor->get_dir_entries((byte*)buffer, size);
  813. }
  814. int Process::sys$lseek(int fd, off_t offset, int whence)
  815. {
  816. auto* descriptor = file_descriptor(fd);
  817. if (!descriptor)
  818. return -EBADF;
  819. return descriptor->seek(offset, whence);
  820. }
  821. int Process::sys$ttyname_r(int fd, char* buffer, size_t size)
  822. {
  823. VALIDATE_USER_WRITE(buffer, size);
  824. auto* descriptor = file_descriptor(fd);
  825. if (!descriptor)
  826. return -EBADF;
  827. if (!descriptor->isTTY())
  828. return -ENOTTY;
  829. auto ttyName = descriptor->tty()->ttyName();
  830. if (size < ttyName.length() + 1)
  831. return -ERANGE;
  832. strcpy(buffer, ttyName.characters());
  833. return 0;
  834. }
  835. ssize_t Process::sys$write(int fd, const void* data, size_t size)
  836. {
  837. VALIDATE_USER_READ(data, size);
  838. #ifdef DEBUG_IO
  839. kprintf("Process::sys$write: called(%d, %p, %u)\n", fd, data, size);
  840. #endif
  841. auto* descriptor = file_descriptor(fd);
  842. #ifdef DEBUG_IO
  843. kprintf("Process::sys$write: handle=%p\n", descriptor);
  844. #endif
  845. if (!descriptor)
  846. return -EBADF;
  847. auto nwritten = descriptor->write((const byte*)data, size);
  848. #ifdef DEBUG_IO
  849. kprintf("Process::sys$write: nwritten=%u\n", nwritten);
  850. #endif
  851. return nwritten;
  852. }
  853. ssize_t Process::sys$read(int fd, void* outbuf, size_t nread)
  854. {
  855. VALIDATE_USER_WRITE(outbuf, nread);
  856. #ifdef DEBUG_IO
  857. kprintf("Process::sys$read: called(%d, %p, %u)\n", fd, outbuf, nread);
  858. #endif
  859. auto* descriptor = file_descriptor(fd);
  860. #ifdef DEBUG_IO
  861. kprintf("Process::sys$read: handle=%p\n", descriptor);
  862. #endif
  863. if (!descriptor)
  864. return -EBADF;
  865. if (descriptor->isBlocking()) {
  866. if (!descriptor->hasDataAvailableForRead()) {
  867. m_fdBlockedOnRead = fd;
  868. block(BlockedRead);
  869. sched_yield();
  870. if (m_was_interrupted_while_blocked)
  871. return -EINTR;
  872. }
  873. }
  874. nread = descriptor->read((byte*)outbuf, nread);
  875. #ifdef DEBUG_IO
  876. kprintf("Process::sys$read: nread=%u\n", nread);
  877. #endif
  878. return nread;
  879. }
  880. int Process::sys$close(int fd)
  881. {
  882. auto* descriptor = file_descriptor(fd);
  883. if (!descriptor)
  884. return -EBADF;
  885. int rc = descriptor->close();
  886. m_file_descriptors[fd] = nullptr;
  887. return rc;
  888. }
  889. int Process::sys$lstat(const char* path, Unix::stat* statbuf)
  890. {
  891. VALIDATE_USER_WRITE(statbuf, sizeof(Unix::stat));
  892. int error;
  893. auto descriptor = VirtualFileSystem::the().open(move(path), error, O_NOFOLLOW_NOERROR, cwdInode());
  894. if (!descriptor)
  895. return error;
  896. descriptor->stat(statbuf);
  897. return 0;
  898. }
  899. int Process::sys$stat(const char* path, Unix::stat* statbuf)
  900. {
  901. VALIDATE_USER_WRITE(statbuf, sizeof(Unix::stat));
  902. int error;
  903. auto descriptor = VirtualFileSystem::the().open(move(path), error, 0, cwdInode());
  904. if (!descriptor)
  905. return error;
  906. descriptor->stat(statbuf);
  907. return 0;
  908. }
  909. int Process::sys$readlink(const char* path, char* buffer, size_t size)
  910. {
  911. VALIDATE_USER_READ(path, strlen(path));
  912. VALIDATE_USER_WRITE(buffer, size);
  913. int error;
  914. auto descriptor = VirtualFileSystem::the().open(path, error, O_RDONLY | O_NOFOLLOW_NOERROR, cwdInode());
  915. if (!descriptor)
  916. return error;
  917. if (!descriptor->metadata().isSymbolicLink())
  918. return -EINVAL;
  919. auto contents = descriptor->readEntireFile();
  920. if (!contents)
  921. return -EIO; // FIXME: Get a more detailed error from VFS.
  922. memcpy(buffer, contents.pointer(), min(size, contents.size()));
  923. if (contents.size() + 1 < size)
  924. buffer[contents.size()] = '\0';
  925. return 0;
  926. }
  927. int Process::sys$chdir(const char* path)
  928. {
  929. VALIDATE_USER_READ(path, strlen(path));
  930. int error;
  931. auto descriptor = VirtualFileSystem::the().open(path, error, 0, cwdInode());
  932. if (!descriptor)
  933. return error;
  934. if (!descriptor->isDirectory())
  935. return -ENOTDIR;
  936. m_cwd = descriptor->vnode();
  937. return 0;
  938. }
  939. int Process::sys$getcwd(char* buffer, size_t size)
  940. {
  941. VALIDATE_USER_WRITE(buffer, size);
  942. auto path = VirtualFileSystem::the().absolutePath(cwdInode());
  943. if (path.isNull())
  944. return -EINVAL;
  945. if (size < path.length() + 1)
  946. return -ERANGE;
  947. strcpy(buffer, path.characters());
  948. return -ENOTIMPL;
  949. }
  950. size_t Process::number_of_open_file_descriptors() const
  951. {
  952. size_t count = 0;
  953. for (auto& descriptor : m_file_descriptors) {
  954. if (descriptor)
  955. ++count;
  956. }
  957. return count;
  958. }
  959. int Process::sys$open(const char* path, int options)
  960. {
  961. #ifdef DEBUG_IO
  962. kprintf("Process::sys$open(): PID=%u, path=%s {%u}\n", m_pid, path, pathLength);
  963. #endif
  964. VALIDATE_USER_READ(path, strlen(path));
  965. if (number_of_open_file_descriptors() >= m_max_open_file_descriptors)
  966. return -EMFILE;
  967. int error;
  968. auto descriptor = VirtualFileSystem::the().open(path, error, options, cwdInode());
  969. if (!descriptor)
  970. return error;
  971. if (options & O_DIRECTORY && !descriptor->isDirectory())
  972. return -ENOTDIR; // FIXME: This should be handled by VFS::open.
  973. int fd = 0;
  974. for (; fd < m_max_open_file_descriptors; ++fd) {
  975. if (!m_file_descriptors[fd])
  976. break;
  977. }
  978. m_file_descriptors[fd] = move(descriptor);
  979. return fd;
  980. }
  981. int Process::sys$uname(utsname* buf)
  982. {
  983. VALIDATE_USER_WRITE(buf, sizeof(utsname));
  984. strcpy(buf->sysname, "Serenity");
  985. strcpy(buf->release, "1.0-dev");
  986. strcpy(buf->version, "FIXME");
  987. strcpy(buf->machine, "i386");
  988. strcpy(buf->nodename, getHostname().characters());
  989. return 0;
  990. }
  991. int Process::sys$isatty(int fd)
  992. {
  993. auto* descriptor = file_descriptor(fd);
  994. if (!descriptor)
  995. return -EBADF;
  996. if (!descriptor->isTTY())
  997. return -ENOTTY;
  998. return 1;
  999. }
  1000. int Process::sys$kill(pid_t pid, int signal)
  1001. {
  1002. if (pid == 0) {
  1003. // FIXME: Send to same-group processes.
  1004. ASSERT(pid != 0);
  1005. }
  1006. if (pid == -1) {
  1007. // FIXME: Send to all processes.
  1008. ASSERT(pid != -1);
  1009. }
  1010. ASSERT(pid != current->pid()); // FIXME: Support this scenario.
  1011. InterruptDisabler disabler;
  1012. auto* peer = Process::from_pid(pid);
  1013. if (!peer)
  1014. return -ESRCH;
  1015. peer->send_signal(signal, this);
  1016. return 0;
  1017. }
  1018. int Process::sys$sleep(unsigned seconds)
  1019. {
  1020. if (!seconds)
  1021. return 0;
  1022. sleep(seconds * TICKS_PER_SECOND);
  1023. if (m_wakeupTime > system.uptime) {
  1024. ASSERT(m_was_interrupted_while_blocked);
  1025. dword ticks_left_until_original_wakeup_time = m_wakeupTime - system.uptime;
  1026. return ticks_left_until_original_wakeup_time / TICKS_PER_SECOND;
  1027. }
  1028. return 0;
  1029. }
  1030. int Process::sys$gettimeofday(timeval* tv)
  1031. {
  1032. VALIDATE_USER_WRITE(tv, sizeof(tv));
  1033. InterruptDisabler disabler;
  1034. auto now = RTC::now();
  1035. tv->tv_sec = now;
  1036. tv->tv_usec = 0;
  1037. return 0;
  1038. }
  1039. uid_t Process::sys$getuid()
  1040. {
  1041. return m_uid;
  1042. }
  1043. gid_t Process::sys$getgid()
  1044. {
  1045. return m_gid;
  1046. }
  1047. uid_t Process::sys$geteuid()
  1048. {
  1049. return m_euid;
  1050. }
  1051. gid_t Process::sys$getegid()
  1052. {
  1053. return m_egid;
  1054. }
  1055. pid_t Process::sys$getpid()
  1056. {
  1057. return m_pid;
  1058. }
  1059. pid_t Process::sys$getppid()
  1060. {
  1061. return m_ppid;
  1062. }
  1063. mode_t Process::sys$umask(mode_t mask)
  1064. {
  1065. auto old_mask = m_umask;
  1066. m_umask = mask;
  1067. return old_mask;
  1068. }
  1069. void Process::reap(pid_t pid)
  1070. {
  1071. InterruptDisabler disabler;
  1072. auto* process = Process::from_pid(pid);
  1073. ASSERT(process);
  1074. dbgprintf("reap: %s(%u) {%s}\n", process->name().characters(), process->pid(), toString(process->state()));
  1075. ASSERT(process->state() == Dead);
  1076. g_processes->remove(process);
  1077. delete process;
  1078. }
  1079. pid_t Process::sys$waitpid(pid_t waitee, int* wstatus, int options)
  1080. {
  1081. if (wstatus)
  1082. VALIDATE_USER_WRITE(wstatus, sizeof(int));
  1083. {
  1084. InterruptDisabler disabler;
  1085. if (!Process::from_pid(waitee))
  1086. return -ECHILD;
  1087. }
  1088. m_waitee = waitee;
  1089. m_waitee_status = 0;
  1090. block(BlockedWait);
  1091. sched_yield();
  1092. if (m_was_interrupted_while_blocked)
  1093. return -EINTR;
  1094. reap(waitee);
  1095. if (wstatus)
  1096. *wstatus = m_waitee_status;
  1097. return m_waitee;
  1098. }
  1099. void Process::unblock()
  1100. {
  1101. ASSERT(m_state != Process::Runnable && m_state != Process::Running);
  1102. system.nblocked--;
  1103. m_state = Process::Runnable;
  1104. }
  1105. void Process::block(Process::State state)
  1106. {
  1107. ASSERT(current->state() == Process::Running);
  1108. system.nblocked++;
  1109. m_was_interrupted_while_blocked = false;
  1110. set_state(state);
  1111. }
  1112. void block(Process::State state)
  1113. {
  1114. current->block(state);
  1115. sched_yield();
  1116. }
  1117. void sleep(DWORD ticks)
  1118. {
  1119. ASSERT(current->state() == Process::Running);
  1120. current->setWakeupTime(system.uptime + ticks);
  1121. current->block(Process::BlockedSleep);
  1122. sched_yield();
  1123. }
  1124. bool Process::isValidAddressForKernel(LinearAddress laddr) const
  1125. {
  1126. // We check extra carefully here since the first 4MB of the address space is identity-mapped.
  1127. // This code allows access outside of the known used address ranges to get caught.
  1128. InterruptDisabler disabler;
  1129. if (laddr.get() >= ksyms().first().address && laddr.get() <= ksyms().last().address)
  1130. return true;
  1131. if (is_kmalloc_address((void*)laddr.get()))
  1132. return true;
  1133. return validate_user_read(laddr);
  1134. }
  1135. bool Process::validate_user_read(LinearAddress laddr) const
  1136. {
  1137. InterruptDisabler disabler;
  1138. return MM.validate_user_read(*this, laddr);
  1139. }
  1140. bool Process::validate_user_write(LinearAddress laddr) const
  1141. {
  1142. InterruptDisabler disabler;
  1143. return MM.validate_user_write(*this, laddr);
  1144. }
  1145. pid_t Process::sys$getsid(pid_t pid)
  1146. {
  1147. if (pid == 0)
  1148. return m_sid;
  1149. InterruptDisabler disabler;
  1150. auto* process = Process::from_pid(pid);
  1151. if (!process)
  1152. return -ESRCH;
  1153. if (m_sid != process->m_sid)
  1154. return -EPERM;
  1155. return process->m_sid;
  1156. }
  1157. pid_t Process::sys$setsid()
  1158. {
  1159. InterruptDisabler disabler;
  1160. bool found_process_with_same_pgid_as_my_pid = false;
  1161. Process::for_each_in_pgrp(pid(), [&] (auto& process) {
  1162. found_process_with_same_pgid_as_my_pid = true;
  1163. return false;
  1164. });
  1165. if (found_process_with_same_pgid_as_my_pid)
  1166. return -EPERM;
  1167. m_sid = m_pid;
  1168. m_pgid = m_pid;
  1169. return m_sid;
  1170. }
  1171. pid_t Process::sys$getpgid(pid_t pid)
  1172. {
  1173. if (pid == 0)
  1174. return m_pgid;
  1175. InterruptDisabler disabler; // FIXME: Use a ProcessHandle
  1176. auto* process = Process::from_pid(pid);
  1177. if (!process)
  1178. return -ESRCH;
  1179. return process->m_pgid;
  1180. }
  1181. pid_t Process::sys$getpgrp()
  1182. {
  1183. return m_pgid;
  1184. }
  1185. static pid_t get_sid_from_pgid(pid_t pgid)
  1186. {
  1187. InterruptDisabler disabler;
  1188. auto* group_leader = Process::from_pid(pgid);
  1189. if (!group_leader)
  1190. return -1;
  1191. return group_leader->sid();
  1192. }
  1193. int Process::sys$setpgid(pid_t specified_pid, pid_t specified_pgid)
  1194. {
  1195. InterruptDisabler disabler; // FIXME: Use a ProcessHandle
  1196. pid_t pid = specified_pid ? specified_pid : m_pid;
  1197. if (specified_pgid < 0)
  1198. return -EINVAL;
  1199. auto* process = Process::from_pid(pid);
  1200. if (!process)
  1201. return -ESRCH;
  1202. pid_t new_pgid = specified_pgid ? specified_pgid : process->m_pid;
  1203. pid_t current_sid = get_sid_from_pgid(process->m_pgid);
  1204. pid_t new_sid = get_sid_from_pgid(new_pgid);
  1205. if (current_sid != new_sid) {
  1206. // Can't move a process between sessions.
  1207. return -EPERM;
  1208. }
  1209. // FIXME: There are more EPERM conditions to check for here..
  1210. process->m_pgid = new_pgid;
  1211. return 0;
  1212. }
  1213. pid_t Process::sys$tcgetpgrp(int fd)
  1214. {
  1215. auto* descriptor = file_descriptor(fd);
  1216. if (!descriptor)
  1217. return -EBADF;
  1218. if (!descriptor->isTTY())
  1219. return -ENOTTY;
  1220. auto& tty = *descriptor->tty();
  1221. if (&tty != m_tty)
  1222. return -ENOTTY;
  1223. return tty.pgid();
  1224. }
  1225. int Process::sys$tcsetpgrp(int fd, pid_t pgid)
  1226. {
  1227. if (pgid < 0)
  1228. return -EINVAL;
  1229. if (get_sid_from_pgid(pgid) != m_sid)
  1230. return -EINVAL;
  1231. auto* descriptor = file_descriptor(fd);
  1232. if (!descriptor)
  1233. return -EBADF;
  1234. if (!descriptor->isTTY())
  1235. return -ENOTTY;
  1236. auto& tty = *descriptor->tty();
  1237. if (&tty != m_tty)
  1238. return -ENOTTY;
  1239. tty.set_pgid(pgid);
  1240. return 0;
  1241. }
  1242. int Process::sys$getdtablesize()
  1243. {
  1244. return m_max_open_file_descriptors;
  1245. }
  1246. int Process::sys$dup(int old_fd)
  1247. {
  1248. auto* descriptor = file_descriptor(old_fd);
  1249. if (!descriptor)
  1250. return -EBADF;
  1251. if (number_of_open_file_descriptors() == m_max_open_file_descriptors)
  1252. return -EMFILE;
  1253. int new_fd = 0;
  1254. for (; new_fd < m_max_open_file_descriptors; ++new_fd) {
  1255. if (!m_file_descriptors[new_fd])
  1256. break;
  1257. }
  1258. m_file_descriptors[new_fd] = descriptor;
  1259. return new_fd;
  1260. }
  1261. int Process::sys$dup2(int old_fd, int new_fd)
  1262. {
  1263. auto* descriptor = file_descriptor(old_fd);
  1264. if (!descriptor)
  1265. return -EBADF;
  1266. if (number_of_open_file_descriptors() == m_max_open_file_descriptors)
  1267. return -EMFILE;
  1268. m_file_descriptors[new_fd] = descriptor;
  1269. return new_fd;
  1270. }
  1271. Unix::sighandler_t Process::sys$signal(int signum, Unix::sighandler_t handler)
  1272. {
  1273. // FIXME: Fail with -EINVAL if attepmting to catch or ignore SIGKILL or SIGSTOP.
  1274. if (signum >= 32)
  1275. return (Unix::sighandler_t)-EINVAL;
  1276. dbgprintf("sys$signal: %d => L%x\n", signum, handler);
  1277. return nullptr;
  1278. }
  1279. int Process::sys$sigaction(int signum, const Unix::sigaction* act, Unix::sigaction* old_act)
  1280. {
  1281. // FIXME: Fail with -EINVAL if attepmting to change action for SIGKILL or SIGSTOP.
  1282. if (signum >= 32)
  1283. return -EINVAL;
  1284. VALIDATE_USER_READ(act, sizeof(Unix::sigaction));
  1285. InterruptDisabler disabler; // FIXME: This should use a narrower lock.
  1286. auto& action = m_signal_action_data[signum];
  1287. if (old_act) {
  1288. VALIDATE_USER_WRITE(old_act, sizeof(Unix::sigaction));
  1289. old_act->sa_flags = action.flags;
  1290. old_act->sa_restorer = (decltype(old_act->sa_restorer))action.restorer.get();
  1291. old_act->sa_sigaction = (decltype(old_act->sa_sigaction))action.handler_or_sigaction.get();
  1292. }
  1293. action.restorer = LinearAddress((dword)act->sa_restorer);
  1294. action.flags = act->sa_flags;
  1295. action.handler_or_sigaction = LinearAddress((dword)act->sa_sigaction);
  1296. return 0;
  1297. }
  1298. int Process::sys$getgroups(int count, gid_t* gids)
  1299. {
  1300. if (count < 0)
  1301. return -EINVAL;
  1302. ASSERT(m_gids.size() < MAX_PROCESS_GIDS);
  1303. if (!count)
  1304. return m_gids.size();
  1305. if (count != m_gids.size())
  1306. return -EINVAL;
  1307. VALIDATE_USER_WRITE(gids, sizeof(gid_t) * count);
  1308. size_t i = 0;
  1309. for (auto gid : m_gids)
  1310. gids[i++] = gid;
  1311. return 0;
  1312. }
  1313. int Process::sys$setgroups(size_t count, const gid_t* gids)
  1314. {
  1315. if (!is_root())
  1316. return -EPERM;
  1317. if (count >= MAX_PROCESS_GIDS)
  1318. return -EINVAL;
  1319. VALIDATE_USER_READ(gids, sizeof(gid_t) * count);
  1320. m_gids.clear();
  1321. m_gids.set(m_gid);
  1322. for (size_t i = 0; i < count; ++i)
  1323. m_gids.set(gids[i]);
  1324. return 0;
  1325. }