fd.c 86 KB

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