file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <signal.h>
  10. #include <sys/ioctl.h>
  11. #include <sys/mount.h>
  12. #include <sys/socket.h>
  13. #include <sys/stat.h>
  14. #include <sys/un.h>
  15. #include "kern_constants.h"
  16. #include "os.h"
  17. #include "user.h"
  18. static void copy_stat(struct uml_stat *dst, const struct stat64 *src)
  19. {
  20. *dst = ((struct uml_stat) {
  21. .ust_dev = src->st_dev, /* device */
  22. .ust_ino = src->st_ino, /* inode */
  23. .ust_mode = src->st_mode, /* protection */
  24. .ust_nlink = src->st_nlink, /* number of hard links */
  25. .ust_uid = src->st_uid, /* user ID of owner */
  26. .ust_gid = src->st_gid, /* group ID of owner */
  27. .ust_size = src->st_size, /* total size, in bytes */
  28. .ust_blksize = src->st_blksize, /* blocksize for filesys I/O */
  29. .ust_blocks = src->st_blocks, /* number of blocks allocated */
  30. .ust_atime = src->st_atime, /* time of last access */
  31. .ust_mtime = src->st_mtime, /* time of last modification */
  32. .ust_ctime = src->st_ctime, /* time of last change */
  33. });
  34. }
  35. int os_stat_fd(const int fd, struct uml_stat *ubuf)
  36. {
  37. struct stat64 sbuf;
  38. int err;
  39. CATCH_EINTR(err = fstat64(fd, &sbuf));
  40. if (err < 0)
  41. return -errno;
  42. if (ubuf != NULL)
  43. copy_stat(ubuf, &sbuf);
  44. return err;
  45. }
  46. int os_stat_file(const char *file_name, struct uml_stat *ubuf)
  47. {
  48. struct stat64 sbuf;
  49. int err;
  50. CATCH_EINTR(err = stat64(file_name, &sbuf));
  51. if (err < 0)
  52. return -errno;
  53. if (ubuf != NULL)
  54. copy_stat(ubuf, &sbuf);
  55. return err;
  56. }
  57. int os_access(const char *file, int mode)
  58. {
  59. int amode, err;
  60. amode = (mode & OS_ACC_R_OK ? R_OK : 0) |
  61. (mode & OS_ACC_W_OK ? W_OK : 0) |
  62. (mode & OS_ACC_X_OK ? X_OK : 0) |
  63. (mode & OS_ACC_F_OK ? F_OK : 0);
  64. err = access(file, amode);
  65. if (err < 0)
  66. return -errno;
  67. return 0;
  68. }
  69. /* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
  70. int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
  71. {
  72. int err;
  73. err = ioctl(fd, cmd, arg);
  74. if (err < 0)
  75. return -errno;
  76. return err;
  77. }
  78. /* FIXME: ensure namebuf in os_get_if_name is big enough */
  79. int os_get_ifname(int fd, char* namebuf)
  80. {
  81. if (ioctl(fd, SIOCGIFNAME, namebuf) < 0)
  82. return -errno;
  83. return 0;
  84. }
  85. int os_set_slip(int fd)
  86. {
  87. int disc, sencap;
  88. disc = N_SLIP;
  89. if (ioctl(fd, TIOCSETD, &disc) < 0)
  90. return -errno;
  91. sencap = 0;
  92. if (ioctl(fd, SIOCSIFENCAP, &sencap) < 0)
  93. return -errno;
  94. return 0;
  95. }
  96. int os_mode_fd(int fd, int mode)
  97. {
  98. int err;
  99. CATCH_EINTR(err = fchmod(fd, mode));
  100. if (err < 0)
  101. return -errno;
  102. return 0;
  103. }
  104. int os_file_type(char *file)
  105. {
  106. struct uml_stat buf;
  107. int err;
  108. err = os_stat_file(file, &buf);
  109. if (err < 0)
  110. return err;
  111. if (S_ISDIR(buf.ust_mode))
  112. return OS_TYPE_DIR;
  113. else if (S_ISLNK(buf.ust_mode))
  114. return OS_TYPE_SYMLINK;
  115. else if (S_ISCHR(buf.ust_mode))
  116. return OS_TYPE_CHARDEV;
  117. else if (S_ISBLK(buf.ust_mode))
  118. return OS_TYPE_BLOCKDEV;
  119. else if (S_ISFIFO(buf.ust_mode))
  120. return OS_TYPE_FIFO;
  121. else if (S_ISSOCK(buf.ust_mode))
  122. return OS_TYPE_SOCK;
  123. else return OS_TYPE_FILE;
  124. }
  125. int os_file_mode(const char *file, struct openflags *mode_out)
  126. {
  127. int err;
  128. *mode_out = OPENFLAGS();
  129. err = access(file, W_OK);
  130. if (err && (errno != EACCES))
  131. return -errno;
  132. else if (!err)
  133. *mode_out = of_write(*mode_out);
  134. err = access(file, R_OK);
  135. if (err && (errno != EACCES))
  136. return -errno;
  137. else if (!err)
  138. *mode_out = of_read(*mode_out);
  139. return err;
  140. }
  141. int os_open_file(const char *file, struct openflags flags, int mode)
  142. {
  143. int fd, err, f = 0;
  144. if (flags.r && flags.w)
  145. f = O_RDWR;
  146. else if (flags.r)
  147. f = O_RDONLY;
  148. else if (flags.w)
  149. f = O_WRONLY;
  150. else f = 0;
  151. if (flags.s)
  152. f |= O_SYNC;
  153. if (flags.c)
  154. f |= O_CREAT;
  155. if (flags.t)
  156. f |= O_TRUNC;
  157. if (flags.e)
  158. f |= O_EXCL;
  159. if (flags.a)
  160. f |= O_APPEND;
  161. fd = open64(file, f, mode);
  162. if (fd < 0)
  163. return -errno;
  164. if (flags.cl && fcntl(fd, F_SETFD, 1)) {
  165. err = -errno;
  166. close(fd);
  167. return err;
  168. }
  169. return fd;
  170. }
  171. int os_connect_socket(const char *name)
  172. {
  173. struct sockaddr_un sock;
  174. int fd, err;
  175. sock.sun_family = AF_UNIX;
  176. snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
  177. fd = socket(AF_UNIX, SOCK_STREAM, 0);
  178. if (fd < 0) {
  179. err = -errno;
  180. goto out;
  181. }
  182. err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
  183. if (err) {
  184. err = -errno;
  185. goto out_close;
  186. }
  187. return fd;
  188. out_close:
  189. close(fd);
  190. out:
  191. return err;
  192. }
  193. void os_close_file(int fd)
  194. {
  195. close(fd);
  196. }
  197. int os_seek_file(int fd, unsigned long long offset)
  198. {
  199. unsigned long long actual;
  200. actual = lseek64(fd, offset, SEEK_SET);
  201. if (actual != offset)
  202. return -errno;
  203. return 0;
  204. }
  205. int os_read_file(int fd, void *buf, int len)
  206. {
  207. int n = read(fd, buf, len);
  208. if (n < 0)
  209. return -errno;
  210. return n;
  211. }
  212. int os_write_file(int fd, const void *buf, int len)
  213. {
  214. int n = write(fd, (void *) buf, len);
  215. if (n < 0)
  216. return -errno;
  217. return n;
  218. }
  219. int os_file_size(const char *file, unsigned long long *size_out)
  220. {
  221. struct uml_stat buf;
  222. int err;
  223. err = os_stat_file(file, &buf);
  224. if (err < 0) {
  225. printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
  226. -err);
  227. return err;
  228. }
  229. if (S_ISBLK(buf.ust_mode)) {
  230. int fd;
  231. long blocks;
  232. fd = open(file, O_RDONLY, 0);
  233. if (fd < 0) {
  234. err = -errno;
  235. printk(UM_KERN_ERR "Couldn't open \"%s\", "
  236. "errno = %d\n", file, errno);
  237. return err;
  238. }
  239. if (ioctl(fd, BLKGETSIZE, &blocks) < 0) {
  240. err = -errno;
  241. printk(UM_KERN_ERR "Couldn't get the block size of "
  242. "\"%s\", errno = %d\n", file, errno);
  243. close(fd);
  244. return err;
  245. }
  246. *size_out = ((long long) blocks) * 512;
  247. close(fd);
  248. }
  249. else *size_out = buf.ust_size;
  250. return 0;
  251. }
  252. int os_file_modtime(const char *file, unsigned long *modtime)
  253. {
  254. struct uml_stat buf;
  255. int err;
  256. err = os_stat_file(file, &buf);
  257. if (err < 0) {
  258. printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
  259. -err);
  260. return err;
  261. }
  262. *modtime = buf.ust_mtime;
  263. return 0;
  264. }
  265. int os_set_exec_close(int fd)
  266. {
  267. int err;
  268. CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
  269. if (err < 0)
  270. return -errno;
  271. return err;
  272. }
  273. int os_pipe(int *fds, int stream, int close_on_exec)
  274. {
  275. int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
  276. err = socketpair(AF_UNIX, type, 0, fds);
  277. if (err < 0)
  278. return -errno;
  279. if (!close_on_exec)
  280. return 0;
  281. err = os_set_exec_close(fds[0]);
  282. if (err < 0)
  283. goto error;
  284. err = os_set_exec_close(fds[1]);
  285. if (err < 0)
  286. goto error;
  287. return 0;
  288. error:
  289. printk(UM_KERN_ERR "os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
  290. -err);
  291. close(fds[1]);
  292. close(fds[0]);
  293. return err;
  294. }
  295. int os_set_fd_async(int fd)
  296. {
  297. int err, flags;
  298. flags = fcntl(fd, F_GETFL);
  299. if (flags < 0)
  300. return -errno;
  301. flags |= O_ASYNC | O_NONBLOCK;
  302. if (fcntl(fd, F_SETFL, flags) < 0) {
  303. err = -errno;
  304. printk(UM_KERN_ERR "os_set_fd_async : failed to set O_ASYNC "
  305. "and O_NONBLOCK on fd # %d, errno = %d\n", fd, errno);
  306. return err;
  307. }
  308. if ((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
  309. (fcntl(fd, F_SETOWN, os_getpid()) < 0)) {
  310. err = -errno;
  311. printk(UM_KERN_ERR "os_set_fd_async : Failed to fcntl F_SETOWN "
  312. "(or F_SETSIG) fd %d, errno = %d\n", fd, errno);
  313. return err;
  314. }
  315. return 0;
  316. }
  317. int os_clear_fd_async(int fd)
  318. {
  319. int flags;
  320. flags = fcntl(fd, F_GETFL);
  321. if (flags < 0)
  322. return -errno;
  323. flags &= ~(O_ASYNC | O_NONBLOCK);
  324. if (fcntl(fd, F_SETFL, flags) < 0)
  325. return -errno;
  326. return 0;
  327. }
  328. int os_set_fd_block(int fd, int blocking)
  329. {
  330. int flags;
  331. flags = fcntl(fd, F_GETFL);
  332. if (flags < 0)
  333. return -errno;
  334. if (blocking)
  335. flags &= ~O_NONBLOCK;
  336. else
  337. flags |= O_NONBLOCK;
  338. if (fcntl(fd, F_SETFL, flags) < 0)
  339. return -errno;
  340. return 0;
  341. }
  342. int os_accept_connection(int fd)
  343. {
  344. int new;
  345. new = accept(fd, NULL, 0);
  346. if (new < 0)
  347. return -errno;
  348. return new;
  349. }
  350. #ifndef SHUT_RD
  351. #define SHUT_RD 0
  352. #endif
  353. #ifndef SHUT_WR
  354. #define SHUT_WR 1
  355. #endif
  356. #ifndef SHUT_RDWR
  357. #define SHUT_RDWR 2
  358. #endif
  359. int os_shutdown_socket(int fd, int r, int w)
  360. {
  361. int what, err;
  362. if (r && w)
  363. what = SHUT_RDWR;
  364. else if (r)
  365. what = SHUT_RD;
  366. else if (w)
  367. what = SHUT_WR;
  368. else
  369. return -EINVAL;
  370. err = shutdown(fd, what);
  371. if (err < 0)
  372. return -errno;
  373. return 0;
  374. }
  375. int os_rcv_fd(int fd, int *helper_pid_out)
  376. {
  377. int new, n;
  378. char buf[CMSG_SPACE(sizeof(new))];
  379. struct msghdr msg;
  380. struct cmsghdr *cmsg;
  381. struct iovec iov;
  382. msg.msg_name = NULL;
  383. msg.msg_namelen = 0;
  384. iov = ((struct iovec) { .iov_base = helper_pid_out,
  385. .iov_len = sizeof(*helper_pid_out) });
  386. msg.msg_iov = &iov;
  387. msg.msg_iovlen = 1;
  388. msg.msg_control = buf;
  389. msg.msg_controllen = sizeof(buf);
  390. msg.msg_flags = 0;
  391. n = recvmsg(fd, &msg, 0);
  392. if (n < 0)
  393. return -errno;
  394. else if (n != iov.iov_len)
  395. *helper_pid_out = -1;
  396. cmsg = CMSG_FIRSTHDR(&msg);
  397. if (cmsg == NULL) {
  398. printk(UM_KERN_ERR "rcv_fd didn't receive anything, "
  399. "error = %d\n", errno);
  400. return -1;
  401. }
  402. if ((cmsg->cmsg_level != SOL_SOCKET) ||
  403. (cmsg->cmsg_type != SCM_RIGHTS)) {
  404. printk(UM_KERN_ERR "rcv_fd didn't receive a descriptor\n");
  405. return -1;
  406. }
  407. new = ((int *) CMSG_DATA(cmsg))[0];
  408. return new;
  409. }
  410. int os_create_unix_socket(const char *file, int len, int close_on_exec)
  411. {
  412. struct sockaddr_un addr;
  413. int sock, err;
  414. sock = socket(PF_UNIX, SOCK_DGRAM, 0);
  415. if (sock < 0)
  416. return -errno;
  417. if (close_on_exec) {
  418. err = os_set_exec_close(sock);
  419. if (err < 0)
  420. printk(UM_KERN_ERR "create_unix_socket : "
  421. "close_on_exec failed, err = %d", -err);
  422. }
  423. addr.sun_family = AF_UNIX;
  424. snprintf(addr.sun_path, len, "%s", file);
  425. err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
  426. if (err < 0)
  427. return -errno;
  428. return sock;
  429. }
  430. void os_flush_stdout(void)
  431. {
  432. fflush(stdout);
  433. }
  434. int os_lock_file(int fd, int excl)
  435. {
  436. int type = excl ? F_WRLCK : F_RDLCK;
  437. struct flock lock = ((struct flock) { .l_type = type,
  438. .l_whence = SEEK_SET,
  439. .l_start = 0,
  440. .l_len = 0 } );
  441. int err, save;
  442. err = fcntl(fd, F_SETLK, &lock);
  443. if (!err)
  444. goto out;
  445. save = -errno;
  446. err = fcntl(fd, F_GETLK, &lock);
  447. if (err) {
  448. err = -errno;
  449. goto out;
  450. }
  451. printk(UM_KERN_ERR "F_SETLK failed, file already locked by pid %d\n",
  452. lock.l_pid);
  453. err = save;
  454. out:
  455. return err;
  456. }
  457. unsigned os_major(unsigned long long dev)
  458. {
  459. return major(dev);
  460. }
  461. unsigned os_minor(unsigned long long dev)
  462. {
  463. return minor(dev);
  464. }
  465. unsigned long long os_makedev(unsigned major, unsigned minor)
  466. {
  467. return makedev(major, minor);
  468. }