db_process.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_process.c
  17. *
  18. * This source file contains functions to create a child process and terminate
  19. * the running process. Child does not inherit any objects or memory from its
  20. * parent pricess. A Parent process may not modify the execution of its
  21. * children. It can wait for a child to exit using its handle. Also, parent and
  22. * child may communicate through I/O streams provided by the parent to the child
  23. * at creation.
  24. */
  25. #include "pal_defs.h"
  26. #include "pal_linux_defs.h"
  27. #include "pal.h"
  28. #include "pal_internal.h"
  29. #include "pal_linux.h"
  30. #include "pal_debug.h"
  31. #include "pal_error.h"
  32. #include "pal_security.h"
  33. #include "graphene.h"
  34. #include "graphene-ipc.h"
  35. #include "api.h"
  36. #include <linux/sched.h>
  37. #include <linux/types.h>
  38. typedef __kernel_pid_t pid_t;
  39. #include <asm/fcntl.h>
  40. #include <sys/socket.h>
  41. #include <asm-errno.h>
  42. #ifndef SEEK_SET
  43. # define SEEK_SET 0
  44. #endif
  45. int _DkProcessCreate (PAL_HANDLE * handle, const char * uri,
  46. int flags, const char ** args)
  47. {
  48. int ret, rete = 0;
  49. const char * manifest_uri = pal_config.manifest;
  50. PAL_HANDLE manifest = pal_config.manifest_handle;
  51. const char * exec_uri = NULL;
  52. PAL_HANDLE exec = NULL;
  53. bool noexec = false;
  54. if (uri) {
  55. exec_uri = uri;
  56. if ((ret = _DkStreamOpen(&exec, uri, PAL_ACCESS_RDONLY, 0, 0, 0)) < 0)
  57. return ret;
  58. if (check_elf_object(exec) < 0) {
  59. manifest = exec;
  60. manifest_uri = uri;
  61. exec = NULL;
  62. exec_uri = NULL;
  63. }
  64. } else {
  65. noexec = true;
  66. }
  67. int fds[6] = { -1, -1, -1, -1, -1, -1 };
  68. if (IS_ERR((ret = INLINE_SYSCALL(pipe2, 2, &fds[0], 0))) ||
  69. IS_ERR((ret = INLINE_SYSCALL(pipe2, 2, &fds[2], 0))) ||
  70. IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, SOCK_STREAM,
  71. 0, &fds[4])))) {
  72. ret = -PAL_ERROR_DENIED;
  73. goto out;
  74. }
  75. int proc_fds[2][3] = {
  76. { fds[0], fds[3], fds[4] },
  77. { fds[2], fds[1], fds[5] },
  78. };
  79. int nargs = 0;
  80. if (args) {
  81. const char ** p = args;
  82. while (*p) {
  83. p++;
  84. nargs++;
  85. }
  86. }
  87. # define STRARG_SIZE 256
  88. const char ** new_args = __alloca(sizeof(const char *) * (nargs + 3));
  89. int bufsize = STRARG_SIZE;
  90. char * argbuf = __alloca(STRARG_SIZE);
  91. new_args[0] = pal_config.lib_name;
  92. new_args[1] = argbuf;
  93. if (args)
  94. memcpy(new_args + 2, args, sizeof(const char *) * nargs);
  95. new_args[nargs + 2] = NULL;
  96. #define write_arg(...) \
  97. do { \
  98. int _len = snprintf(argbuf, bufsize, __VA_ARGS__); \
  99. argbuf += _len; \
  100. bufsize -= _len; \
  101. } while (0);
  102. write_arg(":domain=%08x;", pal_sec_info.domain_id);
  103. int manifest_fd = -1;
  104. if (manifest) {
  105. manifest_fd = manifest->file.fd;
  106. INLINE_SYSCALL(fcntl, 3, manifest_fd, F_SETFD, 0);
  107. write_arg("manifest=%d,%s;", manifest_fd, manifest_uri ? : "");
  108. if (manifest != pal_config.manifest_handle)
  109. manifest_fd = -1;
  110. }
  111. write_arg("proc=%d,%d,%d,%d;",
  112. proc_fds[0][0], proc_fds[0][1], proc_fds[0][2],
  113. pal_linux_config.pid);
  114. if (exec) {
  115. int exec_fd = exec->file.fd;
  116. INLINE_SYSCALL(fcntl, 3, exec_fd, F_SETFD, 0);
  117. write_arg("exec=%d,%s;", exec_fd, exec_uri ? : "");
  118. } else if (noexec) {
  119. write_arg("noexec;");
  120. }
  121. if (pal_sec_info.pipe_prefix)
  122. write_arg("pipe=%s;", pal_sec_info.pipe_prefix);
  123. if (pal_config.heap_base)
  124. write_arg("heap=%lx;", pal_config.heap_base);
  125. if (pal_sec_info.rand_gen)
  126. write_arg("rand=%d;", pal_sec_info.rand_gen);
  127. if (pal_sec_info.mcast_port)
  128. write_arg("mcast=%u;", pal_sec_info.mcast_port);
  129. ret = ARCH_VFORK();
  130. if (IS_ERR(ret)) {
  131. ret = -PAL_ERROR_DENIED;
  132. goto out;
  133. }
  134. if (!ret) {
  135. for (int i = 0 ; i < 3 ; i++)
  136. INLINE_SYSCALL(close, 1, proc_fds[1][i]);
  137. if (manifest_fd >= 0)
  138. INLINE_SYSCALL(fcntl, 3, manifest_fd, F_SETFD, 0);
  139. rete = INLINE_SYSCALL(execve, 3, pal_config.lib_name, new_args,
  140. pal_config.environments);
  141. /* shouldn't get to here */
  142. printf("unexpected failure of new process\n");
  143. asm("hlt");
  144. return 0;
  145. }
  146. if (IS_ERR(rete)) {
  147. ret = -PAL_ERROR_DENIED;
  148. goto out;
  149. }
  150. for (int i = 0 ; i < 3 ; i++)
  151. INLINE_SYSCALL(close, 1, proc_fds[0][i]);
  152. for (int i = 0 ; i < 3 ; i++)
  153. INLINE_SYSCALL(fcntl, 3, proc_fds[1][i], F_SETFD, FD_CLOEXEC);
  154. int pid = ret;
  155. PAL_HANDLE hdl = malloc(HANDLE_SIZE(process));
  156. SET_HANDLE_TYPE(hdl, process);
  157. hdl->__in.flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITEABLE(1)|WRITEABLE(2);
  158. hdl->process.stream_in = proc_fds[1][0];
  159. hdl->process.stream_out = proc_fds[1][1];
  160. hdl->process.cargo = proc_fds[1][2];
  161. hdl->process.pid = pid;
  162. hdl->process.nonblocking = PAL_FALSE;
  163. *handle = hdl;
  164. ret = 0;
  165. out:
  166. if (ret < 0) {
  167. for (int i = 0 ; i < 6 ; i++)
  168. if (fds[i] >= 0)
  169. INLINE_SYSCALL(close, 1, fds[i]);
  170. }
  171. return ret;
  172. }
  173. static void read_child_args (const char * val, int vlen,
  174. void * arg1, bool isnum1,
  175. void * arg2, bool isnum2,
  176. void * arg3, bool isnum3,
  177. void * arg4, bool isnum4)
  178. {
  179. const char * v1 = val, * v2 = v1, * end = val + vlen;
  180. void * arg[4] = { arg1, arg2, arg3, arg4 };
  181. bool isnum[4] = { isnum1, isnum2, isnum3, isnum4 };
  182. for (int i = 0 ; i < 4 ; i++) {
  183. if (!arg[i])
  184. return;
  185. while (v2 < end && *v2 != ',')
  186. v2++;
  187. if (v1 >= end || v2 <= v1) {
  188. if (isnum[i])
  189. *(int *) arg[i] = 0;
  190. else
  191. ((char *) arg[i])[0] = 0;
  192. continue;
  193. }
  194. if (isnum[i]) {
  195. *(int *) arg[i] = atoi(v1);
  196. } else {
  197. memcpy((char *) arg[i], v1, v2 - v1);
  198. ((char *) arg[i])[v2 - v1] = 0;
  199. }
  200. v2 = v1 = v2 + 1;
  201. }
  202. }
  203. static inline bool set_fd_cloexec (int fd)
  204. {
  205. return !(IS_ERR(INLINE_SYSCALL(fcntl, 3, fd, F_SETFD, FD_CLOEXEC)));
  206. }
  207. #define STRARG_SIZE 256
  208. static void read_child_handle (const char * val, int vlen,
  209. PAL_HANDLE * handle, const char ** uri)
  210. {
  211. int fd;
  212. char buf[STRARG_SIZE];
  213. read_child_args(val, vlen, &fd, true, buf, false,
  214. NULL, false, NULL, false);
  215. if (!fd || !set_fd_cloexec(fd)) {
  216. *handle = NULL;
  217. *uri = NULL;
  218. return;
  219. }
  220. INLINE_SYSCALL(lseek, 3, fd, 0, SEEK_SET);
  221. int len = strlen(buf);
  222. PAL_HANDLE hdl = malloc(HANDLE_SIZE(file) + (len > 4 ? len - 4 : 0));
  223. SET_HANDLE_TYPE(hdl, file);
  224. hdl->__in.flags |= RFD(0)|WFD(0)|WRITEABLE(0);
  225. hdl->file.fd = fd;
  226. if (len > 4) {
  227. char * path = (void *) hdl + HANDLE_SIZE(file);
  228. memcpy(path, buf + 5, len - 4);
  229. hdl->file.realpath = path;
  230. } else {
  231. hdl->file.realpath = NULL;
  232. }
  233. *handle = hdl;
  234. *uri = len ? remalloc(buf, len + 1) : NULL;
  235. }
  236. int init_child_process (const char * proc_args)
  237. {
  238. const char * c = proc_args;
  239. while (*c) {
  240. const char * key = c, * val;
  241. int klen, vlen;
  242. while (*c && *c != '=' && *c != ';')
  243. c++;
  244. klen = c - key;
  245. if (klen == 6 && !memcmp(key, "noexec", 6))
  246. /* format: noexec */
  247. pal_linux_config.noexec = true;
  248. if (!*c)
  249. break;
  250. if (*c == ';') {
  251. c++;
  252. continue;
  253. }
  254. val = (++c);
  255. while (*c && *c != ';')
  256. c++;
  257. vlen = c - val;
  258. if (*c == ';')
  259. c++;
  260. if (klen == 4) {
  261. if (!memcmp(key, "exec", 4)) {
  262. /* format: exec=fd,uri */
  263. read_child_handle(val, vlen, &pal_config.exec_handle,
  264. &pal_config.exec);
  265. } else if (!memcmp(key, "proc", 4)) {
  266. /* format: proc=fd,pid */
  267. int fds[3], pid;
  268. read_child_args(val, vlen,
  269. &fds[0], true, &fds[1], true, &fds[2], true,
  270. &pid, true);
  271. for (int i = 0 ; i < 3 ; i++)
  272. if (!set_fd_cloexec(fds[i]))
  273. fds[i] = PAL_IDX_POISON;
  274. PAL_HANDLE proc = malloc(HANDLE_SIZE(process));
  275. SET_HANDLE_TYPE(proc, process);
  276. proc->__in.flags |= RFD(0)|WFD(1)|RFD(2)|WFD(2)|WRITEABLE(1)|WRITEABLE(2);
  277. proc->process.stream_in = fds[0];
  278. proc->process.stream_out = fds[1];
  279. proc->process.cargo = fds[2];
  280. proc->process.pid = pid;
  281. __pal_control.parent_process = proc;
  282. } else if (!memcmp(key, "pipe", 4)) {
  283. /* format: pipe=prefix */
  284. char * prefix = remalloc(val, vlen + 1);
  285. prefix[vlen] = 0;
  286. pal_sec_info.pipe_prefix = prefix;
  287. } else if (!memcmp(key, "rand", 4)) {
  288. /* format: rand=fd */
  289. pal_sec_info.rand_gen = atoi(val);
  290. } else if (!memcmp(key, "heap", 4)) {
  291. /* format: heap=addr (hex) */
  292. pal_config.heap_base = (void *) strtol(val, NULL, 16);
  293. }
  294. } else if (klen == 5) {
  295. if (!memcmp(key, "mcast", 5)) {
  296. /* format: mcast=port */
  297. pal_sec_info.mcast_port = atoi(val);
  298. }
  299. } else if (klen == 6) {
  300. if (!memcmp(key, "domain", 6)) {
  301. /* format: domain=id */
  302. pal_sec_info.domain_id = strtol(val, NULL, 16);
  303. }
  304. } else if (klen == 8) {
  305. if (!memcmp(key, "manifest", 8)) {
  306. /* format: manifest=fd,uri */
  307. read_child_handle(val, vlen, &pal_config.manifest_handle,
  308. &pal_config.manifest);
  309. }
  310. }
  311. }
  312. return 0;
  313. }
  314. void _DkProcessExit (int exitcode)
  315. {
  316. if (__pal_control.parent_process)
  317. _DkObjectClose(__pal_control.parent_process);
  318. if (__pal_control.manifest_handle)
  319. _DkObjectClose(__pal_control.manifest_handle);
  320. INLINE_SYSCALL(exit_group, 1, exitcode);
  321. }
  322. int ioctl_set_graphene (struct config_store * config, int ndefault,
  323. const struct graphene_user_policy * default_policies);
  324. static int set_graphene_task (const char * uri, int flags)
  325. {
  326. PAL_HANDLE handle = NULL;
  327. int ret;
  328. if ((ret = _DkStreamOpen(&handle, uri, PAL_ACCESS_RDONLY, 0, 0, 0)) < 0)
  329. return ret;
  330. PAL_STREAM_ATTR attr;
  331. if ((ret = _DkStreamAttributesQuerybyHandle(handle, &attr)) < 0)
  332. goto out;
  333. void * addr = NULL;
  334. size_t size = attr.size;
  335. if ((ret = _DkStreamMap(handle, &addr, PAL_PROT_READ, 0,
  336. ALLOC_ALIGNUP(size))) < 0)
  337. goto out;
  338. struct config_store cfg;
  339. cfg.raw_data = addr;
  340. cfg.raw_size = size;
  341. cfg.malloc = malloc;
  342. cfg.free = free;
  343. if ((ret = read_config(&cfg, NULL, NULL)) < 0)
  344. goto out_mem;
  345. const char * manifest = uri;
  346. struct graphene_user_policy manifest_policy;
  347. if (!memcmp(manifest, "file:", 5)) {
  348. manifest_policy.type = GRAPHENE_FS_PATH | GRAPHENE_FS_READ;
  349. manifest_policy.value = manifest + 5;
  350. } else {
  351. manifest_policy.type = 0;
  352. }
  353. if (flags & PAL_SANDBOX_PIPE) {
  354. do {
  355. getrand(&pal_sec_info.mcast_port, sizeof(unsigned short));
  356. } while (pal_sec_info.mcast_port < 1024);
  357. }
  358. struct graphene_net_policy mcast_rules[2];
  359. memset(mcast_rules, 0, sizeof(struct graphene_net_policy) * 2);
  360. mcast_rules[0].family = AF_INET;
  361. mcast_rules[0].local.port_begin = pal_sec_info.mcast_port;
  362. mcast_rules[0].local.port_end = pal_sec_info.mcast_port;
  363. mcast_rules[0].peer.port_begin = 0;
  364. mcast_rules[0].peer.port_end = 65535;
  365. mcast_rules[1].family = AF_INET;
  366. mcast_rules[1].local.port_begin = 0;
  367. mcast_rules[1].local.port_end = 65535;
  368. inet_pton(AF_INET, MCAST_GROUP, &mcast_rules[1].peer.addr);
  369. mcast_rules[1].peer.port_begin = pal_sec_info.mcast_port;
  370. mcast_rules[1].peer.port_end = pal_sec_info.mcast_port;
  371. if (flags & PAL_SANDBOX_PIPE) {
  372. char pipe_root[GRAPHENE_PIPEDIR_LEN + 20];
  373. char pipe_prefix[9];
  374. int sandboxid;
  375. snprintf(pipe_root,
  376. GRAPHENE_PIPEDIR_LEN + 20, GRAPHENE_PIPEDIR "/%08x",
  377. pal_sec_info.domain_id);
  378. getrand(&sandboxid, sizeof(int));
  379. snprintf(pipe_prefix, 9, "%08x", sandboxid);
  380. struct graphene_user_policy default_policies[] = {
  381. { .type = GRAPHENE_UNIX_ROOT, .value = pipe_root, },
  382. { .type = GRAPHENE_UNIX_PREFIX, .value = pipe_prefix, },
  383. { .type = GRAPHENE_NET_RULE, .value = &mcast_rules[0], },
  384. { .type = GRAPHENE_NET_RULE, .value = &mcast_rules[1], },
  385. manifest_policy,
  386. };
  387. ret = ioctl_set_graphene(&cfg, manifest_policy.type ? 5 : 4,
  388. default_policies);
  389. if (ret < 0)
  390. goto out_mem;
  391. pal_sec_info.pipe_prefix = remalloc(pipe_prefix, 9);
  392. } else {
  393. const struct graphene_user_policy default_policies[] = {
  394. { .type = GRAPHENE_NET_RULE, .value = &mcast_rules[0], },
  395. { .type = GRAPHENE_NET_RULE, .value = &mcast_rules[1], },
  396. manifest_policy,
  397. };
  398. ret = ioctl_set_graphene(&cfg, manifest_policy.type ? 3 : 2,
  399. default_policies);
  400. if (ret < 0)
  401. goto out_mem;
  402. }
  403. pal_config.manifest = manifest;
  404. _DkObjectClose(pal_config.manifest_handle);
  405. pal_config.manifest_handle = handle;
  406. free_config(&cfg);
  407. out_mem:
  408. _DkStreamUnmap(cfg.raw_data, ALLOC_ALIGNUP(cfg.raw_size));
  409. out:
  410. DkObjectClose(handle);
  411. return ret;
  412. }
  413. int _DkProcessSandboxCreate (const char * manifest, int flags)
  414. {
  415. return set_graphene_task(manifest, flags);
  416. }
  417. static int proc_read (PAL_HANDLE handle, int offset, int count,
  418. void * buffer)
  419. {
  420. int bytes = INLINE_SYSCALL(read, 3, handle->process.stream_in, buffer,
  421. count);
  422. if (IS_ERR(bytes))
  423. switch(ERRNO(bytes)) {
  424. case EWOULDBLOCK:
  425. return-PAL_ERROR_TRYAGAIN;
  426. case EINTR:
  427. return -PAL_ERROR_INTERRUPTED;
  428. default:
  429. return -PAL_ERROR_DENIED;
  430. }
  431. return bytes;
  432. }
  433. static int proc_write (PAL_HANDLE handle, int offset, int count,
  434. const void * buffer)
  435. {
  436. int bytes = INLINE_SYSCALL(write, 3, handle->process.stream_out, buffer,
  437. count);
  438. if (IS_ERR(bytes))
  439. switch(ERRNO(bytes)) {
  440. case EWOULDBLOCK:
  441. handle->__in.flags &= ~WRITEABLE(1);
  442. return-PAL_ERROR_TRYAGAIN;
  443. case EINTR:
  444. return -PAL_ERROR_INTERRUPTED;
  445. default:
  446. return -PAL_ERROR_DENIED;
  447. }
  448. if (bytes == count)
  449. handle->__in.flags |= WRITEABLE(1);
  450. else
  451. handle->__in.flags &= ~WRITEABLE(1);
  452. return bytes;
  453. }
  454. static int proc_close (PAL_HANDLE handle)
  455. {
  456. if (handle->process.stream_in != PAL_IDX_POISON) {
  457. INLINE_SYSCALL(close, 1, handle->process.stream_in);
  458. handle->process.stream_in = PAL_IDX_POISON;
  459. }
  460. if (handle->process.stream_out != PAL_IDX_POISON) {
  461. INLINE_SYSCALL(close, 1, handle->process.stream_out);
  462. handle->process.stream_out = PAL_IDX_POISON;
  463. }
  464. if (handle->process.cargo != PAL_IDX_POISON) {
  465. INLINE_SYSCALL(close, 1, handle->process.cargo);
  466. handle->process.cargo = PAL_IDX_POISON;
  467. }
  468. return 0;
  469. }
  470. static int proc_delete (PAL_HANDLE handle, int access)
  471. {
  472. int shutdown;
  473. switch (access) {
  474. case 0:
  475. shutdown = SHUT_RDWR;
  476. break;
  477. case PAL_DELETE_RD:
  478. shutdown = SHUT_RD;
  479. break;
  480. case PAL_DELETE_WR:
  481. shutdown = SHUT_WR;
  482. break;
  483. default:
  484. return -PAL_ERROR_INVAL;
  485. }
  486. if (access != PAL_DELETE_WR &&
  487. handle->process.stream_in != PAL_IDX_POISON) {
  488. INLINE_SYSCALL(close, 1, handle->process.stream_in);
  489. handle->process.stream_in = PAL_IDX_POISON;
  490. }
  491. if (access != PAL_DELETE_RD &&
  492. handle->process.stream_out != PAL_IDX_POISON) {
  493. INLINE_SYSCALL(close, 1, handle->process.stream_out);
  494. handle->process.stream_out = PAL_IDX_POISON;
  495. }
  496. if (handle->process.cargo != PAL_IDX_POISON)
  497. INLINE_SYSCALL(shutdown, 2, handle->process.cargo, shutdown);
  498. return 0;
  499. }
  500. #ifndef FIONREAD
  501. # define FIONREAD 0x541B
  502. #endif
  503. static int proc_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  504. {
  505. int ret, val;
  506. if (handle->process.stream_in == PAL_IDX_POISON)
  507. return -PAL_ERROR_BADHANDLE;
  508. memset(attr, 0, sizeof(PAL_STREAM_ATTR));
  509. ret = INLINE_SYSCALL(ioctl, 3, handle->process.stream_in, FIONREAD, &val);
  510. if (!IS_ERR(ret))
  511. attr->size = val;
  512. attr->disconnected = handle->__in.flags & (ERROR(0)|ERROR(1));
  513. attr->readable = (attr->size > 0);
  514. attr->writeable = handle->__in.flags & WRITEABLE(1);
  515. attr->nonblocking = handle->process.nonblocking;
  516. return 0;
  517. }
  518. static int proc_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
  519. {
  520. if (handle->process.stream_in == PAL_IDX_POISON)
  521. return -PAL_ERROR_BADHANDLE;
  522. int ret;
  523. if (attr->nonblocking != handle->process.nonblocking) {
  524. ret = INLINE_SYSCALL(fcntl, 3, handle->process.stream_in, F_SETFL,
  525. handle->process.nonblocking ? O_NONBLOCK : 0);
  526. if (IS_ERR(ret))
  527. return unix_to_pal_error(ERRNO(ret));
  528. handle->process.nonblocking = attr->nonblocking;
  529. }
  530. return 0;
  531. }
  532. struct handle_ops proc_ops = {
  533. .read = &proc_read,
  534. .write = &proc_write,
  535. .close = &proc_close,
  536. .delete = &proc_delete,
  537. .attrquerybyhdl = &proc_attrquerybyhdl,
  538. .attrsetbyhdl = &proc_attrsetbyhdl,
  539. };