fd.c 87 KB

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