fd.c 88 KB

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