fd.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  1. /*
  2. * Server-side file descriptor management
  3. *
  4. * Copyright (C) 2000, 2003 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <limits.h>
  26. #include <signal.h>
  27. #include <stdarg.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #ifdef HAVE_POLL_H
  32. #include <poll.h>
  33. #endif
  34. #ifdef HAVE_SYS_POLL_H
  35. #include <sys/poll.h>
  36. #endif
  37. #ifdef HAVE_LINUX_MAJOR_H
  38. #include <linux/major.h>
  39. #endif
  40. #ifdef HAVE_SYS_STATVFS_H
  41. #include <sys/statvfs.h>
  42. #endif
  43. #ifdef HAVE_SYS_VFS_H
  44. /* Work around a conflict with Solaris' system list defined in sys/list.h. */
  45. #define list SYSLIST
  46. #define list_next SYSLIST_NEXT
  47. #define list_prev SYSLIST_PREV
  48. #define list_head SYSLIST_HEAD
  49. #define list_tail SYSLIST_TAIL
  50. #define list_move_tail SYSLIST_MOVE_TAIL
  51. #define list_remove SYSLIST_REMOVE
  52. #include <sys/vfs.h>
  53. #undef list
  54. #undef list_next
  55. #undef list_prev
  56. #undef list_head
  57. #undef list_tail
  58. #undef list_move_tail
  59. #undef list_remove
  60. #endif
  61. #ifdef HAVE_SYS_PARAM_H
  62. #include <sys/param.h>
  63. #endif
  64. #ifdef HAVE_SYS_MOUNT_H
  65. #include <sys/mount.h>
  66. #endif
  67. #ifdef HAVE_SYS_STATFS_H
  68. #include <sys/statfs.h>
  69. #endif
  70. #ifdef HAVE_SYS_SYSCTL_H
  71. #include <sys/sysctl.h>
  72. #endif
  73. #ifdef HAVE_SYS_EVENT_H
  74. #include <sys/event.h>
  75. #undef LIST_INIT
  76. #undef LIST_ENTRY
  77. #endif
  78. #ifdef HAVE_STDINT_H
  79. #include <stdint.h>
  80. #endif
  81. #include <sys/stat.h>
  82. #include <sys/time.h>
  83. #ifdef MAJOR_IN_MKDEV
  84. #include <sys/mkdev.h>
  85. #elif defined(MAJOR_IN_SYSMACROS)
  86. #include <sys/sysmacros.h>
  87. #endif
  88. #include <sys/types.h>
  89. #include <unistd.h>
  90. #ifdef HAVE_SYS_SYSCALL_H
  91. #include <sys/syscall.h>
  92. #endif
  93. #include "ntstatus.h"
  94. #define WIN32_NO_STATUS
  95. #include "object.h"
  96. #include "file.h"
  97. #include "handle.h"
  98. #include "process.h"
  99. #include "request.h"
  100. #include "esync.h"
  101. #include "winternl.h"
  102. #include "winioctl.h"
  103. #if !defined(O_SYMLINK) && defined(O_PATH)
  104. # define O_SYMLINK (O_NOFOLLOW | O_PATH)
  105. #endif
  106. #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
  107. # include <sys/epoll.h>
  108. # define USE_EPOLL
  109. #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
  110. # define USE_EPOLL
  111. # define EPOLLIN POLLIN
  112. # define EPOLLOUT POLLOUT
  113. # define EPOLLERR POLLERR
  114. # define EPOLLHUP POLLHUP
  115. # define EPOLL_CTL_ADD 1
  116. # define EPOLL_CTL_DEL 2
  117. # define EPOLL_CTL_MOD 3
  118. typedef union epoll_data
  119. {
  120. void *ptr;
  121. int fd;
  122. uint32_t u32;
  123. uint64_t u64;
  124. } epoll_data_t;
  125. struct epoll_event
  126. {
  127. uint32_t events;
  128. epoll_data_t data;
  129. };
  130. static inline int epoll_create( int size )
  131. {
  132. return syscall( 254 /*NR_epoll_create*/, size );
  133. }
  134. static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
  135. {
  136. return syscall( 255 /*NR_epoll_ctl*/, epfd, op, fd, event );
  137. }
  138. static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
  139. {
  140. return syscall( 256 /*NR_epoll_wait*/, epfd, events, maxevents, timeout );
  141. }
  142. #endif /* linux && __i386__ && HAVE_STDINT_H */
  143. #if defined(HAVE_PORT_H) && defined(HAVE_PORT_CREATE)
  144. # include <port.h>
  145. # define USE_EVENT_PORTS
  146. #endif /* HAVE_PORT_H && HAVE_PORT_CREATE */
  147. /* Because of the stupid Posix locking semantics, we need to keep
  148. * track of all file descriptors referencing a given file, and not
  149. * close a single one until all the locks are gone (sigh).
  150. */
  151. /* file descriptor object */
  152. /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
  153. struct closed_fd
  154. {
  155. struct list entry; /* entry in inode closed list */
  156. int unix_fd; /* the unix file descriptor */
  157. int unlink; /* whether to unlink on close: -1 - implicit FILE_DELETE_ON_CLOSE, 1 - explicit disposition */
  158. char *unix_name; /* name to unlink on close, points to parent fd unix_name */
  159. };
  160. struct fd
  161. {
  162. struct object obj; /* object header */
  163. const struct fd_ops *fd_ops; /* file descriptor operations */
  164. struct inode *inode; /* inode that this fd belongs to */
  165. struct list inode_entry; /* entry in inode fd list */
  166. struct closed_fd *closed; /* structure to store the unix fd at destroy time */
  167. struct object *user; /* object using this file descriptor */
  168. struct list locks; /* list of locks on this fd */
  169. unsigned int access; /* file access (FILE_READ_DATA etc.) */
  170. unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
  171. unsigned int sharing; /* file sharing mode */
  172. char *unix_name; /* unix file name */
  173. int unix_fd; /* unix file descriptor */
  174. unsigned int no_fd_status;/* status to return when unix_fd is -1 */
  175. unsigned int cacheable :1;/* can the fd be cached on the client side? */
  176. unsigned int signaled :1; /* is the fd signaled? */
  177. unsigned int fs_locks :1; /* can we use filesystem locks for this fd? */
  178. int poll_index; /* index of fd in poll array */
  179. struct async_queue read_q; /* async readers of this fd */
  180. struct async_queue write_q; /* async writers of this fd */
  181. struct async_queue wait_q; /* other async waiters of this fd */
  182. struct completion *completion; /* completion object attached to this fd */
  183. apc_param_t comp_key; /* completion key to set in completion events */
  184. unsigned int comp_flags; /* completion flags */
  185. int esync_fd; /* esync file descriptor */
  186. };
  187. static void fd_dump( struct object *obj, int verbose );
  188. static void fd_destroy( struct object *obj );
  189. static const struct object_ops fd_ops =
  190. {
  191. sizeof(struct fd), /* size */
  192. fd_dump, /* dump */
  193. no_get_type, /* get_type */
  194. no_add_queue, /* add_queue */
  195. NULL, /* remove_queue */
  196. NULL, /* signaled */
  197. NULL, /* get_esync_fd */
  198. NULL, /* satisfied */
  199. no_signal, /* signal */
  200. no_get_fd, /* get_fd */
  201. no_map_access, /* map_access */
  202. default_get_sd, /* get_sd */
  203. default_set_sd, /* set_sd */
  204. no_lookup_name, /* lookup_name */
  205. no_link_name, /* link_name */
  206. NULL, /* unlink_name */
  207. no_open_file, /* open_file */
  208. no_kernel_obj_list, /* get_kernel_obj_list */
  209. no_alloc_handle, /* alloc_handle */
  210. no_close_handle, /* close_handle */
  211. fd_destroy /* destroy */
  212. };
  213. /* device object */
  214. #define DEVICE_HASH_SIZE 7
  215. #define INODE_HASH_SIZE 17
  216. struct device
  217. {
  218. struct object obj; /* object header */
  219. struct list entry; /* entry in device hash list */
  220. dev_t dev; /* device number */
  221. int removable; /* removable device? (or -1 if unknown) */
  222. struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
  223. };
  224. static void device_dump( struct object *obj, int verbose );
  225. static void device_destroy( struct object *obj );
  226. static const struct object_ops device_ops =
  227. {
  228. sizeof(struct device), /* size */
  229. device_dump, /* dump */
  230. no_get_type, /* get_type */
  231. no_add_queue, /* add_queue */
  232. NULL, /* remove_queue */
  233. NULL, /* signaled */
  234. NULL, /* get_esync_fd */
  235. NULL, /* satisfied */
  236. no_signal, /* signal */
  237. no_get_fd, /* get_fd */
  238. no_map_access, /* map_access */
  239. default_get_sd, /* get_sd */
  240. default_set_sd, /* set_sd */
  241. no_lookup_name, /* lookup_name */
  242. no_link_name, /* link_name */
  243. NULL, /* unlink_name */
  244. no_open_file, /* open_file */
  245. no_kernel_obj_list, /* get_kernel_obj_list */
  246. no_alloc_handle, /* alloc_handle */
  247. no_close_handle, /* close_handle */
  248. device_destroy /* destroy */
  249. };
  250. /* inode object */
  251. struct inode
  252. {
  253. struct object obj; /* object header */
  254. struct list entry; /* inode hash list entry */
  255. struct device *device; /* device containing this inode */
  256. ino_t ino; /* inode number */
  257. struct list open; /* list of open file descriptors */
  258. struct list locks; /* list of file locks */
  259. struct list closed; /* list of file descriptors to close at destroy time */
  260. };
  261. static void inode_dump( struct object *obj, int verbose );
  262. static void inode_destroy( struct object *obj );
  263. static const struct object_ops inode_ops =
  264. {
  265. sizeof(struct inode), /* size */
  266. inode_dump, /* dump */
  267. no_get_type, /* get_type */
  268. no_add_queue, /* add_queue */
  269. NULL, /* remove_queue */
  270. NULL, /* signaled */
  271. NULL, /* get_esync_fd */
  272. NULL, /* satisfied */
  273. no_signal, /* signal */
  274. no_get_fd, /* get_fd */
  275. no_map_access, /* map_access */
  276. default_get_sd, /* get_sd */
  277. default_set_sd, /* set_sd */
  278. no_lookup_name, /* lookup_name */
  279. no_link_name, /* link_name */
  280. NULL, /* unlink_name */
  281. no_open_file, /* open_file */
  282. no_kernel_obj_list, /* get_kernel_obj_list */
  283. no_alloc_handle, /* alloc_handle */
  284. no_close_handle, /* close_handle */
  285. inode_destroy /* destroy */
  286. };
  287. /* file lock object */
  288. struct file_lock
  289. {
  290. struct object obj; /* object header */
  291. struct fd *fd; /* fd owning this lock */
  292. struct list fd_entry; /* entry in list of locks on a given fd */
  293. struct list inode_entry; /* entry in inode list of locks */
  294. int shared; /* shared lock? */
  295. file_pos_t start; /* locked region is interval [start;end) */
  296. file_pos_t end;
  297. struct process *process; /* process owning this lock */
  298. struct list proc_entry; /* entry in list of locks owned by the process */
  299. };
  300. static void file_lock_dump( struct object *obj, int verbose );
  301. static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry );
  302. static const struct object_ops file_lock_ops =
  303. {
  304. sizeof(struct file_lock), /* size */
  305. file_lock_dump, /* dump */
  306. no_get_type, /* get_type */
  307. add_queue, /* add_queue */
  308. remove_queue, /* remove_queue */
  309. file_lock_signaled, /* signaled */
  310. NULL, /* get_esync_fd */
  311. no_satisfied, /* satisfied */
  312. no_signal, /* signal */
  313. no_get_fd, /* get_fd */
  314. no_map_access, /* map_access */
  315. default_get_sd, /* get_sd */
  316. default_set_sd, /* set_sd */
  317. no_lookup_name, /* lookup_name */
  318. no_link_name, /* link_name */
  319. NULL, /* unlink_name */
  320. no_open_file, /* open_file */
  321. no_kernel_obj_list, /* get_kernel_obj_list */
  322. no_alloc_handle, /* alloc_handle */
  323. no_close_handle, /* close_handle */
  324. no_destroy /* destroy */
  325. };
  326. #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
  327. #define FILE_POS_T_MAX (~(file_pos_t)0)
  328. static file_pos_t max_unix_offset = OFF_T_MAX;
  329. #define DUMP_LONG_LONG(val) do { \
  330. if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
  331. fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
  332. else \
  333. fprintf( stderr, "%lx", (unsigned long)(val) ); \
  334. } while (0)
  335. /****************************************************************/
  336. /* timeouts support */
  337. struct timeout_user
  338. {
  339. struct list entry; /* entry in sorted timeout list */
  340. timeout_t when; /* timeout expiry (absolute time) */
  341. timeout_callback callback; /* callback function */
  342. void *private; /* callback private data */
  343. };
  344. static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */
  345. timeout_t current_time;
  346. static inline void set_current_time(void)
  347. {
  348. static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
  349. struct timeval now;
  350. gettimeofday( &now, NULL );
  351. current_time = (timeout_t)now.tv_sec * TICKS_PER_SEC + now.tv_usec * 10 + ticks_1601_to_1970;
  352. }
  353. /* add a timeout user */
  354. struct timeout_user *add_timeout_user( timeout_t when, timeout_callback func, void *private )
  355. {
  356. struct timeout_user *user;
  357. struct list *ptr;
  358. if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
  359. user->when = (when > 0) ? when : current_time - when;
  360. user->callback = func;
  361. user->private = private;
  362. /* Now insert it in the linked list */
  363. LIST_FOR_EACH( ptr, &timeout_list )
  364. {
  365. struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
  366. if (timeout->when >= user->when) break;
  367. }
  368. list_add_before( ptr, &user->entry );
  369. return user;
  370. }
  371. /* remove a timeout user */
  372. void remove_timeout_user( struct timeout_user *user )
  373. {
  374. list_remove( &user->entry );
  375. free( user );
  376. }
  377. /* return a text description of a timeout for debugging purposes */
  378. const char *get_timeout_str( timeout_t timeout )
  379. {
  380. static char buffer[64];
  381. long secs, nsecs;
  382. if (!timeout) return "0";
  383. if (timeout == TIMEOUT_INFINITE) return "infinite";
  384. if (timeout < 0) /* relative */
  385. {
  386. secs = -timeout / TICKS_PER_SEC;
  387. nsecs = -timeout % TICKS_PER_SEC;
  388. sprintf( buffer, "+%ld.%07ld", secs, nsecs );
  389. }
  390. else /* absolute */
  391. {
  392. secs = (timeout - current_time) / TICKS_PER_SEC;
  393. nsecs = (timeout - current_time) % TICKS_PER_SEC;
  394. if (nsecs < 0)
  395. {
  396. nsecs += TICKS_PER_SEC;
  397. secs--;
  398. }
  399. if (secs >= 0)
  400. sprintf( buffer, "%x%08x (+%ld.%07ld)",
  401. (unsigned int)(timeout >> 32), (unsigned int)timeout, secs, nsecs );
  402. else
  403. sprintf( buffer, "%x%08x (-%ld.%07ld)",
  404. (unsigned int)(timeout >> 32), (unsigned int)timeout,
  405. -(secs + 1), TICKS_PER_SEC - nsecs );
  406. }
  407. return buffer;
  408. }
  409. /****************************************************************/
  410. /* poll support */
  411. static struct fd **poll_users; /* users array */
  412. static struct pollfd *pollfd; /* poll fd array */
  413. static int nb_users; /* count of array entries actually in use */
  414. static int active_users; /* current number of active users */
  415. static int allocated_users; /* count of allocated entries in the array */
  416. static struct fd **freelist; /* list of free entries in the array */
  417. static int get_next_timeout(void);
  418. static inline void fd_poll_event( struct fd *fd, int event )
  419. {
  420. fd->fd_ops->poll_event( fd, event );
  421. }
  422. #ifdef USE_EPOLL
  423. static int epoll_fd = -1;
  424. static inline void init_epoll(void)
  425. {
  426. epoll_fd = epoll_create( 128 );
  427. }
  428. /* set the events that epoll waits for on this fd; helper for set_fd_events */
  429. static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
  430. {
  431. struct epoll_event ev;
  432. int ctl;
  433. if (epoll_fd == -1) return;
  434. if (events == -1) /* stop waiting on this fd completely */
  435. {
  436. if (pollfd[user].fd == -1) return; /* already removed */
  437. ctl = EPOLL_CTL_DEL;
  438. }
  439. else if (pollfd[user].fd == -1)
  440. {
  441. if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
  442. ctl = EPOLL_CTL_ADD;
  443. }
  444. else
  445. {
  446. if (pollfd[user].events == events) return; /* nothing to do */
  447. ctl = EPOLL_CTL_MOD;
  448. }
  449. ev.events = events;
  450. memset(&ev.data, 0, sizeof(ev.data));
  451. ev.data.u32 = user;
  452. if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
  453. {
  454. if (errno == ENOMEM) /* not enough memory, give up on epoll */
  455. {
  456. close( epoll_fd );
  457. epoll_fd = -1;
  458. }
  459. else perror( "epoll_ctl" ); /* should not happen */
  460. }
  461. }
  462. static inline void remove_epoll_user( struct fd *fd, int user )
  463. {
  464. if (epoll_fd == -1) return;
  465. if (pollfd[user].fd != -1)
  466. {
  467. struct epoll_event dummy;
  468. epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
  469. }
  470. }
  471. static inline void main_loop_epoll(void)
  472. {
  473. int i, ret, timeout;
  474. struct epoll_event events[128];
  475. assert( POLLIN == EPOLLIN );
  476. assert( POLLOUT == EPOLLOUT );
  477. assert( POLLERR == EPOLLERR );
  478. assert( POLLHUP == EPOLLHUP );
  479. if (epoll_fd == -1) return;
  480. while (active_users)
  481. {
  482. timeout = get_next_timeout();
  483. if (!active_users) break; /* last user removed by a timeout */
  484. if (epoll_fd == -1) break; /* an error occurred with epoll */
  485. ret = epoll_wait( epoll_fd, events, ARRAY_SIZE( events ), timeout );
  486. set_current_time();
  487. /* put the events into the pollfd array first, like poll does */
  488. for (i = 0; i < ret; i++)
  489. {
  490. int user = events[i].data.u32;
  491. pollfd[user].revents = events[i].events;
  492. }
  493. /* read events from the pollfd array, as set_fd_events may modify them */
  494. for (i = 0; i < ret; i++)
  495. {
  496. int user = events[i].data.u32;
  497. if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
  498. }
  499. }
  500. }
  501. #elif defined(HAVE_KQUEUE)
  502. static int kqueue_fd = -1;
  503. static inline void init_epoll(void)
  504. {
  505. #ifdef __APPLE__ /* kqueue support is broken in Mac OS < 10.5 */
  506. int mib[2];
  507. char release[32];
  508. size_t len = sizeof(release);
  509. mib[0] = CTL_KERN;
  510. mib[1] = KERN_OSRELEASE;
  511. if (sysctl( mib, 2, release, &len, NULL, 0 ) == -1) return;
  512. if (atoi(release) < 9) return;
  513. #endif
  514. kqueue_fd = kqueue();
  515. }
  516. static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
  517. {
  518. struct kevent ev[2];
  519. if (kqueue_fd == -1) return;
  520. EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, 0, NOTE_LOWAT, 1, (void *)(long)user );
  521. EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, 0, NOTE_LOWAT, 1, (void *)(long)user );
  522. if (events == -1) /* stop waiting on this fd completely */
  523. {
  524. if (pollfd[user].fd == -1) return; /* already removed */
  525. ev[0].flags |= EV_DELETE;
  526. ev[1].flags |= EV_DELETE;
  527. }
  528. else if (pollfd[user].fd == -1)
  529. {
  530. if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
  531. ev[0].flags |= EV_ADD | ((events & POLLIN) ? EV_ENABLE : EV_DISABLE);
  532. ev[1].flags |= EV_ADD | ((events & POLLOUT) ? EV_ENABLE : EV_DISABLE);
  533. }
  534. else
  535. {
  536. if (pollfd[user].events == events) return; /* nothing to do */
  537. ev[0].flags |= (events & POLLIN) ? EV_ENABLE : EV_DISABLE;
  538. ev[1].flags |= (events & POLLOUT) ? EV_ENABLE : EV_DISABLE;
  539. }
  540. if (kevent( kqueue_fd, ev, 2, NULL, 0, NULL ) == -1)
  541. {
  542. if (errno == ENOMEM) /* not enough memory, give up on kqueue */
  543. {
  544. close( kqueue_fd );
  545. kqueue_fd = -1;
  546. }
  547. else perror( "kevent" ); /* should not happen */
  548. }
  549. }
  550. static inline void remove_epoll_user( struct fd *fd, int user )
  551. {
  552. if (kqueue_fd == -1) return;
  553. if (pollfd[user].fd != -1)
  554. {
  555. struct kevent ev[2];
  556. EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, EV_DELETE, 0, 0, 0 );
  557. EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0 );
  558. kevent( kqueue_fd, ev, 2, NULL, 0, NULL );
  559. }
  560. }
  561. static inline void main_loop_epoll(void)
  562. {
  563. int i, ret, timeout;
  564. struct kevent events[128];
  565. if (kqueue_fd == -1) return;
  566. while (active_users)
  567. {
  568. timeout = get_next_timeout();
  569. if (!active_users) break; /* last user removed by a timeout */
  570. if (kqueue_fd == -1) break; /* an error occurred with kqueue */
  571. if (timeout != -1)
  572. {
  573. struct timespec ts;
  574. ts.tv_sec = timeout / 1000;
  575. ts.tv_nsec = (timeout % 1000) * 1000000;
  576. ret = kevent( kqueue_fd, NULL, 0, events, ARRAY_SIZE( events ), &ts );
  577. }
  578. else ret = kevent( kqueue_fd, NULL, 0, events, ARRAY_SIZE( events ), NULL );
  579. set_current_time();
  580. /* put the events into the pollfd array first, like poll does */
  581. for (i = 0; i < ret; i++)
  582. {
  583. long user = (long)events[i].udata;
  584. pollfd[user].revents = 0;
  585. }
  586. for (i = 0; i < ret; i++)
  587. {
  588. long user = (long)events[i].udata;
  589. if (events[i].filter == EVFILT_READ) pollfd[user].revents |= POLLIN;
  590. else if (events[i].filter == EVFILT_WRITE) pollfd[user].revents |= POLLOUT;
  591. if (events[i].flags & EV_EOF) pollfd[user].revents |= POLLHUP;
  592. if (events[i].flags & EV_ERROR) pollfd[user].revents |= POLLERR;
  593. }
  594. /* read events from the pollfd array, as set_fd_events may modify them */
  595. for (i = 0; i < ret; i++)
  596. {
  597. long user = (long)events[i].udata;
  598. if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
  599. pollfd[user].revents = 0;
  600. }
  601. }
  602. }
  603. #elif defined(USE_EVENT_PORTS)
  604. static int port_fd = -1;
  605. static inline void init_epoll(void)
  606. {
  607. port_fd = port_create();
  608. }
  609. static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
  610. {
  611. int ret;
  612. if (port_fd == -1) return;
  613. if (events == -1) /* stop waiting on this fd completely */
  614. {
  615. if (pollfd[user].fd == -1) return; /* already removed */
  616. port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
  617. }
  618. else if (pollfd[user].fd == -1)
  619. {
  620. if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
  621. ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
  622. }
  623. else
  624. {
  625. if (pollfd[user].events == events) return; /* nothing to do */
  626. ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
  627. }
  628. if (ret == -1)
  629. {
  630. if (errno == ENOMEM) /* not enough memory, give up on port_associate */
  631. {
  632. close( port_fd );
  633. port_fd = -1;
  634. }
  635. else perror( "port_associate" ); /* should not happen */
  636. }
  637. }
  638. static inline void remove_epoll_user( struct fd *fd, int user )
  639. {
  640. if (port_fd == -1) return;
  641. if (pollfd[user].fd != -1)
  642. {
  643. port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
  644. }
  645. }
  646. static inline void main_loop_epoll(void)
  647. {
  648. int i, nget, ret, timeout;
  649. port_event_t events[128];
  650. if (port_fd == -1) return;
  651. while (active_users)
  652. {
  653. timeout = get_next_timeout();
  654. nget = 1;
  655. if (!active_users) break; /* last user removed by a timeout */
  656. if (port_fd == -1) break; /* an error occurred with event completion */
  657. if (timeout != -1)
  658. {
  659. struct timespec ts;
  660. ts.tv_sec = timeout / 1000;
  661. ts.tv_nsec = (timeout % 1000) * 1000000;
  662. ret = port_getn( port_fd, events, ARRAY_SIZE( events ), &nget, &ts );
  663. }
  664. else ret = port_getn( port_fd, events, ARRAY_SIZE( events ), &nget, NULL );
  665. if (ret == -1) break; /* an error occurred with event completion */
  666. set_current_time();
  667. /* put the events into the pollfd array first, like poll does */
  668. for (i = 0; i < nget; i++)
  669. {
  670. long user = (long)events[i].portev_user;
  671. pollfd[user].revents = events[i].portev_events;
  672. }
  673. /* read events from the pollfd array, as set_fd_events may modify them */
  674. for (i = 0; i < nget; i++)
  675. {
  676. long user = (long)events[i].portev_user;
  677. if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
  678. /* if we are still interested, reassociate the fd */
  679. if (pollfd[user].fd != -1) {
  680. port_associate( port_fd, PORT_SOURCE_FD, pollfd[user].fd, pollfd[user].events, (void *)user );
  681. }
  682. }
  683. }
  684. }
  685. #else /* HAVE_KQUEUE */
  686. static inline void init_epoll(void) { }
  687. static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) { }
  688. static inline void remove_epoll_user( struct fd *fd, int user ) { }
  689. static inline void main_loop_epoll(void) { }
  690. #endif /* USE_EPOLL */
  691. /* add a user in the poll array and return its index, or -1 on failure */
  692. static int add_poll_user( struct fd *fd )
  693. {
  694. int ret;
  695. if (freelist)
  696. {
  697. ret = freelist - poll_users;
  698. freelist = (struct fd **)poll_users[ret];
  699. }
  700. else
  701. {
  702. if (nb_users == allocated_users)
  703. {
  704. struct fd **newusers;
  705. struct pollfd *newpoll;
  706. int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
  707. if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
  708. if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
  709. {
  710. if (allocated_users)
  711. poll_users = newusers;
  712. else
  713. free( newusers );
  714. return -1;
  715. }
  716. poll_users = newusers;
  717. pollfd = newpoll;
  718. if (!allocated_users) init_epoll();
  719. allocated_users = new_count;
  720. }
  721. ret = nb_users++;
  722. }
  723. pollfd[ret].fd = -1;
  724. pollfd[ret].events = 0;
  725. pollfd[ret].revents = 0;
  726. poll_users[ret] = fd;
  727. active_users++;
  728. return ret;
  729. }
  730. /* remove a user from the poll list */
  731. static void remove_poll_user( struct fd *fd, int user )
  732. {
  733. assert( user >= 0 );
  734. assert( poll_users[user] == fd );
  735. remove_epoll_user( fd, user );
  736. pollfd[user].fd = -1;
  737. pollfd[user].events = 0;
  738. pollfd[user].revents = 0;
  739. poll_users[user] = (struct fd *)freelist;
  740. freelist = &poll_users[user];
  741. active_users--;
  742. }
  743. /* process pending timeouts and return the time until the next timeout, in milliseconds */
  744. static int get_next_timeout(void)
  745. {
  746. if (!list_empty( &timeout_list ))
  747. {
  748. struct list expired_list, *ptr;
  749. /* first remove all expired timers from the list */
  750. list_init( &expired_list );
  751. while ((ptr = list_head( &timeout_list )) != NULL)
  752. {
  753. struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
  754. if (timeout->when <= current_time)
  755. {
  756. list_remove( &timeout->entry );
  757. list_add_tail( &expired_list, &timeout->entry );
  758. }
  759. else break;
  760. }
  761. /* now call the callback for all the removed timers */
  762. while ((ptr = list_head( &expired_list )) != NULL)
  763. {
  764. struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
  765. list_remove( &timeout->entry );
  766. timeout->callback( timeout->private );
  767. free( timeout );
  768. }
  769. if ((ptr = list_head( &timeout_list )) != NULL)
  770. {
  771. struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
  772. int diff = (timeout->when - current_time + 9999) / 10000;
  773. if (diff < 0) diff = 0;
  774. return diff;
  775. }
  776. }
  777. return -1; /* no pending timeouts */
  778. }
  779. /* server main poll() loop */
  780. void main_loop(void)
  781. {
  782. int i, ret, timeout;
  783. set_current_time();
  784. server_start_time = current_time;
  785. main_loop_epoll();
  786. /* fall through to normal poll loop */
  787. while (active_users)
  788. {
  789. timeout = get_next_timeout();
  790. if (!active_users) break; /* last user removed by a timeout */
  791. ret = poll( pollfd, nb_users, timeout );
  792. set_current_time();
  793. if (ret > 0)
  794. {
  795. for (i = 0; i < nb_users; i++)
  796. {
  797. if (pollfd[i].revents)
  798. {
  799. fd_poll_event( poll_users[i], pollfd[i].revents );
  800. if (!--ret) break;
  801. }
  802. }
  803. }
  804. }
  805. }
  806. /****************************************************************/
  807. /* device functions */
  808. static struct list device_hash[DEVICE_HASH_SIZE];
  809. static int is_device_removable( dev_t dev, int unix_fd )
  810. {
  811. #if defined(linux) && defined(HAVE_FSTATFS)
  812. struct statfs stfs;
  813. /* check for floppy disk */
  814. if (major(dev) == FLOPPY_MAJOR) return 1;
  815. if (fstatfs( unix_fd, &stfs ) == -1) return 0;
  816. return (stfs.f_type == 0x9660 || /* iso9660 */
  817. stfs.f_type == 0x9fa1 || /* supermount */
  818. stfs.f_type == 0x15013346); /* udf */
  819. #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
  820. struct statfs stfs;
  821. if (fstatfs( unix_fd, &stfs ) == -1) return 0;
  822. return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
  823. #elif defined(__NetBSD__)
  824. struct statvfs stfs;
  825. if (fstatvfs( unix_fd, &stfs ) == -1) return 0;
  826. return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
  827. #elif defined(sun)
  828. # include <sys/dkio.h>
  829. # include <sys/vtoc.h>
  830. struct dk_cinfo dkinf;
  831. if (ioctl( unix_fd, DKIOCINFO, &dkinf ) == -1) return 0;
  832. return (dkinf.dki_ctype == DKC_CDROM ||
  833. dkinf.dki_ctype == DKC_NCRFLOPPY ||
  834. dkinf.dki_ctype == DKC_SMSFLOPPY ||
  835. dkinf.dki_ctype == DKC_INTEL82072 ||
  836. dkinf.dki_ctype == DKC_INTEL82077);
  837. #else
  838. return 0;
  839. #endif
  840. }
  841. /* retrieve the device object for a given fd, creating it if needed */
  842. static struct device *get_device( dev_t dev, int unix_fd )
  843. {
  844. struct device *device;
  845. unsigned int i, hash = dev % DEVICE_HASH_SIZE;
  846. if (device_hash[hash].next)
  847. {
  848. LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry )
  849. if (device->dev == dev) return (struct device *)grab_object( device );
  850. }
  851. else list_init( &device_hash[hash] );
  852. /* not found, create it */
  853. if (unix_fd == -1) return NULL;
  854. if ((device = alloc_object( &device_ops )))
  855. {
  856. device->dev = dev;
  857. device->removable = is_device_removable( dev, unix_fd );
  858. for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
  859. list_add_head( &device_hash[hash], &device->entry );
  860. }
  861. return device;
  862. }
  863. static void device_dump( struct object *obj, int verbose )
  864. {
  865. struct device *device = (struct device *)obj;
  866. fprintf( stderr, "Device dev=" );
  867. DUMP_LONG_LONG( device->dev );
  868. fprintf( stderr, "\n" );
  869. }
  870. static void device_destroy( struct object *obj )
  871. {
  872. struct device *device = (struct device *)obj;
  873. unsigned int i;
  874. for (i = 0; i < INODE_HASH_SIZE; i++)
  875. assert( list_empty(&device->inode_hash[i]) );
  876. list_remove( &device->entry ); /* remove it from the hash table */
  877. }
  878. /****************************************************************/
  879. /* inode functions */
  880. /* close all pending file descriptors in the closed list */
  881. static void inode_close_pending( struct inode *inode, int keep_unlinks )
  882. {
  883. struct list *ptr = list_head( &inode->closed );
  884. while (ptr)
  885. {
  886. struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
  887. struct list *next = list_next( &inode->closed, ptr );
  888. if (fd->unix_fd != -1)
  889. {
  890. close( fd->unix_fd );
  891. fd->unix_fd = -1;
  892. }
  893. if (!keep_unlinks || !fd->unlink) /* get rid of it unless there's an unlink pending on that file */
  894. {
  895. list_remove( ptr );
  896. free( fd->unix_name );
  897. free( fd );
  898. }
  899. ptr = next;
  900. }
  901. }
  902. static void inode_dump( struct object *obj, int verbose )
  903. {
  904. struct inode *inode = (struct inode *)obj;
  905. fprintf( stderr, "Inode device=%p ino=", inode->device );
  906. DUMP_LONG_LONG( inode->ino );
  907. fprintf( stderr, "\n" );
  908. }
  909. static void inode_destroy( struct object *obj )
  910. {
  911. struct inode *inode = (struct inode *)obj;
  912. struct list *ptr;
  913. assert( list_empty(&inode->open) );
  914. assert( list_empty(&inode->locks) );
  915. list_remove( &inode->entry );
  916. while ((ptr = list_head( &inode->closed )))
  917. {
  918. struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
  919. list_remove( ptr );
  920. if (fd->unix_fd != -1) close( fd->unix_fd );
  921. if (fd->unlink)
  922. {
  923. /* make sure it is still the same file */
  924. struct stat st;
  925. if (!lstat( fd->unix_name, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino)
  926. {
  927. if (S_ISDIR(st.st_mode)) rmdir( fd->unix_name );
  928. else unlink( fd->unix_name );
  929. }
  930. }
  931. free( fd->unix_name );
  932. free( fd );
  933. }
  934. release_object( inode->device );
  935. }
  936. /* retrieve the inode object for a given fd, creating it if needed */
  937. static struct inode *get_inode( dev_t dev, ino_t ino, int unix_fd )
  938. {
  939. struct device *device;
  940. struct inode *inode;
  941. unsigned int hash = ino % INODE_HASH_SIZE;
  942. if (!(device = get_device( dev, unix_fd ))) return NULL;
  943. LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
  944. {
  945. if (inode->ino == ino)
  946. {
  947. release_object( device );
  948. return (struct inode *)grab_object( inode );
  949. }
  950. }
  951. /* not found, create it */
  952. if ((inode = alloc_object( &inode_ops )))
  953. {
  954. inode->device = device;
  955. inode->ino = ino;
  956. list_init( &inode->open );
  957. list_init( &inode->locks );
  958. list_init( &inode->closed );
  959. list_add_head( &device->inode_hash[hash], &inode->entry );
  960. }
  961. else release_object( device );
  962. return inode;
  963. }
  964. /* add fd to the inode list of file descriptors to close */
  965. static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
  966. {
  967. if (!list_empty( &inode->locks ))
  968. {
  969. list_add_head( &inode->closed, &fd->entry );
  970. }
  971. else if (fd->unlink) /* close the fd but keep the structure around for unlink */
  972. {
  973. if (fd->unix_fd != -1) close( fd->unix_fd );
  974. fd->unix_fd = -1;
  975. list_add_head( &inode->closed, &fd->entry );
  976. }
  977. else /* no locks on this inode and no unlink, get rid of the fd */
  978. {
  979. if (fd->unix_fd != -1) close( fd->unix_fd );
  980. free( fd->unix_name );
  981. free( fd );
  982. }
  983. }
  984. /****************************************************************/
  985. /* file lock functions */
  986. static void file_lock_dump( struct object *obj, int verbose )
  987. {
  988. struct file_lock *lock = (struct file_lock *)obj;
  989. fprintf( stderr, "Lock %s fd=%p proc=%p start=",
  990. lock->shared ? "shared" : "excl", lock->fd, lock->process );
  991. DUMP_LONG_LONG( lock->start );
  992. fprintf( stderr, " end=" );
  993. DUMP_LONG_LONG( lock->end );
  994. fprintf( stderr, "\n" );
  995. }
  996. static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry )
  997. {
  998. struct file_lock *lock = (struct file_lock *)obj;
  999. /* lock is signaled if it has lost its owner */
  1000. return !lock->process;
  1001. }
  1002. /* set (or remove) a Unix lock if possible for the given range */
  1003. static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
  1004. {
  1005. struct flock fl;
  1006. if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
  1007. for (;;)
  1008. {
  1009. if (start == end) return 1; /* can't set zero-byte lock */
  1010. if (start > max_unix_offset) return 1; /* ignore it */
  1011. fl.l_type = type;
  1012. fl.l_whence = SEEK_SET;
  1013. fl.l_start = start;
  1014. if (!end || end > max_unix_offset) fl.l_len = 0;
  1015. else fl.l_len = end - start;
  1016. if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
  1017. switch(errno)
  1018. {
  1019. case EACCES:
  1020. /* check whether locks work at all on this file system */
  1021. if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
  1022. {
  1023. set_error( STATUS_FILE_LOCK_CONFLICT );
  1024. return 0;
  1025. }
  1026. /* fall through */
  1027. case EIO:
  1028. case ENOLCK:
  1029. case ENOTSUP:
  1030. /* no locking on this fs, just ignore it */
  1031. fd->fs_locks = 0;
  1032. return 1;
  1033. case EAGAIN:
  1034. set_error( STATUS_FILE_LOCK_CONFLICT );
  1035. return 0;
  1036. case EBADF:
  1037. /* this can happen if we try to set a write lock on a read-only file */
  1038. /* try to at least grab a read lock */
  1039. if (fl.l_type == F_WRLCK)
  1040. {
  1041. type = F_RDLCK;
  1042. break; /* retry */
  1043. }
  1044. set_error( STATUS_ACCESS_DENIED );
  1045. return 0;
  1046. #ifdef EOVERFLOW
  1047. case EOVERFLOW:
  1048. #endif
  1049. case EINVAL:
  1050. /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
  1051. /* in that case we shrink the limit and retry */
  1052. if (max_unix_offset > INT_MAX)
  1053. {
  1054. max_unix_offset = INT_MAX;
  1055. break; /* retry */
  1056. }
  1057. /* fall through */
  1058. default:
  1059. file_set_error();
  1060. return 0;
  1061. }
  1062. }
  1063. }
  1064. /* check if interval [start;end) overlaps the lock */
  1065. static inline int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
  1066. {
  1067. if (lock->end && start >= lock->end) return 0;
  1068. if (end && lock->start >= end) return 0;
  1069. return 1;
  1070. }
  1071. /* remove Unix locks for all bytes in the specified area that are no longer locked */
  1072. static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
  1073. {
  1074. struct hole
  1075. {
  1076. struct hole *next;
  1077. struct hole *prev;
  1078. file_pos_t start;
  1079. file_pos_t end;
  1080. } *first, *cur, *next, *buffer;
  1081. struct list *ptr;
  1082. int count = 0;
  1083. if (!fd->inode) return;
  1084. if (!fd->fs_locks) return;
  1085. if (start == end || start > max_unix_offset) return;
  1086. if (!end || end > max_unix_offset) end = max_unix_offset + 1;
  1087. /* count the number of locks overlapping the specified area */
  1088. LIST_FOR_EACH( ptr, &fd->inode->locks )
  1089. {
  1090. struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
  1091. if (lock->start == lock->end) continue;
  1092. if (lock_overlaps( lock, start, end )) count++;
  1093. }
  1094. if (!count) /* no locks at all, we can unlock everything */
  1095. {
  1096. set_unix_lock( fd, start, end, F_UNLCK );
  1097. return;
  1098. }
  1099. /* allocate space for the list of holes */
  1100. /* max. number of holes is number of locks + 1 */
  1101. if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
  1102. first = buffer;
  1103. first->next = NULL;
  1104. first->prev = NULL;
  1105. first->start = start;
  1106. first->end = end;
  1107. next = first + 1;
  1108. /* build a sorted list of unlocked holes in the specified area */
  1109. LIST_FOR_EACH( ptr, &fd->inode->locks )
  1110. {
  1111. struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
  1112. if (lock->start == lock->end) continue;
  1113. if (!lock_overlaps( lock, start, end )) continue;
  1114. /* go through all the holes touched by this lock */
  1115. for (cur = first; cur; cur = cur->next)
  1116. {
  1117. if (cur->end <= lock->start) continue; /* hole is before start of lock */
  1118. if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
  1119. /* now we know that lock is overlapping hole */
  1120. if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
  1121. {
  1122. cur->start = lock->end;
  1123. if (cur->start && cur->start < cur->end) break; /* done with this lock */
  1124. /* now hole is empty, remove it */
  1125. if (cur->next) cur->next->prev = cur->prev;
  1126. if (cur->prev) cur->prev->next = cur->next;
  1127. else if (!(first = cur->next)) goto done; /* no more holes at all */
  1128. }
  1129. else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
  1130. {
  1131. cur->end = lock->start;
  1132. assert( cur->start < cur->end );
  1133. }
  1134. else /* lock is in the middle of hole, split hole in two */
  1135. {
  1136. next->prev = cur;
  1137. next->next = cur->next;
  1138. cur->next = next;
  1139. next->start = lock->end;
  1140. next->end = cur->end;
  1141. cur->end = lock->start;
  1142. assert( next->start < next->end );
  1143. assert( cur->end < next->start );
  1144. next++;
  1145. break; /* done with this lock */
  1146. }
  1147. }
  1148. }
  1149. /* clear Unix locks for all the holes */
  1150. for (cur = first; cur; cur = cur->next)
  1151. set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
  1152. done:
  1153. free( buffer );
  1154. }
  1155. /* create a new lock on a fd */
  1156. static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
  1157. {
  1158. struct file_lock *lock;
  1159. if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
  1160. lock->shared = shared;
  1161. lock->start = start;
  1162. lock->end = end;
  1163. lock->fd = fd;
  1164. lock->process = current->process;
  1165. /* now try to set a Unix lock */
  1166. if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
  1167. {
  1168. release_object( lock );
  1169. return NULL;
  1170. }
  1171. list_add_tail( &fd->locks, &lock->fd_entry );
  1172. list_add_tail( &fd->inode->locks, &lock->inode_entry );
  1173. list_add_tail( &lock->process->locks, &lock->proc_entry );
  1174. return lock;
  1175. }
  1176. /* remove an existing lock */
  1177. static void remove_lock( struct file_lock *lock, int remove_unix )
  1178. {
  1179. struct inode *inode = lock->fd->inode;
  1180. list_remove( &lock->fd_entry );
  1181. list_remove( &lock->inode_entry );
  1182. list_remove( &lock->proc_entry );
  1183. if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
  1184. if (list_empty( &inode->locks )) inode_close_pending( inode, 1 );
  1185. lock->process = NULL;
  1186. wake_up( &lock->obj, 0 );
  1187. release_object( lock );
  1188. }
  1189. /* remove all locks owned by a given process */
  1190. void remove_process_locks( struct process *process )
  1191. {
  1192. struct list *ptr;
  1193. while ((ptr = list_head( &process->locks )))
  1194. {
  1195. struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
  1196. remove_lock( lock, 1 ); /* this removes it from the list */
  1197. }
  1198. }
  1199. /* remove all locks on a given fd */
  1200. static void remove_fd_locks( struct fd *fd )
  1201. {
  1202. file_pos_t start = FILE_POS_T_MAX, end = 0;
  1203. struct list *ptr;
  1204. while ((ptr = list_head( &fd->locks )))
  1205. {
  1206. struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
  1207. if (lock->start < start) start = lock->start;
  1208. if (!lock->end || lock->end > end) end = lock->end - 1;
  1209. remove_lock( lock, 0 );
  1210. }
  1211. if (start < end) remove_unix_locks( fd, start, end + 1 );
  1212. }
  1213. /* add a lock on an fd */
  1214. /* returns handle to wait on */
  1215. obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
  1216. {
  1217. struct list *ptr;
  1218. file_pos_t end = start + count;
  1219. if (!fd->inode) /* not a regular file */
  1220. {
  1221. set_error( STATUS_INVALID_DEVICE_REQUEST );
  1222. return 0;
  1223. }
  1224. /* don't allow wrapping locks */
  1225. if (end && end < start)
  1226. {
  1227. set_error( STATUS_INVALID_PARAMETER );
  1228. return 0;
  1229. }
  1230. /* check if another lock on that file overlaps the area */
  1231. LIST_FOR_EACH( ptr, &fd->inode->locks )
  1232. {
  1233. struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
  1234. if (!lock_overlaps( lock, start, end )) continue;
  1235. if (shared && (lock->shared || lock->fd == fd)) continue;
  1236. /* found one */
  1237. if (!wait)
  1238. {
  1239. set_error( STATUS_FILE_LOCK_CONFLICT );
  1240. return 0;
  1241. }
  1242. set_error( STATUS_PENDING );
  1243. return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
  1244. }
  1245. /* not found, add it */
  1246. if (add_lock( fd, shared, start, end )) return 0;
  1247. if (get_error() == STATUS_FILE_LOCK_CONFLICT)
  1248. {
  1249. /* Unix lock conflict -> tell client to wait and retry */
  1250. if (wait) set_error( STATUS_PENDING );
  1251. }
  1252. return 0;
  1253. }
  1254. /* remove a lock on an fd */
  1255. void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
  1256. {
  1257. struct list *ptr;
  1258. file_pos_t end = start + count;
  1259. /* find an existing lock with the exact same parameters */
  1260. LIST_FOR_EACH( ptr, &fd->locks )
  1261. {
  1262. struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
  1263. if ((lock->start == start) && (lock->end == end))
  1264. {
  1265. remove_lock( lock, 1 );
  1266. return;
  1267. }
  1268. }
  1269. set_error( STATUS_FILE_LOCK_CONFLICT );
  1270. }
  1271. /****************************************************************/
  1272. /* file descriptor functions */
  1273. static void fd_dump( struct object *obj, int verbose )
  1274. {
  1275. struct fd *fd = (struct fd *)obj;
  1276. fprintf( stderr, "Fd unix_fd=%d user=%p options=%08x", fd->unix_fd, fd->user, fd->options );
  1277. if (fd->inode) fprintf( stderr, " inode=%p unlink=%d", fd->inode, fd->closed->unlink );
  1278. fprintf( stderr, "\n" );
  1279. }
  1280. static void fd_destroy( struct object *obj )
  1281. {
  1282. struct fd *fd = (struct fd *)obj;
  1283. free_async_queue( &fd->read_q );
  1284. free_async_queue( &fd->write_q );
  1285. free_async_queue( &fd->wait_q );
  1286. if (fd->completion) release_object( fd->completion );
  1287. remove_fd_locks( fd );
  1288. list_remove( &fd->inode_entry );
  1289. if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
  1290. if (fd->inode)
  1291. {
  1292. inode_add_closed_fd( fd->inode, fd->closed );
  1293. release_object( fd->inode );
  1294. }
  1295. else /* no inode, close it right away */
  1296. {
  1297. if (fd->unix_fd != -1) close( fd->unix_fd );
  1298. free( fd->unix_name );
  1299. }
  1300. if (do_esync())
  1301. close( fd->esync_fd );
  1302. }
  1303. /* check if the desired access is possible without violating */
  1304. /* the sharing mode of other opens of the same file */
  1305. static unsigned int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing,
  1306. unsigned int open_flags, unsigned int options )
  1307. {
  1308. /* only a few access bits are meaningful wrt sharing */
  1309. const unsigned int read_access = FILE_READ_DATA | FILE_EXECUTE;
  1310. const unsigned int write_access = FILE_WRITE_DATA | FILE_APPEND_DATA;
  1311. const unsigned int all_access = read_access | write_access | DELETE;
  1312. unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
  1313. unsigned int existing_access = 0;
  1314. struct list *ptr;
  1315. fd->access = access;
  1316. fd->sharing = sharing;
  1317. LIST_FOR_EACH( ptr, &fd->inode->open )
  1318. {
  1319. struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
  1320. if (fd_ptr != fd)
  1321. {
  1322. /* if access mode is 0, sharing mode is ignored */
  1323. if (fd_ptr->access & all_access) existing_sharing &= fd_ptr->sharing;
  1324. existing_access |= fd_ptr->access;
  1325. }
  1326. }
  1327. if (((access & read_access) && !(existing_sharing & FILE_SHARE_READ)) ||
  1328. ((access & write_access) && !(existing_sharing & FILE_SHARE_WRITE)) ||
  1329. ((access & DELETE) && !(existing_sharing & FILE_SHARE_DELETE)))
  1330. return STATUS_SHARING_VIOLATION;
  1331. if (((existing_access & FILE_MAPPING_WRITE) && !(sharing & FILE_SHARE_WRITE)) ||
  1332. ((existing_access & FILE_MAPPING_IMAGE) && (access & FILE_WRITE_DATA)))
  1333. return STATUS_SHARING_VIOLATION;
  1334. if ((existing_access & FILE_MAPPING_IMAGE) && (options & FILE_DELETE_ON_CLOSE))
  1335. return STATUS_CANNOT_DELETE;
  1336. if ((existing_access & FILE_MAPPING_ACCESS) && (open_flags & O_TRUNC))
  1337. return STATUS_USER_MAPPED_FILE;
  1338. if (!(access & all_access))
  1339. return 0; /* if access mode is 0, sharing mode is ignored (except for mappings) */
  1340. if (((existing_access & read_access) && !(sharing & FILE_SHARE_READ)) ||
  1341. ((existing_access & write_access) && !(sharing & FILE_SHARE_WRITE)) ||
  1342. ((existing_access & DELETE) && !(sharing & FILE_SHARE_DELETE)))
  1343. return STATUS_SHARING_VIOLATION;
  1344. return 0;
  1345. }
  1346. /* set the events that select waits for on this fd */
  1347. void set_fd_events( struct fd *fd, int events )
  1348. {
  1349. int user = fd->poll_index;
  1350. assert( poll_users[user] == fd );
  1351. set_fd_epoll_events( fd, user, events );
  1352. if (events == -1) /* stop waiting on this fd completely */
  1353. {
  1354. pollfd[user].fd = -1;
  1355. pollfd[user].events = POLLERR;
  1356. pollfd[user].revents = 0;
  1357. }
  1358. else if (pollfd[user].fd != -1 || !pollfd[user].events)
  1359. {
  1360. pollfd[user].fd = fd->unix_fd;
  1361. pollfd[user].events = events;
  1362. }
  1363. }
  1364. /* prepare an fd for unmounting its corresponding device */
  1365. static inline void unmount_fd( struct fd *fd )
  1366. {
  1367. assert( fd->inode );
  1368. async_wake_up( &fd->read_q, STATUS_VOLUME_DISMOUNTED );
  1369. async_wake_up( &fd->write_q, STATUS_VOLUME_DISMOUNTED );
  1370. if (fd->poll_index != -1) set_fd_events( fd, -1 );
  1371. if (fd->unix_fd != -1) close( fd->unix_fd );
  1372. fd->unix_fd = -1;
  1373. fd->no_fd_status = STATUS_VOLUME_DISMOUNTED;
  1374. fd->closed->unix_fd = -1;
  1375. fd->closed->unlink = 0;
  1376. /* stop using Unix locks on this fd (existing locks have been removed by close) */
  1377. fd->fs_locks = 0;
  1378. }
  1379. /* allocate an fd object, without setting the unix fd yet */
  1380. static struct fd *alloc_fd_object(void)
  1381. {
  1382. struct fd *fd = alloc_object( &fd_ops );
  1383. if (!fd) return NULL;
  1384. fd->fd_ops = NULL;
  1385. fd->user = NULL;
  1386. fd->inode = NULL;
  1387. fd->closed = NULL;
  1388. fd->access = 0;
  1389. fd->options = 0;
  1390. fd->sharing = 0;
  1391. fd->unix_fd = -1;
  1392. fd->unix_name = NULL;
  1393. fd->cacheable = 0;
  1394. fd->signaled = 1;
  1395. fd->fs_locks = 1;
  1396. fd->poll_index = -1;
  1397. fd->completion = NULL;
  1398. fd->comp_flags = 0;
  1399. fd->esync_fd = -1;
  1400. init_async_queue( &fd->read_q );
  1401. init_async_queue( &fd->write_q );
  1402. init_async_queue( &fd->wait_q );
  1403. list_init( &fd->inode_entry );
  1404. list_init( &fd->locks );
  1405. if (do_esync())
  1406. fd->esync_fd = esync_create_fd( 1, 0 );
  1407. if ((fd->poll_index = add_poll_user( fd )) == -1)
  1408. {
  1409. release_object( fd );
  1410. return NULL;
  1411. }
  1412. return fd;
  1413. }
  1414. /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
  1415. struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user, unsigned int options )
  1416. {
  1417. struct fd *fd = alloc_object( &fd_ops );
  1418. if (!fd) return NULL;
  1419. fd->fd_ops = fd_user_ops;
  1420. fd->user = user;
  1421. fd->inode = NULL;
  1422. fd->closed = NULL;
  1423. fd->access = 0;
  1424. fd->options = options;
  1425. fd->sharing = 0;
  1426. fd->unix_name = NULL;
  1427. fd->unix_fd = -1;
  1428. fd->cacheable = 0;
  1429. fd->signaled = 0;
  1430. fd->fs_locks = 0;
  1431. fd->poll_index = -1;
  1432. fd->completion = NULL;
  1433. fd->comp_flags = 0;
  1434. fd->no_fd_status = STATUS_BAD_DEVICE_TYPE;
  1435. fd->esync_fd = -1;
  1436. init_async_queue( &fd->read_q );
  1437. init_async_queue( &fd->write_q );
  1438. init_async_queue( &fd->wait_q );
  1439. list_init( &fd->inode_entry );
  1440. list_init( &fd->locks );
  1441. if (do_esync())
  1442. fd->esync_fd = esync_create_fd( 0, 0 );
  1443. return fd;
  1444. }
  1445. /* duplicate an fd object for a different user */
  1446. struct fd *dup_fd_object( struct fd *orig, unsigned int access, unsigned int sharing, unsigned int options )
  1447. {
  1448. unsigned int err;
  1449. struct fd *fd = alloc_fd_object();
  1450. if (!fd) return NULL;
  1451. fd->options = options;
  1452. fd->cacheable = orig->cacheable;
  1453. if (orig->unix_name)
  1454. {
  1455. if (!(fd->unix_name = mem_alloc( strlen(orig->unix_name) + 1 ))) goto failed;
  1456. strcpy( fd->unix_name, orig->unix_name );
  1457. }
  1458. if (orig->inode)
  1459. {
  1460. struct closed_fd *closed = mem_alloc( sizeof(*closed) );
  1461. if (!closed) goto failed;
  1462. if ((fd->unix_fd = dup( orig->unix_fd )) == -1)
  1463. {
  1464. file_set_error();
  1465. free( closed );
  1466. goto failed;
  1467. }
  1468. closed->unix_fd = fd->unix_fd;
  1469. closed->unlink = 0;
  1470. closed->unix_name = fd->unix_name;
  1471. fd->closed = closed;
  1472. fd->inode = (struct inode *)grab_object( orig->inode );
  1473. list_add_head( &fd->inode->open, &fd->inode_entry );
  1474. if ((err = check_sharing( fd, access, sharing, 0, options )))
  1475. {
  1476. set_error( err );
  1477. goto failed;
  1478. }
  1479. }
  1480. else if ((fd->unix_fd = dup( orig->unix_fd )) == -1)
  1481. {
  1482. file_set_error();
  1483. goto failed;
  1484. }
  1485. return fd;
  1486. failed:
  1487. release_object( fd );
  1488. return NULL;
  1489. }
  1490. /* find an existing fd object that can be reused for a mapping */
  1491. struct fd *get_fd_object_for_mapping( struct fd *fd, unsigned int access, unsigned int sharing )
  1492. {
  1493. struct fd *fd_ptr;
  1494. if (!fd->inode) return NULL;
  1495. LIST_FOR_EACH_ENTRY( fd_ptr, &fd->inode->open, struct fd, inode_entry )
  1496. if (fd_ptr->access == access && fd_ptr->sharing == sharing)
  1497. return (struct fd *)grab_object( fd_ptr );
  1498. return NULL;
  1499. }
  1500. /* sets the user of an fd that previously had no user */
  1501. void set_fd_user( struct fd *fd, const struct fd_ops *user_ops, struct object *user )
  1502. {
  1503. assert( fd->fd_ops == NULL );
  1504. fd->fd_ops = user_ops;
  1505. fd->user = user;
  1506. }
  1507. char *dup_fd_name( struct fd *root, const char *name )
  1508. {
  1509. char *ret;
  1510. if (!root) return strdup( name );
  1511. if (!root->unix_name) return NULL;
  1512. /* skip . prefix */
  1513. if (name[0] == '.' && (!name[1] || name[1] == '/')) name++;
  1514. if ((ret = malloc( strlen(root->unix_name) + strlen(name) + 2 )))
  1515. {
  1516. strcpy( ret, root->unix_name );
  1517. if (name[0] && name[0] != '/') strcat( ret, "/" );
  1518. strcat( ret, name );
  1519. }
  1520. return ret;
  1521. }
  1522. static void decode_symlink(char *name, int *is_dir)
  1523. {
  1524. char link[MAX_PATH], *p;
  1525. ULONG reparse_tag;
  1526. int len, i;
  1527. len = readlink( name, link, sizeof(link) );
  1528. link[len] = 0;
  1529. p = link;
  1530. /* skip past relative/absolute indication */
  1531. if (*p == '.')
  1532. p++;
  1533. if (*p++ != '/')
  1534. {
  1535. return;
  1536. }
  1537. /* decode the reparse tag */
  1538. reparse_tag = 0;
  1539. for (i = 0; i < sizeof(ULONG)*8; i++)
  1540. {
  1541. char c = *p++;
  1542. int val;
  1543. if (c == '/')
  1544. val = 0;
  1545. else if (c == '.' && *p++ == '/')
  1546. val = 1;
  1547. else
  1548. return;
  1549. reparse_tag |= (val << i);
  1550. }
  1551. /* decode the directory/file flag */
  1552. if (reparse_tag == IO_REPARSE_TAG_SYMLINK)
  1553. {
  1554. char c = *p++;
  1555. if (c == '/')
  1556. *is_dir = FALSE;
  1557. else if (c == '.' && *p++ == '/')
  1558. *is_dir = TRUE;
  1559. else
  1560. return;
  1561. }
  1562. else
  1563. *is_dir = TRUE;
  1564. }
  1565. /* open() wrapper that returns a struct fd with no fd user set */
  1566. struct fd *open_fd( struct fd *root, const char *name, int flags, mode_t *mode, unsigned int access,
  1567. unsigned int sharing, unsigned int options )
  1568. {
  1569. struct stat st;
  1570. struct closed_fd *closed_fd;
  1571. struct fd *fd;
  1572. int root_fd = -1;
  1573. int rw_mode;
  1574. char *path;
  1575. int do_chmod = 0;
  1576. int created = (flags & O_CREAT);
  1577. if (((options & FILE_DELETE_ON_CLOSE) && !(access & DELETE)) ||
  1578. ((options & FILE_DIRECTORY_FILE) && (flags & O_TRUNC)))
  1579. {
  1580. set_error( STATUS_INVALID_PARAMETER );
  1581. return NULL;
  1582. }
  1583. if (!(fd = alloc_fd_object())) return NULL;
  1584. fd->options = options;
  1585. if (!(closed_fd = mem_alloc( sizeof(*closed_fd) )))
  1586. {
  1587. release_object( fd );
  1588. return NULL;
  1589. }
  1590. if (root)
  1591. {
  1592. if ((root_fd = get_unix_fd( root )) == -1) goto error;
  1593. if (fchdir( root_fd ) == -1)
  1594. {
  1595. file_set_error();
  1596. root_fd = -1;
  1597. goto error;
  1598. }
  1599. }
  1600. /* create the directory if needed */
  1601. if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
  1602. {
  1603. if (mkdir( name, *mode | S_IRUSR ) != -1)
  1604. {
  1605. /* remove S_IRUSR later, after we have opened the directory */
  1606. do_chmod = !(*mode & S_IRUSR);
  1607. }
  1608. else
  1609. {
  1610. if (errno != EEXIST || (flags & O_EXCL))
  1611. {
  1612. file_set_error();
  1613. goto error;
  1614. }
  1615. created = 0;
  1616. }
  1617. flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
  1618. }
  1619. if ((access & FILE_UNIX_WRITE_ACCESS) && !(options & FILE_DIRECTORY_FILE))
  1620. {
  1621. if (access & FILE_UNIX_READ_ACCESS) rw_mode = O_RDWR;
  1622. else rw_mode = O_WRONLY;
  1623. }
  1624. else rw_mode = O_RDONLY;
  1625. if ((fd->unix_fd = open( name, rw_mode | (flags & ~O_TRUNC), *mode )) == -1)
  1626. {
  1627. /* if we tried to open a directory for write access, retry read-only */
  1628. if (errno == EISDIR)
  1629. {
  1630. if ((access & FILE_UNIX_WRITE_ACCESS) || (flags & O_CREAT))
  1631. fd->unix_fd = open( name, O_RDONLY | (flags & ~(O_TRUNC | O_CREAT | O_EXCL)), *mode );
  1632. }
  1633. #if defined(O_SYMLINK)
  1634. /* if we tried to open a dangling symlink then try again with O_SYMLINK */
  1635. else if (errno == ENOENT)
  1636. {
  1637. fd->unix_fd = open( name, rw_mode | O_SYMLINK | (flags & ~O_TRUNC), *mode );
  1638. }
  1639. #endif
  1640. else if (errno == EACCES)
  1641. {
  1642. /* try to change permissions temporarily to open a file descriptor */
  1643. if (!(access & ((FILE_UNIX_WRITE_ACCESS | FILE_UNIX_READ_ACCESS | DELETE) & ~FILE_WRITE_ATTRIBUTES)) &&
  1644. !stat( name, &st ) && st.st_uid == getuid() &&
  1645. !chmod( name, st.st_mode | S_IRUSR ))
  1646. {
  1647. fd->unix_fd = open( name, O_RDONLY | (flags & ~(O_TRUNC | O_CREAT | O_EXCL)), *mode );
  1648. *mode = st.st_mode;
  1649. do_chmod = 1;
  1650. }
  1651. else
  1652. {
  1653. set_error( STATUS_ACCESS_DENIED );
  1654. goto error;
  1655. }
  1656. }
  1657. if (fd->unix_fd == -1)
  1658. {
  1659. file_set_error();
  1660. if (do_chmod) chmod( name, *mode );
  1661. goto error;
  1662. }
  1663. }
  1664. fd->unix_name = NULL;
  1665. if ((path = dup_fd_name( root, name )))
  1666. {
  1667. fd->unix_name = realpath( path, NULL );
  1668. free( path );
  1669. }
  1670. closed_fd->unix_fd = fd->unix_fd;
  1671. closed_fd->unlink = 0;
  1672. closed_fd->unix_name = fd->unix_name;
  1673. if (do_chmod) chmod( name, *mode );
  1674. lstat( fd->unix_name, &st );
  1675. *mode = st.st_mode;
  1676. /* only bother with an inode for normal files and directories */
  1677. if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
  1678. {
  1679. unsigned int err;
  1680. struct inode *inode = get_inode( st.st_dev, st.st_ino, fd->unix_fd );
  1681. int is_link = S_ISLNK(st.st_mode), is_dir;
  1682. if (!inode)
  1683. {
  1684. /* we can close the fd because there are no others open on the same file,
  1685. * otherwise we wouldn't have failed to allocate a new inode
  1686. */
  1687. goto error;
  1688. }
  1689. fd->inode = inode;
  1690. fd->closed = closed_fd;
  1691. fd->cacheable = !inode->device->removable;
  1692. list_add_head( &inode->open, &fd->inode_entry );
  1693. closed_fd = NULL;
  1694. /* decode symlink type */
  1695. fstat( fd->unix_fd, &st );
  1696. *mode = st.st_mode;
  1697. is_dir = S_ISDIR(st.st_mode);
  1698. if (is_link)
  1699. decode_symlink(fd->unix_name, &is_dir);
  1700. /* check directory options */
  1701. if ((options & FILE_DIRECTORY_FILE) && !is_dir)
  1702. {
  1703. set_error( STATUS_NOT_A_DIRECTORY );
  1704. goto error;
  1705. }
  1706. if ((options & FILE_NON_DIRECTORY_FILE) && is_dir)
  1707. {
  1708. set_error( STATUS_FILE_IS_A_DIRECTORY );
  1709. goto error;
  1710. }
  1711. if ((err = check_sharing( fd, access, sharing, flags, options )))
  1712. {
  1713. set_error( err );
  1714. goto error;
  1715. }
  1716. /* can't unlink files if we don't have permission to access */
  1717. if ((options & FILE_DELETE_ON_CLOSE) && !created &&
  1718. !(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
  1719. {
  1720. set_error( STATUS_CANNOT_DELETE );
  1721. goto error;
  1722. }
  1723. fd->closed->unlink = (options & FILE_DELETE_ON_CLOSE) ? -1 : 0;
  1724. if (flags & O_TRUNC)
  1725. {
  1726. if (S_ISDIR(st.st_mode))
  1727. {
  1728. set_error( STATUS_OBJECT_NAME_COLLISION );
  1729. goto error;
  1730. }
  1731. ftruncate( fd->unix_fd, 0 );
  1732. }
  1733. }
  1734. else /* special file */
  1735. {
  1736. if (options & FILE_DELETE_ON_CLOSE) /* we can't unlink special files */
  1737. {
  1738. set_error( STATUS_INVALID_PARAMETER );
  1739. goto error;
  1740. }
  1741. free( closed_fd );
  1742. fd->cacheable = 1;
  1743. }
  1744. if (root_fd != -1) fchdir( server_dir_fd ); /* go back to the server dir */
  1745. return fd;
  1746. error:
  1747. release_object( fd );
  1748. free( closed_fd );
  1749. if (root_fd != -1) fchdir( server_dir_fd ); /* go back to the server dir */
  1750. return NULL;
  1751. }
  1752. /* create an fd for an anonymous file */
  1753. /* if the function fails the unix fd is closed */
  1754. struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user,
  1755. unsigned int options )
  1756. {
  1757. struct fd *fd = alloc_fd_object();
  1758. if (fd)
  1759. {
  1760. set_fd_user( fd, fd_user_ops, user );
  1761. fd->unix_fd = unix_fd;
  1762. fd->options = options;
  1763. return fd;
  1764. }
  1765. close( unix_fd );
  1766. return NULL;
  1767. }
  1768. /* retrieve the object that is using an fd */
  1769. void *get_fd_user( struct fd *fd )
  1770. {
  1771. return fd->user;
  1772. }
  1773. /* retrieve the opening options for the fd */
  1774. unsigned int get_fd_options( struct fd *fd )
  1775. {
  1776. return fd->options;
  1777. }
  1778. /* check if fd is in overlapped mode */
  1779. int is_fd_overlapped( struct fd *fd )
  1780. {
  1781. return !(fd->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
  1782. }
  1783. /* retrieve the unix fd for an object */
  1784. int get_unix_fd( struct fd *fd )
  1785. {
  1786. if (fd->unix_fd == -1) set_error( fd->no_fd_status );
  1787. return fd->unix_fd;
  1788. }
  1789. /* check if two file descriptors point to the same file */
  1790. int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
  1791. {
  1792. return fd1->inode == fd2->inode;
  1793. }
  1794. /* allow the fd to be cached (can't be reset once set) */
  1795. void allow_fd_caching( struct fd *fd )
  1796. {
  1797. fd->cacheable = 1;
  1798. }
  1799. /* check if fd is on a removable device */
  1800. int is_fd_removable( struct fd *fd )
  1801. {
  1802. return (fd->inode && fd->inode->device->removable);
  1803. }
  1804. /* set or clear the fd signaled state */
  1805. void set_fd_signaled( struct fd *fd, int signaled )
  1806. {
  1807. if (fd->comp_flags & FILE_SKIP_SET_EVENT_ON_HANDLE) return;
  1808. fd->signaled = signaled;
  1809. if (signaled) wake_up( fd->user, 0 );
  1810. if (do_esync() && !signaled)
  1811. esync_clear( fd->esync_fd );
  1812. }
  1813. /* check if fd is signaled */
  1814. int is_fd_signaled( struct fd *fd )
  1815. {
  1816. return fd->signaled;
  1817. }
  1818. /* handler for close_handle that refuses to close fd-associated handles in other processes */
  1819. int fd_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
  1820. {
  1821. return (!current || current->process == process);
  1822. }
  1823. /* check if events are pending and if yes return which one(s) */
  1824. int check_fd_events( struct fd *fd, int events )
  1825. {
  1826. struct pollfd pfd;
  1827. if (fd->unix_fd == -1) return POLLERR;
  1828. if (fd->inode) return events; /* regular files are always signaled */
  1829. pfd.fd = fd->unix_fd;
  1830. pfd.events = events;
  1831. if (poll( &pfd, 1, 0 ) <= 0) return 0;
  1832. return pfd.revents;
  1833. }
  1834. /* default signaled() routine for objects that poll() on an fd */
  1835. int default_fd_signaled( struct object *obj, struct wait_queue_entry *entry )
  1836. {
  1837. struct fd *fd = get_obj_fd( obj );
  1838. int ret = fd->signaled;
  1839. release_object( fd );
  1840. return ret;
  1841. }
  1842. int default_fd_get_esync_fd( struct object *obj, enum esync_type *type )
  1843. {
  1844. struct fd *fd = get_obj_fd( obj );
  1845. int ret = fd->esync_fd;
  1846. *type = ESYNC_MANUAL_SERVER;
  1847. release_object( fd );
  1848. return ret;
  1849. }
  1850. /* default map_access() routine for objects that behave like an fd */
  1851. unsigned int default_fd_map_access( struct object *obj, unsigned int access )
  1852. {
  1853. if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
  1854. if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
  1855. if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
  1856. if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
  1857. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  1858. }
  1859. int default_fd_get_poll_events( struct fd *fd )
  1860. {
  1861. int events = 0;
  1862. if (async_waiting( &fd->read_q )) events |= POLLIN;
  1863. if (async_waiting( &fd->write_q )) events |= POLLOUT;
  1864. return events;
  1865. }
  1866. /* default handler for poll() events */
  1867. void default_poll_event( struct fd *fd, int event )
  1868. {
  1869. if (event & (POLLIN | POLLERR | POLLHUP)) async_wake_up( &fd->read_q, STATUS_ALERTED );
  1870. if (event & (POLLOUT | POLLERR | POLLHUP)) async_wake_up( &fd->write_q, STATUS_ALERTED );
  1871. /* if an error occurred, stop polling this fd to avoid busy-looping */
  1872. if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
  1873. else if (!fd->inode) set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
  1874. }
  1875. void fd_queue_async( struct fd *fd, struct async *async, int type )
  1876. {
  1877. struct async_queue *queue;
  1878. switch (type)
  1879. {
  1880. case ASYNC_TYPE_READ:
  1881. queue = &fd->read_q;
  1882. break;
  1883. case ASYNC_TYPE_WRITE:
  1884. queue = &fd->write_q;
  1885. break;
  1886. case ASYNC_TYPE_WAIT:
  1887. queue = &fd->wait_q;
  1888. break;
  1889. default:
  1890. queue = NULL;
  1891. assert(0);
  1892. }
  1893. queue_async( queue, async );
  1894. if (type != ASYNC_TYPE_WAIT)
  1895. {
  1896. if (!fd->inode)
  1897. set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
  1898. else /* regular files are always ready for read and write */
  1899. async_wake_up( queue, STATUS_ALERTED );
  1900. }
  1901. }
  1902. void fd_async_wake_up( struct fd *fd, int type, unsigned int status )
  1903. {
  1904. switch (type)
  1905. {
  1906. case ASYNC_TYPE_READ:
  1907. async_wake_up( &fd->read_q, status );
  1908. break;
  1909. case ASYNC_TYPE_WRITE:
  1910. async_wake_up( &fd->write_q, status );
  1911. break;
  1912. case ASYNC_TYPE_WAIT:
  1913. async_wake_up( &fd->wait_q, status );
  1914. break;
  1915. default:
  1916. assert(0);
  1917. }
  1918. }
  1919. void fd_reselect_async( struct fd *fd, struct async_queue *queue )
  1920. {
  1921. fd->fd_ops->reselect_async( fd, queue );
  1922. }
  1923. void no_fd_queue_async( struct fd *fd, struct async *async, int type, int count )
  1924. {
  1925. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  1926. }
  1927. void default_fd_queue_async( struct fd *fd, struct async *async, int type, int count )
  1928. {
  1929. fd_queue_async( fd, async, type );
  1930. set_error( STATUS_PENDING );
  1931. }
  1932. /* default reselect_async() fd routine */
  1933. void default_fd_reselect_async( struct fd *fd, struct async_queue *queue )
  1934. {
  1935. if (queue == &fd->read_q || queue == &fd->write_q)
  1936. {
  1937. int poll_events = fd->fd_ops->get_poll_events( fd );
  1938. int events = check_fd_events( fd, poll_events );
  1939. if (events) fd->fd_ops->poll_event( fd, events );
  1940. else set_fd_events( fd, poll_events );
  1941. }
  1942. }
  1943. static inline int is_valid_mounted_device( struct stat *st )
  1944. {
  1945. #if defined(linux) || defined(__sun__)
  1946. return S_ISBLK( st->st_mode );
  1947. #else
  1948. /* disks are char devices on *BSD */
  1949. return S_ISCHR( st->st_mode );
  1950. #endif
  1951. }
  1952. /* close all Unix file descriptors on a device to allow unmounting it */
  1953. static void unmount_device( struct fd *device_fd )
  1954. {
  1955. unsigned int i;
  1956. struct stat st;
  1957. struct device *device;
  1958. struct inode *inode;
  1959. struct fd *fd;
  1960. int unix_fd = get_unix_fd( device_fd );
  1961. if (unix_fd == -1) return;
  1962. if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
  1963. {
  1964. set_error( STATUS_INVALID_PARAMETER );
  1965. return;
  1966. }
  1967. if (!(device = get_device( st.st_rdev, -1 ))) return;
  1968. for (i = 0; i < INODE_HASH_SIZE; i++)
  1969. {
  1970. LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
  1971. {
  1972. LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
  1973. {
  1974. unmount_fd( fd );
  1975. }
  1976. inode_close_pending( inode, 0 );
  1977. }
  1978. }
  1979. /* remove it from the hash table */
  1980. list_remove( &device->entry );
  1981. list_init( &device->entry );
  1982. release_object( device );
  1983. }
  1984. /* default read() routine */
  1985. int no_fd_read( struct fd *fd, struct async *async, file_pos_t pos )
  1986. {
  1987. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  1988. return 0;
  1989. }
  1990. /* default write() routine */
  1991. int no_fd_write( struct fd *fd, struct async *async, file_pos_t pos )
  1992. {
  1993. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  1994. return 0;
  1995. }
  1996. /* default flush() routine */
  1997. int no_fd_flush( struct fd *fd, struct async *async )
  1998. {
  1999. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  2000. return 0;
  2001. }
  2002. /* default get_file_info() routine */
  2003. void no_fd_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
  2004. {
  2005. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  2006. }
  2007. /* default get_file_info() routine */
  2008. void default_fd_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
  2009. {
  2010. switch (info_class)
  2011. {
  2012. case FileAccessInformation:
  2013. {
  2014. FILE_ACCESS_INFORMATION info;
  2015. if (get_reply_max_size() < sizeof(info))
  2016. {
  2017. set_error( STATUS_INFO_LENGTH_MISMATCH );
  2018. return;
  2019. }
  2020. info.AccessFlags = get_handle_access( current->process, handle );
  2021. set_reply_data( &info, sizeof(info) );
  2022. break;
  2023. }
  2024. case FileModeInformation:
  2025. {
  2026. FILE_MODE_INFORMATION info;
  2027. if (get_reply_max_size() < sizeof(info))
  2028. {
  2029. set_error( STATUS_INFO_LENGTH_MISMATCH );
  2030. return;
  2031. }
  2032. info.Mode = fd->options & ( FILE_WRITE_THROUGH
  2033. | FILE_SEQUENTIAL_ONLY
  2034. | FILE_NO_INTERMEDIATE_BUFFERING
  2035. | FILE_SYNCHRONOUS_IO_ALERT
  2036. | FILE_SYNCHRONOUS_IO_NONALERT );
  2037. set_reply_data( &info, sizeof(info) );
  2038. break;
  2039. }
  2040. case FileIoCompletionNotificationInformation:
  2041. {
  2042. FILE_IO_COMPLETION_NOTIFICATION_INFORMATION info;
  2043. if (get_reply_max_size() < sizeof(info))
  2044. {
  2045. set_error( STATUS_INFO_LENGTH_MISMATCH );
  2046. return;
  2047. }
  2048. info.Flags = fd->comp_flags;
  2049. set_reply_data( &info, sizeof(info) );
  2050. break;
  2051. }
  2052. default:
  2053. set_error( STATUS_NOT_IMPLEMENTED );
  2054. }
  2055. }
  2056. /* default get_volume_info() routine */
  2057. void no_fd_get_volume_info( struct fd *fd, unsigned int info_class )
  2058. {
  2059. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  2060. }
  2061. /* default ioctl() routine */
  2062. int no_fd_ioctl( struct fd *fd, ioctl_code_t code, client_ptr_t in_buf, client_ptr_t out_buf, struct async *async )
  2063. {
  2064. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  2065. return 0;
  2066. }
  2067. /* default ioctl() routine */
  2068. int default_fd_ioctl( struct fd *fd, ioctl_code_t code, client_ptr_t in_buf, client_ptr_t out_buf, struct async *async )
  2069. {
  2070. switch(code)
  2071. {
  2072. case FSCTL_DISMOUNT_VOLUME:
  2073. unmount_device( fd );
  2074. return 1;
  2075. default:
  2076. set_error( STATUS_NOT_SUPPORTED );
  2077. return 0;
  2078. }
  2079. }
  2080. /* same as get_handle_obj but retrieve the struct fd associated to the object */
  2081. static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
  2082. unsigned int access )
  2083. {
  2084. struct fd *fd = NULL;
  2085. struct object *obj;
  2086. if ((obj = get_handle_obj( process, handle, access, NULL )))
  2087. {
  2088. fd = get_obj_fd( obj );
  2089. release_object( obj );
  2090. }
  2091. return fd;
  2092. }
  2093. /* set disposition for the fd */
  2094. static void set_fd_disposition( struct fd *fd, int unlink )
  2095. {
  2096. struct stat st;
  2097. struct list *ptr;
  2098. if (!fd->inode)
  2099. {
  2100. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  2101. return;
  2102. }
  2103. if (fd->unix_fd == -1)
  2104. {
  2105. set_error( fd->no_fd_status );
  2106. return;
  2107. }
  2108. if (fstat( fd->unix_fd, &st ) == -1)
  2109. {
  2110. file_set_error();
  2111. return;
  2112. }
  2113. /* can't unlink special files */
  2114. if (unlink && !S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
  2115. {
  2116. set_error( STATUS_INVALID_PARAMETER );
  2117. return;
  2118. }
  2119. /* can't unlink files we don't have permission to write */
  2120. if (unlink && !(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) && !S_ISDIR(st.st_mode))
  2121. {
  2122. set_error( STATUS_CANNOT_DELETE );
  2123. return;
  2124. }
  2125. /* can't unlink files which are mapped to memory */
  2126. LIST_FOR_EACH( ptr, &fd->inode->open )
  2127. {
  2128. struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
  2129. if (fd_ptr != fd && (fd_ptr->access & FILE_MAPPING_ACCESS))
  2130. {
  2131. set_error( STATUS_CANNOT_DELETE );
  2132. return;
  2133. }
  2134. }
  2135. fd->closed->unlink = unlink ? 1 : 0;
  2136. if (fd->options & FILE_DELETE_ON_CLOSE)
  2137. fd->closed->unlink = -1;
  2138. }
  2139. /* set new name for the fd */
  2140. static void set_fd_name( struct fd *fd, struct fd *root, const char *nameptr,
  2141. data_size_t len, int create_link, int replace )
  2142. {
  2143. struct inode *inode;
  2144. struct stat st, st2;
  2145. char *name;
  2146. if (!fd->inode || !fd->unix_name)
  2147. {
  2148. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  2149. return;
  2150. }
  2151. if (fd->unix_fd == -1)
  2152. {
  2153. set_error( fd->no_fd_status );
  2154. return;
  2155. }
  2156. if (!len || ((nameptr[0] == '/') ^ !root))
  2157. {
  2158. set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
  2159. return;
  2160. }
  2161. if (!(name = mem_alloc( len + 1 ))) return;
  2162. memcpy( name, nameptr, len );
  2163. name[len] = 0;
  2164. if (root)
  2165. {
  2166. char *combined_name = dup_fd_name( root, name );
  2167. if (!combined_name)
  2168. {
  2169. set_error( STATUS_NO_MEMORY );
  2170. goto failed;
  2171. }
  2172. free( name );
  2173. name = combined_name;
  2174. }
  2175. /* when creating a hard link, source cannot be a dir */
  2176. if (create_link && !fstat( fd->unix_fd, &st ) && S_ISDIR( st.st_mode ))
  2177. {
  2178. set_error( STATUS_FILE_IS_A_DIRECTORY );
  2179. goto failed;
  2180. }
  2181. if (!stat( name, &st ))
  2182. {
  2183. if (!fstat( fd->unix_fd, &st2 ) && st.st_ino == st2.st_ino && st.st_dev == st2.st_dev)
  2184. {
  2185. if (create_link && !replace) set_error( STATUS_OBJECT_NAME_COLLISION );
  2186. free( name );
  2187. return;
  2188. }
  2189. if (!replace)
  2190. {
  2191. set_error( STATUS_OBJECT_NAME_COLLISION );
  2192. goto failed;
  2193. }
  2194. /* can't replace directories or special files */
  2195. if (!S_ISREG( st.st_mode ))
  2196. {
  2197. set_error( STATUS_ACCESS_DENIED );
  2198. goto failed;
  2199. }
  2200. /* can't replace an opened file */
  2201. if ((inode = get_inode( st.st_dev, st.st_ino, -1 )))
  2202. {
  2203. int is_empty = list_empty( &inode->open );
  2204. release_object( inode );
  2205. if (!is_empty)
  2206. {
  2207. set_error( STATUS_ACCESS_DENIED );
  2208. goto failed;
  2209. }
  2210. }
  2211. /* link() expects that the target doesn't exist */
  2212. /* rename() cannot replace files with directories */
  2213. if (create_link || S_ISDIR( st2.st_mode ))
  2214. {
  2215. if (unlink( name ))
  2216. {
  2217. file_set_error();
  2218. goto failed;
  2219. }
  2220. }
  2221. }
  2222. if (create_link)
  2223. {
  2224. if (link( fd->unix_name, name ))
  2225. file_set_error();
  2226. free( name );
  2227. return;
  2228. }
  2229. if (rename( fd->unix_name, name ))
  2230. {
  2231. file_set_error();
  2232. goto failed;
  2233. }
  2234. if (is_file_executable( fd->unix_name ) != is_file_executable( name ) && !fstat( fd->unix_fd, &st ))
  2235. {
  2236. if (is_file_executable( name ))
  2237. /* set executable bit where read bit is set */
  2238. st.st_mode |= (st.st_mode & 0444) >> 2;
  2239. else
  2240. st.st_mode &= ~0111;
  2241. fchmod( fd->unix_fd, st.st_mode );
  2242. }
  2243. free( fd->unix_name );
  2244. fd->closed->unix_name = fd->unix_name = realpath( name, NULL );
  2245. free( name );
  2246. if (!fd->unix_name)
  2247. set_error( STATUS_NO_MEMORY );
  2248. return;
  2249. failed:
  2250. free( name );
  2251. }
  2252. static void set_fd_eof( struct fd *fd, file_pos_t eof )
  2253. {
  2254. static const char zero;
  2255. struct stat st;
  2256. struct list *ptr;
  2257. if (!fd->inode)
  2258. {
  2259. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  2260. return;
  2261. }
  2262. if (fd->unix_fd == -1)
  2263. {
  2264. set_error( fd->no_fd_status );
  2265. return;
  2266. }
  2267. if (fstat( fd->unix_fd, &st ) == -1)
  2268. {
  2269. file_set_error();
  2270. return;
  2271. }
  2272. /* can't truncate files which are mapped to memory */
  2273. if (eof < st.st_size)
  2274. {
  2275. LIST_FOR_EACH( ptr, &fd->inode->open )
  2276. {
  2277. struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
  2278. if (fd_ptr != fd && (fd_ptr->access & FILE_MAPPING_ACCESS))
  2279. {
  2280. set_error( STATUS_USER_MAPPED_FILE );
  2281. return;
  2282. }
  2283. }
  2284. }
  2285. /* first try normal truncate */
  2286. if (ftruncate( fd->unix_fd, eof ) != -1) return;
  2287. /* now check for the need to extend the file */
  2288. if (eof > st.st_size)
  2289. {
  2290. /* extend the file one byte beyond the requested size and then truncate it */
  2291. /* this should work around ftruncate implementations that can't extend files */
  2292. if (pwrite( fd->unix_fd, &zero, 1, eof ) != -1 &&
  2293. ftruncate( fd->unix_fd, eof) != -1) return;
  2294. }
  2295. file_set_error();
  2296. }
  2297. struct completion *fd_get_completion( struct fd *fd, apc_param_t *p_key )
  2298. {
  2299. *p_key = fd->comp_key;
  2300. return fd->completion ? (struct completion *)grab_object( fd->completion ) : NULL;
  2301. }
  2302. void fd_copy_completion( struct fd *src, struct fd *dst )
  2303. {
  2304. assert( !dst->completion );
  2305. dst->completion = fd_get_completion( src, &dst->comp_key );
  2306. dst->comp_flags = src->comp_flags;
  2307. }
  2308. /* flush a file buffers */
  2309. DECL_HANDLER(flush)
  2310. {
  2311. struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, 0 );
  2312. struct async *async;
  2313. if (!fd) return;
  2314. if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
  2315. {
  2316. reply->event = async_handoff( async, fd->fd_ops->flush( fd, async ), NULL, 1 );
  2317. release_object( async );
  2318. }
  2319. release_object( fd );
  2320. }
  2321. /* query file info */
  2322. DECL_HANDLER(get_file_info)
  2323. {
  2324. struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
  2325. if (fd)
  2326. {
  2327. fd->fd_ops->get_file_info( fd, req->handle, req->info_class );
  2328. release_object( fd );
  2329. }
  2330. }
  2331. /* query volume info */
  2332. DECL_HANDLER(get_volume_info)
  2333. {
  2334. struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
  2335. if (fd)
  2336. {
  2337. fd->fd_ops->get_volume_info( fd, req->info_class );
  2338. release_object( fd );
  2339. }
  2340. }
  2341. /* open a file object */
  2342. DECL_HANDLER(open_file_object)
  2343. {
  2344. struct unicode_str name = get_req_unicode_str();
  2345. struct object *obj, *result, *root = NULL;
  2346. if (req->rootdir && !(root = get_handle_obj( current->process, req->rootdir, 0, NULL ))) return;
  2347. obj = open_named_object( root, NULL, &name, req->attributes );
  2348. if (root) release_object( root );
  2349. if (!obj) return;
  2350. if ((result = obj->ops->open_file( obj, req->access, req->sharing, req->options )))
  2351. {
  2352. reply->handle = alloc_handle( current->process, result, req->access, req->attributes );
  2353. release_object( result );
  2354. }
  2355. release_object( obj );
  2356. }
  2357. /* get the Unix name from a file handle */
  2358. DECL_HANDLER(get_handle_unix_name)
  2359. {
  2360. struct fd *fd;
  2361. if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
  2362. {
  2363. if (fd->unix_name)
  2364. {
  2365. data_size_t name_len = strlen( fd->unix_name );
  2366. reply->name_len = name_len;
  2367. if (name_len <= get_reply_max_size()) set_reply_data( fd->unix_name, name_len );
  2368. else set_error( STATUS_BUFFER_OVERFLOW );
  2369. }
  2370. else set_error( STATUS_OBJECT_TYPE_MISMATCH );
  2371. release_object( fd );
  2372. }
  2373. }
  2374. /* get a Unix fd to access a file */
  2375. DECL_HANDLER(get_handle_fd)
  2376. {
  2377. struct fd *fd;
  2378. if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
  2379. {
  2380. int unix_fd = get_unix_fd( fd );
  2381. reply->cacheable = fd->cacheable;
  2382. if (unix_fd != -1)
  2383. {
  2384. reply->type = fd->fd_ops->get_fd_type( fd );
  2385. reply->options = fd->options;
  2386. reply->access = get_handle_access( current->process, req->handle );
  2387. send_client_fd( current->process, unix_fd, req->handle );
  2388. }
  2389. release_object( fd );
  2390. }
  2391. }
  2392. /* perform a read on a file object */
  2393. DECL_HANDLER(read)
  2394. {
  2395. struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, FILE_READ_DATA );
  2396. struct async *async;
  2397. if (!fd) return;
  2398. if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
  2399. {
  2400. reply->wait = async_handoff( async, fd->fd_ops->read( fd, async, req->pos ), NULL, 0 );
  2401. reply->options = fd->options;
  2402. release_object( async );
  2403. }
  2404. release_object( fd );
  2405. }
  2406. /* perform a write on a file object */
  2407. DECL_HANDLER(write)
  2408. {
  2409. struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, FILE_WRITE_DATA );
  2410. struct async *async;
  2411. if (!fd) return;
  2412. if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
  2413. {
  2414. reply->wait = async_handoff( async, fd->fd_ops->write( fd, async, req->pos ), &reply->size, 0 );
  2415. reply->options = fd->options;
  2416. release_object( async );
  2417. }
  2418. release_object( fd );
  2419. }
  2420. /* get file descriptor to shared memory block */
  2421. DECL_HANDLER(get_shared_memory)
  2422. {
  2423. if (req->tid)
  2424. {
  2425. struct thread *thread = get_thread_from_id( req->tid );
  2426. if (thread)
  2427. {
  2428. if (thread->shm_fd != -1 || allocate_shared_memory( &thread->shm_fd,
  2429. (void **)&thread->shm, sizeof(*thread->shm) ))
  2430. {
  2431. send_client_fd( current->process, thread->shm_fd, 0 );
  2432. }
  2433. else
  2434. set_error( STATUS_NOT_SUPPORTED );
  2435. release_object( thread );
  2436. }
  2437. }
  2438. else
  2439. {
  2440. if (shmglobal_fd != -1)
  2441. send_client_fd( current->process, shmglobal_fd, 0 );
  2442. else
  2443. set_error( STATUS_NOT_SUPPORTED );
  2444. }
  2445. }
  2446. /* perform an ioctl on a file */
  2447. DECL_HANDLER(ioctl)
  2448. {
  2449. unsigned int access = (req->code >> 14) & (FILE_READ_DATA|FILE_WRITE_DATA);
  2450. struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, access );
  2451. struct async *async;
  2452. if (!fd) return;
  2453. if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
  2454. {
  2455. reply->wait = async_handoff( async, fd->fd_ops->ioctl( fd, req->code, req->input_buffer, req->output_buffer, async ), NULL, 0 );
  2456. reply->options = fd->options;
  2457. release_object( async );
  2458. }
  2459. release_object( fd );
  2460. }
  2461. /* create / reschedule an async I/O */
  2462. DECL_HANDLER(register_async)
  2463. {
  2464. unsigned int access;
  2465. struct async *async;
  2466. struct fd *fd;
  2467. switch(req->type)
  2468. {
  2469. case ASYNC_TYPE_READ:
  2470. access = FILE_READ_DATA;
  2471. break;
  2472. case ASYNC_TYPE_WRITE:
  2473. access = FILE_WRITE_DATA;
  2474. break;
  2475. default:
  2476. set_error( STATUS_INVALID_PARAMETER );
  2477. return;
  2478. }
  2479. if ((fd = get_handle_fd_obj( current->process, req->async.handle, access )))
  2480. {
  2481. if (get_unix_fd( fd ) != -1 && (async = create_async( fd, current, &req->async, NULL )))
  2482. {
  2483. fd->fd_ops->queue_async( fd, async, req->type, req->count );
  2484. release_object( async );
  2485. }
  2486. release_object( fd );
  2487. }
  2488. }
  2489. /* attach completion object to a fd */
  2490. DECL_HANDLER(set_completion_info)
  2491. {
  2492. struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
  2493. if (fd)
  2494. {
  2495. if (is_fd_overlapped( fd ) && !fd->completion)
  2496. {
  2497. fd->completion = get_completion_obj( current->process, req->chandle, IO_COMPLETION_MODIFY_STATE );
  2498. fd->comp_key = req->ckey;
  2499. }
  2500. else set_error( STATUS_INVALID_PARAMETER );
  2501. release_object( fd );
  2502. }
  2503. }
  2504. /* push new completion msg into a completion queue attached to the fd */
  2505. DECL_HANDLER(add_fd_completion)
  2506. {
  2507. struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
  2508. if (fd)
  2509. {
  2510. if (fd->completion && (req->async || !(fd->comp_flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)))
  2511. add_completion( fd->completion, fd->comp_key, req->cvalue, req->status, req->information );
  2512. release_object( fd );
  2513. }
  2514. }
  2515. /* set fd completion information */
  2516. DECL_HANDLER(set_fd_completion_mode)
  2517. {
  2518. struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
  2519. if (fd)
  2520. {
  2521. if (is_fd_overlapped( fd ))
  2522. {
  2523. if (req->flags & FILE_SKIP_SET_EVENT_ON_HANDLE)
  2524. set_fd_signaled( fd, 0 );
  2525. /* removing flags is not allowed */
  2526. fd->comp_flags |= req->flags & ( FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
  2527. | FILE_SKIP_SET_EVENT_ON_HANDLE
  2528. | FILE_SKIP_SET_USER_EVENT_ON_FAST_IO );
  2529. }
  2530. else
  2531. set_error( STATUS_INVALID_PARAMETER );
  2532. release_object( fd );
  2533. }
  2534. }
  2535. /* set fd disposition information */
  2536. DECL_HANDLER(set_fd_disp_info)
  2537. {
  2538. struct fd *fd = get_handle_fd_obj( current->process, req->handle, DELETE );
  2539. if (fd)
  2540. {
  2541. set_fd_disposition( fd, req->unlink );
  2542. release_object( fd );
  2543. }
  2544. }
  2545. /* set fd name information */
  2546. DECL_HANDLER(set_fd_name_info)
  2547. {
  2548. struct fd *fd, *root_fd = NULL;
  2549. if (req->rootdir)
  2550. {
  2551. struct dir *root;
  2552. if (!(root = get_dir_obj( current->process, req->rootdir, 0 ))) return;
  2553. root_fd = get_obj_fd( (struct object *)root );
  2554. release_object( root );
  2555. if (!root_fd) return;
  2556. }
  2557. if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
  2558. {
  2559. set_fd_name( fd, root_fd, get_req_data(), get_req_data_size(), req->link, req->replace );
  2560. release_object( fd );
  2561. }
  2562. if (root_fd) release_object( root_fd );
  2563. }
  2564. /* set fd eof information */
  2565. DECL_HANDLER(set_fd_eof_info)
  2566. {
  2567. struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
  2568. if (fd)
  2569. {
  2570. set_fd_eof( fd, req->eof );
  2571. release_object( fd );
  2572. }
  2573. }