1 | /* |
1 | /* |
2 | * libeio implementation |
2 | * libeio implementation |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012,2013 Marc Alexander Lehmann <libeio@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012,2013,2016 Marc Alexander Lehmann <libeio@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
42 | #endif |
42 | #endif |
43 | |
43 | |
44 | #include "eio.h" |
44 | #include "eio.h" |
45 | #include "ecb.h" |
45 | #include "ecb.h" |
46 | |
46 | |
47 | #ifdef EIO_STACKSIZE |
|
|
48 | # define X_STACKSIZE EIO_STACKSIZE |
|
|
49 | #endif |
|
|
50 | #include "xthread.h" |
|
|
51 | |
|
|
52 | #include <errno.h> |
47 | #include <errno.h> |
53 | #include <stddef.h> |
48 | #include <stddef.h> |
54 | #include <stdlib.h> |
49 | #include <stdlib.h> |
55 | #include <string.h> |
50 | #include <string.h> |
56 | #include <errno.h> |
51 | #include <errno.h> |
… | |
… | |
120 | #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) |
115 | #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) |
121 | |
116 | |
122 | #define chmod(path,mode) _chmod (path, mode) |
117 | #define chmod(path,mode) _chmod (path, mode) |
123 | #define dup(fd) _dup (fd) |
118 | #define dup(fd) _dup (fd) |
124 | #define dup2(fd1,fd2) _dup2 (fd1, fd2) |
119 | #define dup2(fd1,fd2) _dup2 (fd1, fd2) |
|
|
120 | #define pipe(fds) _pipe (fds, 4096, O_BINARY) |
125 | |
121 | |
|
|
122 | #define fcntl(fd,cmd,arg) EIO_ENOSYS () |
|
|
123 | #define ioctl(fd,cmd,arg) EIO_ENOSYS () |
126 | #define fchmod(fd,mode) EIO_ENOSYS () |
124 | #define fchmod(fd,mode) EIO_ENOSYS () |
127 | #define chown(path,uid,gid) EIO_ENOSYS () |
125 | #define chown(path,uid,gid) EIO_ENOSYS () |
128 | #define fchown(fd,uid,gid) EIO_ENOSYS () |
126 | #define fchown(fd,uid,gid) EIO_ENOSYS () |
129 | #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ |
127 | #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ |
130 | #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ |
128 | #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ |
… | |
… | |
203 | /* we could even stat and see if it exists */ |
201 | /* we could even stat and see if it exists */ |
204 | static int |
202 | static int |
205 | symlink (const char *old, const char *neu) |
203 | symlink (const char *old, const char *neu) |
206 | { |
204 | { |
207 | #if WINVER >= 0x0600 |
205 | #if WINVER >= 0x0600 |
|
|
206 | int flags; |
|
|
207 | |
|
|
208 | /* This tries out all combinations of SYMBOLIC_LINK_FLAG_DIRECTORY |
|
|
209 | * and SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE, |
|
|
210 | * with directory first. |
|
|
211 | */ |
|
|
212 | for (flags = 3; flags >= 0; --flags) |
208 | if (CreateSymbolicLink (neu, old, 1)) |
213 | if (CreateSymbolicLink (neu, old, flags)) |
209 | return 0; |
214 | return 0; |
210 | |
|
|
211 | if (CreateSymbolicLink (neu, old, 0)) |
|
|
212 | return 0; |
|
|
213 | #endif |
215 | #endif |
214 | |
216 | |
215 | return EIO_ERRNO (ENOENT, -1); |
217 | return EIO_ERRNO (ENOENT, -1); |
216 | } |
218 | } |
217 | |
219 | |
… | |
… | |
280 | # include <utime.h> |
282 | # include <utime.h> |
281 | #endif |
283 | #endif |
282 | |
284 | |
283 | #if HAVE_SYS_SYSCALL_H |
285 | #if HAVE_SYS_SYSCALL_H |
284 | # include <sys/syscall.h> |
286 | # include <sys/syscall.h> |
285 | #endif |
|
|
286 | |
|
|
287 | #if HAVE_SYS_PRCTL_H |
|
|
288 | # include <sys/prctl.h> |
|
|
289 | #endif |
287 | #endif |
290 | |
288 | |
291 | #if HAVE_SENDFILE |
289 | #if HAVE_SENDFILE |
292 | # if __linux |
290 | # if __linux |
293 | # include <sys/sendfile.h> |
291 | # include <sys/sendfile.h> |
… | |
… | |
318 | # define NAME_MAX 4096 |
316 | # define NAME_MAX 4096 |
319 | #endif |
317 | #endif |
320 | |
318 | |
321 | /* used for readlink etc. */ |
319 | /* used for readlink etc. */ |
322 | #ifndef PATH_MAX |
320 | #ifndef PATH_MAX |
323 | # define PATH_MAX 4096 |
321 | # define PATH_MAX 0 |
324 | #endif |
322 | #endif |
|
|
323 | |
|
|
324 | #ifndef EIO_PATH_MIN |
|
|
325 | # define EIO_PATH_MIN 8160 |
|
|
326 | #endif |
|
|
327 | |
|
|
328 | #define EIO_PATH_MAX (PATH_MAX <= EIO_PATH_MIN ? EIO_PATH_MIN : PATH_MAX) |
325 | |
329 | |
326 | /* buffer size for various temporary buffers */ |
330 | /* buffer size for various temporary buffers */ |
327 | #define EIO_BUFSIZE 65536 |
331 | #define EIO_BUFSIZE 65536 |
328 | |
332 | |
329 | #define dBUF \ |
333 | #define dBUF \ |
… | |
… | |
335 | #define FUBd \ |
339 | #define FUBd \ |
336 | free (eio_buf) |
340 | free (eio_buf) |
337 | |
341 | |
338 | /*****************************************************************************/ |
342 | /*****************************************************************************/ |
339 | |
343 | |
340 | struct tmpbuf |
|
|
341 | { |
|
|
342 | void *ptr; |
|
|
343 | int len; |
|
|
344 | }; |
|
|
345 | |
|
|
346 | static void * |
|
|
347 | tmpbuf_get (struct tmpbuf *buf, int len) |
|
|
348 | { |
|
|
349 | if (buf->len < len) |
|
|
350 | { |
|
|
351 | free (buf->ptr); |
|
|
352 | buf->ptr = malloc (buf->len = len); |
|
|
353 | } |
|
|
354 | |
|
|
355 | return buf->ptr; |
|
|
356 | } |
|
|
357 | |
|
|
358 | struct tmpbuf; |
344 | struct etp_tmpbuf; |
359 | |
345 | |
360 | #if _POSIX_VERSION >= 200809L |
346 | #if _POSIX_VERSION >= 200809L |
361 | #define HAVE_AT 1 |
347 | #define HAVE_AT 1 |
362 | #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) |
348 | #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) |
363 | #ifndef O_SEARCH |
349 | #ifndef O_SEARCH |
364 | #define O_SEARCH O_RDONLY |
350 | #define O_SEARCH O_RDONLY |
365 | #endif |
351 | #endif |
366 | #else |
352 | #else |
367 | #define HAVE_AT 0 |
353 | #define HAVE_AT 0 |
368 | static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path); |
354 | static const char *wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path); |
369 | #endif |
355 | #endif |
370 | |
356 | |
371 | struct eio_pwd |
357 | struct eio_pwd |
372 | { |
358 | { |
373 | #if HAVE_AT |
359 | #if HAVE_AT |
… | |
… | |
383 | #define ETP_PRI_MAX EIO_PRI_MAX |
369 | #define ETP_PRI_MAX EIO_PRI_MAX |
384 | |
370 | |
385 | #define ETP_TYPE_QUIT -1 |
371 | #define ETP_TYPE_QUIT -1 |
386 | #define ETP_TYPE_GROUP EIO_GROUP |
372 | #define ETP_TYPE_GROUP EIO_GROUP |
387 | |
373 | |
|
|
374 | static void eio_nop_callback (void) { } |
|
|
375 | static void (*eio_want_poll_cb)(void) = eio_nop_callback; |
|
|
376 | static void (*eio_done_poll_cb)(void) = eio_nop_callback; |
|
|
377 | |
|
|
378 | #define ETP_WANT_POLL(pool) eio_want_poll_cb () |
|
|
379 | #define ETP_DONE_POLL(pool) eio_done_poll_cb () |
|
|
380 | |
388 | struct etp_worker; |
381 | struct etp_worker; |
389 | |
|
|
390 | #define ETP_REQ eio_req |
382 | #define ETP_REQ eio_req |
391 | #define ETP_DESTROY(req) eio_destroy (req) |
383 | #define ETP_DESTROY(req) eio_destroy (req) |
392 | static int eio_finish (eio_req *req); |
384 | static int eio_finish (eio_req *req); |
393 | #define ETP_FINISH(req) eio_finish (req) |
385 | #define ETP_FINISH(req) eio_finish (req) |
394 | static void eio_execute (struct etp_worker *self, eio_req *req); |
386 | static void eio_execute (struct etp_worker *self, eio_req *req); |
395 | #define ETP_EXECUTE(wrk,req) eio_execute (wrk, req) |
387 | #define ETP_EXECUTE(wrk,req) eio_execute (wrk, req) |
396 | |
388 | |
397 | #include "etp.c" |
389 | #include "etp.c" |
398 | |
390 | |
|
|
391 | static struct etp_pool eio_pool; |
|
|
392 | #define EIO_POOL (&eio_pool) |
|
|
393 | |
399 | /*****************************************************************************/ |
394 | /*****************************************************************************/ |
400 | |
395 | |
401 | static void |
396 | static void |
402 | grp_try_feed (eio_req *grp) |
397 | grp_try_feed (eio_req *grp) |
403 | { |
398 | { |
404 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
399 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
405 | { |
400 | { |
406 | grp->flags &= ~EIO_FLAG_GROUPADD; |
401 | grp->flags &= ~ETP_FLAG_GROUPADD; |
407 | |
402 | |
408 | EIO_FEED (grp); |
403 | EIO_FEED (grp); |
409 | |
404 | |
410 | /* stop if no progress has been made */ |
405 | /* stop if no progress has been made */ |
411 | if (!(grp->flags & EIO_FLAG_GROUPADD)) |
406 | if (!(grp->flags & ETP_FLAG_GROUPADD)) |
412 | { |
407 | { |
413 | grp->feed = 0; |
408 | grp->feed = 0; |
414 | break; |
409 | break; |
415 | } |
410 | } |
416 | } |
411 | } |
… | |
… | |
423 | |
418 | |
424 | /* call feeder, if applicable */ |
419 | /* call feeder, if applicable */ |
425 | grp_try_feed (grp); |
420 | grp_try_feed (grp); |
426 | |
421 | |
427 | /* finish, if done */ |
422 | /* finish, if done */ |
428 | if (!grp->size && grp->int1) |
423 | if (!grp->size && grp->flags & ETP_FLAG_DELAYED) |
429 | return eio_finish (grp); |
424 | return eio_finish (grp); |
430 | else |
425 | else |
431 | return 0; |
426 | return 0; |
432 | } |
427 | } |
433 | |
428 | |
… | |
… | |
469 | } |
464 | } |
470 | |
465 | |
471 | void |
466 | void |
472 | eio_grp_cancel (eio_req *grp) |
467 | eio_grp_cancel (eio_req *grp) |
473 | { |
468 | { |
474 | etp_grp_cancel (grp); |
469 | etp_grp_cancel (EIO_POOL, grp); |
475 | } |
470 | } |
476 | |
471 | |
477 | void |
472 | void |
478 | eio_cancel (eio_req *req) |
473 | eio_cancel (eio_req *req) |
479 | { |
474 | { |
480 | etp_cancel (req); |
475 | etp_cancel (EIO_POOL, req); |
481 | } |
476 | } |
482 | |
477 | |
483 | void |
478 | void |
484 | eio_submit (eio_req *req) |
479 | eio_submit (eio_req *req) |
485 | { |
480 | { |
486 | etp_submit (req); |
481 | etp_submit (EIO_POOL, req); |
487 | } |
482 | } |
488 | |
483 | |
489 | unsigned int |
484 | unsigned int |
490 | eio_nreqs (void) |
485 | eio_nreqs (void) |
491 | { |
486 | { |
492 | return etp_nreqs (); |
487 | return etp_nreqs (EIO_POOL); |
493 | } |
488 | } |
494 | |
489 | |
495 | unsigned int |
490 | unsigned int |
496 | eio_nready (void) |
491 | eio_nready (void) |
497 | { |
492 | { |
498 | return etp_nready (); |
493 | return etp_nready (EIO_POOL); |
499 | } |
494 | } |
500 | |
495 | |
501 | unsigned int |
496 | unsigned int |
502 | eio_npending (void) |
497 | eio_npending (void) |
503 | { |
498 | { |
504 | return etp_npending (); |
499 | return etp_npending (EIO_POOL); |
505 | } |
500 | } |
506 | |
501 | |
507 | unsigned int ecb_cold |
502 | unsigned int ecb_cold |
508 | eio_nthreads (void) |
503 | eio_nthreads (void) |
509 | { |
504 | { |
510 | return etp_nthreads (); |
505 | return etp_nthreads (EIO_POOL); |
511 | } |
506 | } |
512 | |
507 | |
513 | void ecb_cold |
508 | void ecb_cold |
514 | eio_set_max_poll_time (double nseconds) |
509 | eio_set_max_poll_time (double nseconds) |
515 | { |
510 | { |
516 | etp_set_max_poll_time (nseconds); |
511 | etp_set_max_poll_time (EIO_POOL, nseconds); |
517 | } |
512 | } |
518 | |
513 | |
519 | void ecb_cold |
514 | void ecb_cold |
520 | eio_set_max_poll_reqs (unsigned int maxreqs) |
515 | eio_set_max_poll_reqs (unsigned int maxreqs) |
521 | { |
516 | { |
522 | etp_set_max_poll_reqs (maxreqs); |
517 | etp_set_max_poll_reqs (EIO_POOL, maxreqs); |
523 | } |
518 | } |
524 | |
519 | |
525 | void ecb_cold |
520 | void ecb_cold |
526 | eio_set_max_idle (unsigned int nthreads) |
521 | eio_set_max_idle (unsigned int nthreads) |
527 | { |
522 | { |
528 | etp_set_max_idle (nthreads); |
523 | etp_set_max_idle (EIO_POOL, nthreads); |
529 | } |
524 | } |
530 | |
525 | |
531 | void ecb_cold |
526 | void ecb_cold |
532 | eio_set_idle_timeout (unsigned int seconds) |
527 | eio_set_idle_timeout (unsigned int seconds) |
533 | { |
528 | { |
534 | etp_set_idle_timeout (seconds); |
529 | etp_set_idle_timeout (EIO_POOL, seconds); |
535 | } |
530 | } |
536 | |
531 | |
537 | void ecb_cold |
532 | void ecb_cold |
538 | eio_set_min_parallel (unsigned int nthreads) |
533 | eio_set_min_parallel (unsigned int nthreads) |
539 | { |
534 | { |
540 | etp_set_min_parallel (nthreads); |
535 | etp_set_min_parallel (EIO_POOL, nthreads); |
541 | } |
536 | } |
542 | |
537 | |
543 | void ecb_cold |
538 | void ecb_cold |
544 | eio_set_max_parallel (unsigned int nthreads) |
539 | eio_set_max_parallel (unsigned int nthreads) |
545 | { |
540 | { |
546 | etp_set_max_parallel (nthreads); |
541 | etp_set_max_parallel (EIO_POOL, nthreads); |
547 | } |
542 | } |
548 | |
543 | |
549 | int eio_poll (void) |
544 | int eio_poll (void) |
550 | { |
545 | { |
551 | return etp_poll (); |
546 | return etp_poll (EIO_POOL); |
552 | } |
547 | } |
553 | |
548 | |
554 | /*****************************************************************************/ |
549 | /*****************************************************************************/ |
555 | /* work around various missing functions */ |
550 | /* work around various missing functions */ |
556 | |
551 | |
… | |
… | |
966 | req->result = req->offs == (off_t)-1 ? -1 : 0; |
961 | req->result = req->offs == (off_t)-1 ? -1 : 0; |
967 | } |
962 | } |
968 | |
963 | |
969 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
964 | /* result will always end up in tmpbuf, there is always space for adding a 0-byte */ |
970 | static int |
965 | static int |
971 | eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
966 | eio__realpath (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) |
972 | { |
967 | { |
|
|
968 | char *res; |
973 | const char *rel = path; |
969 | const char *rel = path; |
974 | char *res; |
|
|
975 | char *tmp1, *tmp2; |
970 | char *tmp1, *tmp2; |
976 | #if SYMLOOP_MAX > 32 |
971 | #if SYMLOOP_MAX > 32 |
977 | int symlinks = SYMLOOP_MAX; |
972 | int symlinks = SYMLOOP_MAX; |
978 | #else |
973 | #else |
979 | int symlinks = 32; |
974 | int symlinks = 32; |
… | |
… | |
985 | |
980 | |
986 | errno = ENOENT; |
981 | errno = ENOENT; |
987 | if (!*rel) |
982 | if (!*rel) |
988 | return -1; |
983 | return -1; |
989 | |
984 | |
990 | res = tmpbuf_get (tmpbuf, PATH_MAX * 3); |
985 | res = etp_tmpbuf_get (tmpbuf, EIO_PATH_MAX * 3); |
|
|
986 | #ifdef _WIN32 |
|
|
987 | if (_access (rel, 4) != 0) |
|
|
988 | return -1; |
|
|
989 | |
|
|
990 | symlinks = GetFullPathName (rel, EIO_PATH_MAX * 3, res, 0); |
|
|
991 | |
|
|
992 | errno = ENAMETOOLONG; |
|
|
993 | if (symlinks >= EIO_PATH_MAX * 3) |
|
|
994 | return -1; |
|
|
995 | |
|
|
996 | errno = EIO; |
|
|
997 | if (symlinks <= 0) |
|
|
998 | return -1; |
|
|
999 | |
|
|
1000 | return symlinks; |
|
|
1001 | |
|
|
1002 | #else |
991 | tmp1 = res + PATH_MAX; |
1003 | tmp1 = res + EIO_PATH_MAX; |
992 | tmp2 = tmp1 + PATH_MAX; |
1004 | tmp2 = tmp1 + EIO_PATH_MAX; |
993 | |
1005 | |
994 | #if 0 /* disabled, the musl way to do things is just too racy */ |
1006 | #if 0 /* disabled, the musl way to do things is just too racy */ |
995 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
1007 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
996 | /* on linux we may be able to ask the kernel */ |
1008 | /* on linux we may be able to ask the kernel */ |
997 | { |
1009 | { |
998 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
1010 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
999 | |
1011 | |
1000 | if (fd >= 0) |
1012 | if (fd >= 0) |
1001 | { |
1013 | { |
1002 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
1014 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
1003 | req->result = readlink (tmp1, res, PATH_MAX); |
1015 | req->result = readlink (tmp1, res, EIO_PATH_MAX); |
1004 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
1016 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
1005 | close (fd); |
1017 | close (fd); |
1006 | |
1018 | |
1007 | if (req->result > 0) |
1019 | if (req->result > 0) |
1008 | goto done; |
1020 | goto done; |
… | |
… | |
1021 | if (wd == EIO_INVALID_WD) |
1033 | if (wd == EIO_INVALID_WD) |
1022 | return -1; |
1034 | return -1; |
1023 | |
1035 | |
1024 | if (wd == EIO_CWD) |
1036 | if (wd == EIO_CWD) |
1025 | { |
1037 | { |
1026 | if (!getcwd (res, PATH_MAX)) |
1038 | if (!getcwd (res, EIO_PATH_MAX)) |
1027 | return -1; |
1039 | return -1; |
1028 | |
1040 | |
1029 | len = strlen (res); |
1041 | len = strlen (res); |
1030 | } |
1042 | } |
1031 | else |
1043 | else |
… | |
… | |
1078 | |
1090 | |
1079 | /* zero-terminate, for readlink */ |
1091 | /* zero-terminate, for readlink */ |
1080 | res [len + 1] = 0; |
1092 | res [len + 1] = 0; |
1081 | |
1093 | |
1082 | /* now check if it's a symlink */ |
1094 | /* now check if it's a symlink */ |
1083 | linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); |
1095 | linklen = readlink (tmpbuf->ptr, tmp1, EIO_PATH_MAX); |
1084 | |
1096 | |
1085 | if (linklen < 0) |
1097 | if (linklen < 0) |
1086 | { |
1098 | { |
1087 | if (errno != EINVAL) |
1099 | if (errno != EINVAL) |
1088 | return -1; |
1100 | return -1; |
… | |
… | |
1094 | { |
1106 | { |
1095 | /* yay, it was a symlink - build new path in tmp2 */ |
1107 | /* yay, it was a symlink - build new path in tmp2 */ |
1096 | int rellen = strlen (rel); |
1108 | int rellen = strlen (rel); |
1097 | |
1109 | |
1098 | errno = ENAMETOOLONG; |
1110 | errno = ENAMETOOLONG; |
1099 | if (linklen + 1 + rellen >= PATH_MAX) |
1111 | if (linklen + 1 + rellen >= EIO_PATH_MAX) /* also catch linklen >= EIO_PATH_MAX */ |
1100 | return -1; |
1112 | return -1; |
1101 | |
1113 | |
1102 | errno = ELOOP; |
1114 | errno = ELOOP; |
1103 | if (!--symlinks) |
1115 | if (!--symlinks) |
1104 | return -1; |
1116 | return -1; |
… | |
… | |
1118 | /* special case for the lone root path */ |
1130 | /* special case for the lone root path */ |
1119 | if (res == tmpbuf->ptr) |
1131 | if (res == tmpbuf->ptr) |
1120 | *res++ = '/'; |
1132 | *res++ = '/'; |
1121 | |
1133 | |
1122 | return res - (char *)tmpbuf->ptr; |
1134 | return res - (char *)tmpbuf->ptr; |
|
|
1135 | #endif |
1123 | } |
1136 | } |
1124 | |
1137 | |
1125 | static signed char |
1138 | static signed char |
1126 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1139 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1127 | { |
1140 | { |
… | |
… | |
1586 | #if !HAVE_AT |
1599 | #if !HAVE_AT |
1587 | |
1600 | |
1588 | /* a bit like realpath, but usually faster because it doesn'T have to return */ |
1601 | /* a bit like realpath, but usually faster because it doesn'T have to return */ |
1589 | /* an absolute or canonical path */ |
1602 | /* an absolute or canonical path */ |
1590 | static const char * |
1603 | static const char * |
1591 | wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1604 | wd_expand (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1592 | { |
1605 | { |
1593 | if (!wd || *path == '/') |
1606 | if (!wd || *path == '/') |
1594 | return path; |
1607 | return path; |
1595 | |
1608 | |
1596 | if (path [0] == '.' && !path [1]) |
1609 | if (path [0] == '.' && !path [1]) |
… | |
… | |
1598 | |
1611 | |
1599 | { |
1612 | { |
1600 | int l1 = wd->len; |
1613 | int l1 = wd->len; |
1601 | int l2 = strlen (path); |
1614 | int l2 = strlen (path); |
1602 | |
1615 | |
1603 | char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); |
1616 | char *res = etp_tmpbuf_get (tmpbuf, l1 + l2 + 2); |
1604 | |
1617 | |
1605 | memcpy (res, wd->str, l1); |
1618 | memcpy (res, wd->str, l1); |
1606 | res [l1] = '/'; |
1619 | res [l1] = '/'; |
1607 | memcpy (res + l1 + 1, path, l2 + 1); |
1620 | memcpy (res + l1 + 1, path, l2 + 1); |
1608 | |
1621 | |
… | |
… | |
1611 | } |
1624 | } |
1612 | |
1625 | |
1613 | #endif |
1626 | #endif |
1614 | |
1627 | |
1615 | static eio_wd |
1628 | static eio_wd |
1616 | eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1629 | eio__wd_open_sync (struct etp_tmpbuf *tmpbuf, eio_wd wd, const char *path) |
1617 | { |
1630 | { |
1618 | int fd; |
1631 | int fd; |
1619 | eio_wd res; |
1632 | eio_wd res; |
1620 | int len = eio__realpath (tmpbuf, wd, path); |
1633 | int len = eio__realpath (tmpbuf, wd, path); |
1621 | |
1634 | |
… | |
… | |
1643 | } |
1656 | } |
1644 | |
1657 | |
1645 | eio_wd |
1658 | eio_wd |
1646 | eio_wd_open_sync (eio_wd wd, const char *path) |
1659 | eio_wd_open_sync (eio_wd wd, const char *path) |
1647 | { |
1660 | { |
1648 | struct tmpbuf tmpbuf = { 0 }; |
1661 | struct etp_tmpbuf tmpbuf = { }; |
1649 | wd = eio__wd_open_sync (&tmpbuf, wd, path); |
1662 | wd = eio__wd_open_sync (&tmpbuf, wd, path); |
1650 | free (tmpbuf.ptr); |
1663 | free (tmpbuf.ptr); |
1651 | |
1664 | |
1652 | return wd; |
1665 | return wd; |
1653 | } |
1666 | } |
… | |
… | |
1702 | /*****************************************************************************/ |
1715 | /*****************************************************************************/ |
1703 | |
1716 | |
1704 | #define ALLOC(len) \ |
1717 | #define ALLOC(len) \ |
1705 | if (!req->ptr2) \ |
1718 | if (!req->ptr2) \ |
1706 | { \ |
1719 | { \ |
1707 | X_LOCK (wrklock); \ |
1720 | X_LOCK (EIO_POOL->wrklock); \ |
1708 | req->flags |= EIO_FLAG_PTR2_FREE; \ |
1721 | req->flags |= EIO_FLAG_PTR2_FREE; \ |
1709 | X_UNLOCK (wrklock); \ |
1722 | X_UNLOCK (EIO_POOL->wrklock); \ |
1710 | req->ptr2 = malloc (len); \ |
1723 | req->ptr2 = malloc (len); \ |
1711 | if (!req->ptr2) \ |
1724 | if (!req->ptr2) \ |
1712 | { \ |
1725 | { \ |
1713 | errno = ENOMEM; \ |
1726 | errno = ENOMEM; \ |
1714 | req->result = -1; \ |
1727 | req->result = -1; \ |
1715 | break; \ |
1728 | break; \ |
1716 | } \ |
1729 | } \ |
1717 | } |
1730 | } |
1718 | |
1731 | |
1719 | static void ecb_noinline ecb_cold |
|
|
1720 | etp_proc_init (void) |
|
|
1721 | { |
|
|
1722 | #if HAVE_PRCTL_SET_NAME |
|
|
1723 | /* provide a more sensible "thread name" */ |
|
|
1724 | char name[16 + 1]; |
|
|
1725 | const int namelen = sizeof (name) - 1; |
|
|
1726 | int len; |
|
|
1727 | |
|
|
1728 | prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); |
|
|
1729 | name [namelen] = 0; |
|
|
1730 | len = strlen (name); |
|
|
1731 | strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio"); |
|
|
1732 | prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0); |
|
|
1733 | #endif |
|
|
1734 | } |
|
|
1735 | |
|
|
1736 | /* TODO: move somehow to etp.c */ |
|
|
1737 | X_THREAD_PROC (etp_proc) |
|
|
1738 | { |
|
|
1739 | ETP_REQ *req; |
|
|
1740 | struct timespec ts; |
|
|
1741 | etp_worker *self = (etp_worker *)thr_arg; |
|
|
1742 | |
|
|
1743 | etp_proc_init (); |
|
|
1744 | |
|
|
1745 | /* try to distribute timeouts somewhat evenly */ |
|
|
1746 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
|
|
1747 | |
|
|
1748 | for (;;) |
|
|
1749 | { |
|
|
1750 | ts.tv_sec = 0; |
|
|
1751 | |
|
|
1752 | X_LOCK (reqlock); |
|
|
1753 | |
|
|
1754 | for (;;) |
|
|
1755 | { |
|
|
1756 | req = reqq_shift (&req_queue); |
|
|
1757 | |
|
|
1758 | if (req) |
|
|
1759 | break; |
|
|
1760 | |
|
|
1761 | if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ |
|
|
1762 | { |
|
|
1763 | X_UNLOCK (reqlock); |
|
|
1764 | X_LOCK (wrklock); |
|
|
1765 | --started; |
|
|
1766 | X_UNLOCK (wrklock); |
|
|
1767 | goto quit; |
|
|
1768 | } |
|
|
1769 | |
|
|
1770 | ++idle; |
|
|
1771 | |
|
|
1772 | if (idle <= max_idle) |
|
|
1773 | /* we are allowed to idle, so do so without any timeout */ |
|
|
1774 | X_COND_WAIT (reqwait, reqlock); |
|
|
1775 | else |
|
|
1776 | { |
|
|
1777 | /* initialise timeout once */ |
|
|
1778 | if (!ts.tv_sec) |
|
|
1779 | ts.tv_sec = time (0) + idle_timeout; |
|
|
1780 | |
|
|
1781 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) |
|
|
1782 | ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */ |
|
|
1783 | } |
|
|
1784 | |
|
|
1785 | --idle; |
|
|
1786 | } |
|
|
1787 | |
|
|
1788 | --nready; |
|
|
1789 | |
|
|
1790 | X_UNLOCK (reqlock); |
|
|
1791 | |
|
|
1792 | if (req->type == ETP_TYPE_QUIT) |
|
|
1793 | goto quit; |
|
|
1794 | |
|
|
1795 | ETP_EXECUTE (self, req); |
|
|
1796 | |
|
|
1797 | X_LOCK (reslock); |
|
|
1798 | |
|
|
1799 | ++npending; |
|
|
1800 | |
|
|
1801 | if (!reqq_push (&res_queue, req) && want_poll_cb) |
|
|
1802 | want_poll_cb (); |
|
|
1803 | |
|
|
1804 | etp_worker_clear (self); |
|
|
1805 | |
|
|
1806 | X_UNLOCK (reslock); |
|
|
1807 | } |
|
|
1808 | |
|
|
1809 | quit: |
|
|
1810 | free (req); |
|
|
1811 | |
|
|
1812 | X_LOCK (wrklock); |
|
|
1813 | etp_worker_free (self); |
|
|
1814 | X_UNLOCK (wrklock); |
|
|
1815 | |
|
|
1816 | return 0; |
|
|
1817 | } |
|
|
1818 | |
|
|
1819 | /*****************************************************************************/ |
1732 | /*****************************************************************************/ |
1820 | |
1733 | |
1821 | int ecb_cold |
1734 | int ecb_cold |
1822 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1735 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1823 | { |
1736 | { |
1824 | return etp_init (want_poll, done_poll); |
1737 | eio_want_poll_cb = want_poll; |
|
|
1738 | eio_done_poll_cb = done_poll; |
|
|
1739 | |
|
|
1740 | return etp_init (EIO_POOL, 0, 0, 0); |
1825 | } |
1741 | } |
1826 | |
1742 | |
1827 | ecb_inline void |
1743 | ecb_inline void |
1828 | eio_api_destroy (eio_req *req) |
1744 | eio_api_destroy (eio_req *req) |
1829 | { |
1745 | { |
… | |
… | |
1902 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1818 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1903 | : read (req->int1, req->ptr2, req->size); break; |
1819 | : read (req->int1, req->ptr2, req->size); break; |
1904 | case EIO_WRITE: req->result = req->offs >= 0 |
1820 | case EIO_WRITE: req->result = req->offs >= 0 |
1905 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
1821 | ? pwrite (req->int1, req->ptr2, req->size, req->offs) |
1906 | : write (req->int1, req->ptr2, req->size); break; |
1822 | : write (req->int1, req->ptr2, req->size); break; |
|
|
1823 | |
|
|
1824 | case EIO_FCNTL: req->result = fcntl (req->int1, (int) req->int2, req->ptr2); break; |
|
|
1825 | case EIO_IOCTL: req->result = ioctl (req->int1, (unsigned long)req->int2, req->ptr2); break; |
1907 | |
1826 | |
1908 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1827 | case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1909 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; |
1828 | case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; |
1910 | |
1829 | |
1911 | #if HAVE_AT |
1830 | #if HAVE_AT |
… | |
… | |
1930 | ? rename (req->wd->str, req->ptr2) |
1849 | ? rename (req->wd->str, req->ptr2) |
1931 | : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; |
1850 | : renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; |
1932 | case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; |
1851 | case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; |
1933 | case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; |
1852 | case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; |
1934 | case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1853 | case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1935 | case EIO_READLINK: ALLOC (PATH_MAX); |
|
|
1936 | req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; |
|
|
1937 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1854 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1938 | req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; |
1855 | req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; |
|
|
1856 | case EIO_READLINK: ALLOC (EIO_PATH_MAX); |
|
|
1857 | req->result = readlinkat (dirfd, req->ptr1, req->ptr2, EIO_PATH_MAX); |
|
|
1858 | if (req->result == EIO_PATH_MAX) |
|
|
1859 | { |
|
|
1860 | req->result = -1; |
|
|
1861 | errno = ENAMETOOLONG; |
|
|
1862 | } |
|
|
1863 | break; |
1939 | case EIO_UTIME: |
1864 | case EIO_UTIME: |
1940 | case EIO_FUTIME: |
1865 | case EIO_FUTIME: |
1941 | { |
1866 | { |
1942 | struct timespec ts[2]; |
1867 | struct timespec ts[2]; |
1943 | struct timespec *times; |
1868 | struct timespec *times; |
… | |
… | |
1976 | case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; |
1901 | case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; |
1977 | case EIO_RENAME: req->result = rename (path , req->ptr2); break; |
1902 | case EIO_RENAME: req->result = rename (path , req->ptr2); break; |
1978 | case EIO_LINK: req->result = link (path , req->ptr2); break; |
1903 | case EIO_LINK: req->result = link (path , req->ptr2); break; |
1979 | case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; |
1904 | case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; |
1980 | case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; |
1905 | case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; |
1981 | case EIO_READLINK: ALLOC (PATH_MAX); |
|
|
1982 | req->result = readlink (path, req->ptr2, PATH_MAX); break; |
|
|
1983 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1906 | case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); |
1984 | req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; |
1907 | req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; |
|
|
1908 | case EIO_READLINK: ALLOC (EIO_PATH_MAX); |
|
|
1909 | req->result = readlink (path, req->ptr2, EIO_PATH_MAX); |
|
|
1910 | if (req->result == EIO_PATH_MAX) |
|
|
1911 | { |
|
|
1912 | req->result = -1; |
|
|
1913 | errno = ENAMETOOLONG; |
|
|
1914 | } |
|
|
1915 | break; |
1985 | |
1916 | |
1986 | case EIO_UTIME: |
1917 | case EIO_UTIME: |
1987 | case EIO_FUTIME: |
1918 | case EIO_FUTIME: |
1988 | { |
1919 | { |
1989 | struct timeval tv[2]; |
1920 | struct timeval tv[2]; |
… | |
… | |
2054 | req->result = select (0, 0, 0, 0, &tv); |
1985 | req->result = select (0, 0, 0, 0, &tv); |
2055 | } |
1986 | } |
2056 | #endif |
1987 | #endif |
2057 | break; |
1988 | break; |
2058 | |
1989 | |
|
|
1990 | #if 0 |
2059 | case EIO_GROUP: |
1991 | case EIO_GROUP: |
2060 | abort (); /* handled in eio_request */ |
1992 | abort (); /* handled in eio_request */ |
|
|
1993 | #endif |
2061 | |
1994 | |
2062 | case EIO_NOP: |
1995 | case EIO_NOP: |
2063 | req->result = 0; |
1996 | req->result = 0; |
2064 | break; |
1997 | break; |
2065 | |
1998 | |
… | |
… | |
2168 | } |
2101 | } |
2169 | |
2102 | |
2170 | eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2103 | eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) |
2171 | { |
2104 | { |
2172 | REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
2105 | REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; |
|
|
2106 | } |
|
|
2107 | |
|
|
2108 | eio_req *eio_fcntl (int fd, int cmd, void *arg, int pri, eio_cb cb, void *data) |
|
|
2109 | { |
|
|
2110 | REQ (EIO_IOCTL); req->int1 = fd; req->int2 = cmd; req->ptr2 = arg; SEND; |
|
|
2111 | } |
|
|
2112 | |
|
|
2113 | eio_req *eio_ioctl (int fd, unsigned long request, void *buf, int pri, eio_cb cb, void *data) |
|
|
2114 | { |
|
|
2115 | REQ (EIO_IOCTL); req->int1 = fd; req->int2 = request; req->ptr2 = buf; SEND; |
2173 | } |
2116 | } |
2174 | |
2117 | |
2175 | eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
2118 | eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) |
2176 | { |
2119 | { |
2177 | REQ (EIO_FSTAT); req->int1 = fd; SEND; |
2120 | REQ (EIO_FSTAT); req->int1 = fd; SEND; |
… | |
… | |
2365 | void |
2308 | void |
2366 | eio_grp_add (eio_req *grp, eio_req *req) |
2309 | eio_grp_add (eio_req *grp, eio_req *req) |
2367 | { |
2310 | { |
2368 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2311 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2369 | |
2312 | |
2370 | grp->flags |= EIO_FLAG_GROUPADD; |
2313 | grp->flags |= ETP_FLAG_GROUPADD; |
2371 | |
2314 | |
2372 | ++grp->size; |
2315 | ++grp->size; |
2373 | req->grp = grp; |
2316 | req->grp = grp; |
2374 | |
2317 | |
2375 | req->grp_prev = 0; |
2318 | req->grp_prev = 0; |