ipc_right.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1991,1990,1989 Carnegie Mellon University
  4. * All Rights Reserved.
  5. *
  6. * Permission to use, copy, modify and distribute this software and its
  7. * documentation is hereby granted, provided that both the copyright
  8. * notice and this permission notice appear in all copies of the
  9. * software, derivative works or modified versions, and any portions
  10. * thereof, and that both notices appear in supporting documentation.
  11. *
  12. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15. *
  16. * Carnegie Mellon requests users of this software to return to
  17. *
  18. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  19. * School of Computer Science
  20. * Carnegie Mellon University
  21. * Pittsburgh PA 15213-3890
  22. *
  23. * any improvements or extensions that they make and grant Carnegie Mellon
  24. * the rights to redistribute these changes.
  25. */
  26. /*
  27. */
  28. /*
  29. * File: ipc/ipc_right.c
  30. * Author: Rich Draves
  31. * Date: 1989
  32. *
  33. * Functions to manipulate IPC capabilities.
  34. */
  35. #pragma GCC diagnostic error "-Wundef"
  36. #include <glue/gnulinux.h>
  37. #include <mach/boolean.h>
  38. #include <mach/kern_return.h>
  39. #include <mach/port.h>
  40. #include <mach/message.h>
  41. #include <kern/assert.h>
  42. #include <kern/debug.h>
  43. #include <ipc/port.h>
  44. #include <ipc/ipc_entry.h>
  45. #include <ipc/ipc_space.h>
  46. #include <ipc/ipc_object.h>
  47. #include <ipc/ipc_hash.h>
  48. #include <ipc/ipc_port.h>
  49. #include <ipc/ipc_pset.h>
  50. #include <ipc/ipc_marequest.h>
  51. #include <ipc/ipc_right.h>
  52. #include <ipc/ipc_notify.h>
  53. /*
  54. * Routine: ipc_right_lookup_write
  55. * Purpose:
  56. * Finds an entry in a space, given the name.
  57. * Conditions:
  58. * Nothing locked. If successful, the space is write-locked.
  59. * Returns:
  60. * KERN_SUCCESS Found an entry.
  61. * KERN_INVALID_TASK The space is dead.
  62. * KERN_INVALID_NAME Name doesn't exist in space.
  63. */
  64. kern_return_t
  65. ipc_right_lookup_write(
  66. ipc_space_t space,
  67. mach_port_t name,
  68. ipc_entry_t *entryp)
  69. {
  70. ipc_entry_t entry;
  71. assert(space != IS_NULL);
  72. is_write_lock(space);
  73. if (!space->is_active) {
  74. is_write_unlock(space);
  75. return KERN_INVALID_TASK;
  76. }
  77. if ((entry = ipc_entry_lookup(space, name)) == IE_NULL) {
  78. is_write_unlock(space);
  79. return KERN_INVALID_NAME;
  80. }
  81. *entryp = entry;
  82. return KERN_SUCCESS;
  83. }
  84. /*
  85. * Routine: ipc_right_reverse
  86. * Purpose:
  87. * Translate (space, object) -> (name, entry).
  88. * Only finds send/receive rights.
  89. * Returns TRUE if an entry is found; if so,
  90. * the object is locked and active.
  91. * Conditions:
  92. * The space must be locked (read or write) and active.
  93. * Nothing else locked.
  94. */
  95. boolean_t
  96. ipc_right_reverse(
  97. ipc_space_t space,
  98. ipc_object_t object,
  99. mach_port_t *namep,
  100. ipc_entry_t *entryp)
  101. {
  102. ipc_port_t port;
  103. mach_port_t name;
  104. ipc_entry_t entry;
  105. /* would switch on io_otype to handle multiple types of object */
  106. assert(space->is_active);
  107. assert(io_otype(object) == IOT_PORT);
  108. port = (ipc_port_t) object;
  109. ip_lock(port);
  110. if (!ip_active(port)) {
  111. ip_unlock(port);
  112. return FALSE;
  113. }
  114. if (port->ip_receiver == space) {
  115. name = port->ip_receiver_name;
  116. assert(name != MACH_PORT_NULL);
  117. entry = ipc_entry_lookup(space, name);
  118. assert(entry != IE_NULL);
  119. assert(entry->ie_bits & MACH_PORT_TYPE_RECEIVE);
  120. assert(port == (ipc_port_t) entry->ie_object);
  121. *namep = name;
  122. *entryp = entry;
  123. return TRUE;
  124. }
  125. if (ipc_hash_lookup(space, (ipc_object_t) port, namep, entryp)) {
  126. assert((entry = *entryp) != IE_NULL);
  127. assert(IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_SEND);
  128. assert(port == (ipc_port_t) entry->ie_object);
  129. return TRUE;
  130. }
  131. ip_unlock(port);
  132. return FALSE;
  133. }
  134. /*
  135. * Routine: ipc_right_dnrequest
  136. * Purpose:
  137. * Make a dead-name request, returning the previously
  138. * registered send-once right. If notify is IP_NULL,
  139. * just cancels the previously registered request.
  140. *
  141. * This interacts with the IE_BITS_COMPAT, because they
  142. * both use ie_request. If this is a compat entry, then
  143. * previous always gets IP_NULL. If notify is IP_NULL,
  144. * then the entry remains a compat entry. Otherwise
  145. * the real dead-name request is registered and the entry
  146. * is no longer a compat entry.
  147. * Conditions:
  148. * Nothing locked. May allocate memory.
  149. * Only consumes/returns refs if successful.
  150. * Returns:
  151. * KERN_SUCCESS Made/canceled dead-name request.
  152. * KERN_INVALID_TASK The space is dead.
  153. * KERN_INVALID_NAME Name doesn't exist in space.
  154. * KERN_INVALID_RIGHT Name doesn't denote port/dead rights.
  155. * KERN_INVALID_ARGUMENT Name denotes dead name, but
  156. * immediate is FALSE or notify is IP_NULL.
  157. * KERN_UREFS_OVERFLOW Name denotes dead name, but
  158. * generating immediate notif. would overflow urefs.
  159. * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
  160. */
  161. kern_return_t
  162. ipc_right_dnrequest(
  163. ipc_space_t space,
  164. mach_port_t name,
  165. boolean_t immediate,
  166. ipc_port_t notify,
  167. ipc_port_t *previousp)
  168. {
  169. ipc_port_t previous;
  170. for (;;) {
  171. ipc_entry_t entry;
  172. ipc_entry_bits_t bits;
  173. kern_return_t kr;
  174. kr = ipc_right_lookup_write(space, name, &entry);
  175. if (kr != KERN_SUCCESS)
  176. return kr;
  177. /* space is write-locked and active */
  178. bits = entry->ie_bits;
  179. if (bits & MACH_PORT_TYPE_PORT_RIGHTS) {
  180. ipc_port_t port;
  181. ipc_port_request_index_t request;
  182. port = (ipc_port_t) entry->ie_object;
  183. assert(port != IP_NULL);
  184. if (!ipc_right_check(space, port, name, entry)) {
  185. /* port is locked and active */
  186. if (notify == IP_NULL) {
  187. previous = ipc_right_dncancel_macro(
  188. space, port, name, entry);
  189. ip_unlock(port);
  190. is_write_unlock(space);
  191. break;
  192. }
  193. /*
  194. * If a registered soright exists,
  195. * want to atomically switch with it.
  196. * If ipc_port_dncancel finds us a
  197. * soright, then the following
  198. * ipc_port_dnrequest will reuse
  199. * that slot, so we are guaranteed
  200. * not to unlock and retry.
  201. */
  202. previous = ipc_right_dncancel_macro(space,
  203. port, name, entry);
  204. kr = ipc_port_dnrequest(port, name, notify,
  205. &request);
  206. if (kr != KERN_SUCCESS) {
  207. assert(previous == IP_NULL);
  208. is_write_unlock(space);
  209. kr = ipc_port_dngrow(port);
  210. /* port is unlocked */
  211. if (kr != KERN_SUCCESS)
  212. return kr;
  213. continue;
  214. }
  215. assert(request != 0);
  216. ip_unlock(port);
  217. entry->ie_request = request;
  218. is_write_unlock(space);
  219. break;
  220. }
  221. bits = entry->ie_bits;
  222. assert(bits & MACH_PORT_TYPE_DEAD_NAME);
  223. }
  224. if ((bits & MACH_PORT_TYPE_DEAD_NAME) &&
  225. immediate && (notify != IP_NULL)) {
  226. mach_port_urefs_t urefs = IE_BITS_UREFS(bits);
  227. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME);
  228. assert(urefs > 0);
  229. if (MACH_PORT_UREFS_OVERFLOW(urefs, 1)) {
  230. is_write_unlock(space);
  231. return KERN_UREFS_OVERFLOW;
  232. }
  233. entry->ie_bits = bits + 1; /* increment urefs */
  234. is_write_unlock(space);
  235. ipc_notify_dead_name(notify, name);
  236. previous = IP_NULL;
  237. break;
  238. }
  239. is_write_unlock(space);
  240. if (bits & MACH_PORT_TYPE_PORT_OR_DEAD)
  241. return KERN_INVALID_ARGUMENT;
  242. else
  243. return KERN_INVALID_RIGHT;
  244. }
  245. *previousp = previous;
  246. return KERN_SUCCESS;
  247. }
  248. /*
  249. * Routine: ipc_right_dncancel
  250. * Purpose:
  251. * Cancel a dead-name request and return the send-once right.
  252. * Afterwards, entry->ie_request == 0.
  253. * Conditions:
  254. * The space must be write-locked; the port must be locked.
  255. * The port must be active; the space doesn't have to be.
  256. */
  257. ipc_port_t
  258. ipc_right_dncancel(
  259. ipc_space_t space,
  260. ipc_port_t port,
  261. mach_port_t name,
  262. ipc_entry_t entry)
  263. {
  264. ipc_port_t dnrequest;
  265. assert(ip_active(port));
  266. assert(port == (ipc_port_t) entry->ie_object);
  267. dnrequest = ipc_port_dncancel(port, name, entry->ie_request);
  268. entry->ie_request = 0;
  269. return dnrequest;
  270. }
  271. /*
  272. * Routine: ipc_right_inuse
  273. * Purpose:
  274. * Check if an entry is being used.
  275. * Returns TRUE if it is.
  276. * Conditions:
  277. * The space is write-locked and active.
  278. * It is unlocked if the entry is inuse.
  279. */
  280. boolean_t
  281. ipc_right_inuse(space, name, entry)
  282. ipc_space_t space;
  283. mach_port_t name;
  284. ipc_entry_t entry;
  285. {
  286. ipc_entry_bits_t bits = entry->ie_bits;
  287. if (IE_BITS_TYPE(bits) != MACH_PORT_TYPE_NONE) {
  288. is_write_unlock(space);
  289. return TRUE;
  290. }
  291. return FALSE;
  292. }
  293. /*
  294. * Routine: ipc_right_check
  295. * Purpose:
  296. * Check if the port has died. If it has,
  297. * clean up the entry and return TRUE.
  298. * Conditions:
  299. * The space is write-locked; the port is not locked.
  300. * If returns FALSE, the port is also locked and active.
  301. * Otherwise, entry is converted to a dead name, freeing
  302. * a reference to port.
  303. */
  304. boolean_t
  305. ipc_right_check(space, port, name, entry)
  306. ipc_space_t space;
  307. ipc_port_t port;
  308. mach_port_t name;
  309. ipc_entry_t entry;
  310. {
  311. ipc_entry_bits_t bits;
  312. assert(space->is_active);
  313. assert(port == (ipc_port_t) entry->ie_object);
  314. ip_lock(port);
  315. if (ip_active(port))
  316. return FALSE;
  317. ip_unlock(port);
  318. /* this was either a pure send right or a send-once right */
  319. bits = entry->ie_bits;
  320. assert((bits & MACH_PORT_TYPE_RECEIVE) == 0);
  321. assert(IE_BITS_UREFS(bits) > 0);
  322. if (bits & MACH_PORT_TYPE_SEND) {
  323. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND);
  324. /* clean up msg-accepted request */
  325. if (bits & IE_BITS_MAREQUEST) {
  326. bits &= ~IE_BITS_MAREQUEST;
  327. ipc_marequest_cancel(space, name);
  328. }
  329. ipc_hash_delete(space, (ipc_object_t) port, name, entry);
  330. } else {
  331. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_ONCE);
  332. assert(IE_BITS_UREFS(bits) == 1);
  333. assert((bits & IE_BITS_MAREQUEST) == 0);
  334. }
  335. ipc_port_release(port);
  336. /* convert entry to dead name */
  337. bits = (bits &~ IE_BITS_TYPE_MASK) | MACH_PORT_TYPE_DEAD_NAME;
  338. if (entry->ie_request != 0) {
  339. assert(IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX);
  340. entry->ie_request = 0;
  341. bits++; /* increment urefs */
  342. }
  343. entry->ie_bits = bits;
  344. entry->ie_object = IO_NULL;
  345. return TRUE;
  346. }
  347. /*
  348. * Routine: ipc_right_clean
  349. * Purpose:
  350. * Cleans up an entry in a dead space.
  351. * The entry isn't deallocated or removed
  352. * from reverse hash tables.
  353. * Conditions:
  354. * The space is dead and unlocked.
  355. */
  356. void
  357. ipc_right_clean(
  358. ipc_space_t space,
  359. mach_port_t name,
  360. ipc_entry_t entry)
  361. {
  362. ipc_entry_bits_t bits = entry->ie_bits;
  363. mach_port_type_t type = IE_BITS_TYPE(bits);
  364. assert(!space->is_active);
  365. /*
  366. * We can't clean up IE_BITS_MAREQUEST when the space is dead.
  367. * This is because ipc_marequest_destroy can't turn off
  368. * the bit if the space is dead. Hence, it might be on
  369. * even though the marequest has been destroyed. It's OK
  370. * not to cancel the marequest, because ipc_marequest_destroy
  371. * cancels for us if the space is dead.
  372. *
  373. * IE_BITS_COMPAT/ipc_right_dncancel doesn't have this
  374. * problem, because we check that the port is active. If
  375. * we didn't cancel IE_BITS_COMPAT, ipc_port_destroy
  376. * would still work, but dead space refs would accumulate
  377. * in ip_dnrequests. They would use up slots in
  378. * ip_dnrequests and keep the spaces from being freed.
  379. */
  380. switch (type) {
  381. case MACH_PORT_TYPE_DEAD_NAME:
  382. assert(entry->ie_request == 0);
  383. assert(entry->ie_object == IO_NULL);
  384. assert((bits & IE_BITS_MAREQUEST) == 0);
  385. break;
  386. case MACH_PORT_TYPE_PORT_SET: {
  387. ipc_pset_t pset = (ipc_pset_t) entry->ie_object;
  388. assert(entry->ie_request == 0);
  389. assert((bits & IE_BITS_MAREQUEST) == 0);
  390. assert(pset != IPS_NULL);
  391. ips_lock(pset);
  392. assert(ips_active(pset));
  393. ipc_pset_destroy(pset); /* consumes ref, unlocks */
  394. break;
  395. }
  396. case MACH_PORT_TYPE_SEND:
  397. case MACH_PORT_TYPE_RECEIVE:
  398. case MACH_PORT_TYPE_SEND_RECEIVE:
  399. case MACH_PORT_TYPE_SEND_ONCE: {
  400. ipc_port_t port = (ipc_port_t) entry->ie_object;
  401. ipc_port_t dnrequest;
  402. ipc_port_t nsrequest = IP_NULL;
  403. mach_port_mscount_t mscount = 0; /* '=0' to shut up lint */
  404. assert(port != IP_NULL);
  405. ip_lock(port);
  406. if (!ip_active(port)) {
  407. ip_release(port);
  408. ip_check_unlock(port);
  409. break;
  410. }
  411. dnrequest = ipc_right_dncancel_macro(space, port, name, entry);
  412. if (type & MACH_PORT_TYPE_SEND) {
  413. assert(port->ip_srights > 0);
  414. if (--port->ip_srights == 0) {
  415. nsrequest = port->ip_nsrequest;
  416. if (nsrequest != IP_NULL) {
  417. port->ip_nsrequest = IP_NULL;
  418. mscount = port->ip_mscount;
  419. }
  420. }
  421. }
  422. if (type & MACH_PORT_TYPE_RECEIVE) {
  423. assert(port->ip_receiver_name == name);
  424. assert(port->ip_receiver == space);
  425. ipc_port_clear_receiver(port);
  426. ipc_port_destroy(port); /* consumes our ref, unlocks */
  427. } else if (type & MACH_PORT_TYPE_SEND_ONCE) {
  428. assert(port->ip_sorights > 0);
  429. ip_unlock(port);
  430. ipc_notify_send_once(port); /* consumes our ref */
  431. } else {
  432. assert(port->ip_receiver != space);
  433. ip_release(port);
  434. ip_unlock(port); /* port is active */
  435. }
  436. if (nsrequest != IP_NULL)
  437. ipc_notify_no_senders(nsrequest, mscount);
  438. if (dnrequest != IP_NULL)
  439. ipc_notify_port_deleted(dnrequest, name);
  440. break;
  441. }
  442. default:
  443. #if MACH_ASSERT
  444. assert(!"ipc_right_clean: strange type");
  445. #else
  446. panic("ipc_right_clean: strange type");
  447. #endif
  448. }
  449. }
  450. /*
  451. * Routine: ipc_right_destroy
  452. * Purpose:
  453. * Destroys an entry in a space.
  454. * Conditions:
  455. * The space is write-locked, and is unlocked upon return.
  456. * The space must be active.
  457. * Returns:
  458. * KERN_SUCCESS The entry was destroyed.
  459. */
  460. kern_return_t
  461. ipc_right_destroy(
  462. ipc_space_t space,
  463. mach_port_t name,
  464. ipc_entry_t entry)
  465. {
  466. ipc_entry_bits_t bits = entry->ie_bits;
  467. mach_port_type_t type = IE_BITS_TYPE(bits);
  468. assert(space->is_active);
  469. switch (type) {
  470. case MACH_PORT_TYPE_DEAD_NAME:
  471. assert(entry->ie_request == 0);
  472. assert(entry->ie_object == IO_NULL);
  473. assert((bits & IE_BITS_MAREQUEST) == 0);
  474. ipc_entry_dealloc(space, name, entry);
  475. is_write_unlock(space);
  476. break;
  477. case MACH_PORT_TYPE_PORT_SET: {
  478. ipc_pset_t pset = (ipc_pset_t) entry->ie_object;
  479. assert(entry->ie_request == 0);
  480. assert(pset != IPS_NULL);
  481. entry->ie_object = IO_NULL;
  482. ipc_entry_dealloc(space, name, entry);
  483. ips_lock(pset);
  484. assert(ips_active(pset));
  485. is_write_unlock(space);
  486. ipc_pset_destroy(pset); /* consumes ref, unlocks */
  487. break;
  488. }
  489. case MACH_PORT_TYPE_SEND:
  490. case MACH_PORT_TYPE_RECEIVE:
  491. case MACH_PORT_TYPE_SEND_RECEIVE:
  492. case MACH_PORT_TYPE_SEND_ONCE: {
  493. ipc_port_t port = (ipc_port_t) entry->ie_object;
  494. ipc_port_t nsrequest = IP_NULL;
  495. mach_port_mscount_t mscount = 0; /* '=0' to shut up lint */
  496. ipc_port_t dnrequest;
  497. assert(port != IP_NULL);
  498. if (bits & IE_BITS_MAREQUEST) {
  499. assert(type & MACH_PORT_TYPE_SEND_RECEIVE);
  500. ipc_marequest_cancel(space, name);
  501. }
  502. if (type == MACH_PORT_TYPE_SEND)
  503. ipc_hash_delete(space, (ipc_object_t) port,
  504. name, entry);
  505. ip_lock(port);
  506. if (!ip_active(port)) {
  507. assert((type & MACH_PORT_TYPE_RECEIVE) == 0);
  508. ip_release(port);
  509. ip_check_unlock(port);
  510. entry->ie_request = 0;
  511. entry->ie_object = IO_NULL;
  512. ipc_entry_dealloc(space, name, entry);
  513. is_write_unlock(space);
  514. break;
  515. }
  516. dnrequest = ipc_right_dncancel_macro(space, port, name, entry);
  517. entry->ie_object = IO_NULL;
  518. ipc_entry_dealloc(space, name, entry);
  519. is_write_unlock(space);
  520. if (type & MACH_PORT_TYPE_SEND) {
  521. assert(port->ip_srights > 0);
  522. if (--port->ip_srights == 0) {
  523. nsrequest = port->ip_nsrequest;
  524. if (nsrequest != IP_NULL) {
  525. port->ip_nsrequest = IP_NULL;
  526. mscount = port->ip_mscount;
  527. }
  528. }
  529. }
  530. if (type & MACH_PORT_TYPE_RECEIVE) {
  531. assert(ip_active(port));
  532. assert(port->ip_receiver == space);
  533. ipc_port_clear_receiver(port);
  534. ipc_port_destroy(port); /* consumes our ref, unlocks */
  535. } else if (type & MACH_PORT_TYPE_SEND_ONCE) {
  536. assert(port->ip_sorights > 0);
  537. ip_unlock(port);
  538. ipc_notify_send_once(port); /* consumes our ref */
  539. } else {
  540. assert(port->ip_receiver != space);
  541. ip_release(port);
  542. ip_unlock(port);
  543. }
  544. if (nsrequest != IP_NULL)
  545. ipc_notify_no_senders(nsrequest, mscount);
  546. if (dnrequest != IP_NULL)
  547. ipc_notify_port_deleted(dnrequest, name);
  548. break;
  549. }
  550. default:
  551. #if MACH_ASSERT
  552. assert(!"ipc_right_destroy: strange type");
  553. #else
  554. panic("ipc_right_destroy: strange type");
  555. #endif
  556. }
  557. return KERN_SUCCESS;
  558. }
  559. /*
  560. * Routine: ipc_right_dealloc
  561. * Purpose:
  562. * Releases a send/send-once/dead-name user ref.
  563. * Like ipc_right_delta with a delta of -1,
  564. * but looks at the entry to determine the right.
  565. * Conditions:
  566. * The space is write-locked, and is unlocked upon return.
  567. * The space must be active.
  568. * Returns:
  569. * KERN_SUCCESS A user ref was released.
  570. * KERN_INVALID_RIGHT Entry has wrong type.
  571. */
  572. kern_return_t
  573. ipc_right_dealloc(space, name, entry)
  574. ipc_space_t space;
  575. mach_port_t name;
  576. ipc_entry_t entry;
  577. {
  578. ipc_entry_bits_t bits = entry->ie_bits;
  579. mach_port_type_t type = IE_BITS_TYPE(bits);
  580. assert(space->is_active);
  581. switch (type) {
  582. case MACH_PORT_TYPE_DEAD_NAME: {
  583. dead_name:
  584. assert(IE_BITS_UREFS(bits) > 0);
  585. assert(entry->ie_request == 0);
  586. assert(entry->ie_object == IO_NULL);
  587. assert((bits & IE_BITS_MAREQUEST) == 0);
  588. if (IE_BITS_UREFS(bits) == 1)
  589. ipc_entry_dealloc(space, name, entry);
  590. else
  591. entry->ie_bits = bits-1; /* decrement urefs */
  592. is_write_unlock(space);
  593. break;
  594. }
  595. case MACH_PORT_TYPE_SEND_ONCE: {
  596. ipc_port_t port, dnrequest;
  597. assert(IE_BITS_UREFS(bits) == 1);
  598. assert((bits & IE_BITS_MAREQUEST) == 0);
  599. port = (ipc_port_t) entry->ie_object;
  600. assert(port != IP_NULL);
  601. if (ipc_right_check(space, port, name, entry)) {
  602. bits = entry->ie_bits;
  603. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME);
  604. goto dead_name;
  605. }
  606. /* port is locked and active */
  607. assert(port->ip_sorights > 0);
  608. dnrequest = ipc_right_dncancel_macro(space, port, name, entry);
  609. ip_unlock(port);
  610. entry->ie_object = IO_NULL;
  611. ipc_entry_dealloc(space, name, entry);
  612. is_write_unlock(space);
  613. ipc_notify_send_once(port);
  614. if (dnrequest != IP_NULL)
  615. ipc_notify_port_deleted(dnrequest, name);
  616. break;
  617. }
  618. case MACH_PORT_TYPE_SEND: {
  619. ipc_port_t port;
  620. ipc_port_t dnrequest = IP_NULL;
  621. ipc_port_t nsrequest = IP_NULL;
  622. mach_port_mscount_t mscount = 0; /* '=0' to shut up lint */
  623. assert(IE_BITS_UREFS(bits) > 0);
  624. port = (ipc_port_t) entry->ie_object;
  625. assert(port != IP_NULL);
  626. if (ipc_right_check(space, port, name, entry)) {
  627. bits = entry->ie_bits;
  628. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME);
  629. goto dead_name;
  630. }
  631. /* port is locked and active */
  632. assert(port->ip_srights > 0);
  633. if (IE_BITS_UREFS(bits) == 1) {
  634. if (--port->ip_srights == 0) {
  635. nsrequest = port->ip_nsrequest;
  636. if (nsrequest != IP_NULL) {
  637. port->ip_nsrequest = IP_NULL;
  638. mscount = port->ip_mscount;
  639. }
  640. }
  641. dnrequest = ipc_right_dncancel_macro(space, port,
  642. name, entry);
  643. ipc_hash_delete(space, (ipc_object_t) port,
  644. name, entry);
  645. if (bits & IE_BITS_MAREQUEST)
  646. ipc_marequest_cancel(space, name);
  647. ip_release(port);
  648. entry->ie_object = IO_NULL;
  649. ipc_entry_dealloc(space, name, entry);
  650. } else
  651. entry->ie_bits = bits-1; /* decrement urefs */
  652. ip_unlock(port); /* even if dropped a ref, port is active */
  653. is_write_unlock(space);
  654. if (nsrequest != IP_NULL)
  655. ipc_notify_no_senders(nsrequest, mscount);
  656. if (dnrequest != IP_NULL)
  657. ipc_notify_port_deleted(dnrequest, name);
  658. break;
  659. }
  660. case MACH_PORT_TYPE_SEND_RECEIVE: {
  661. ipc_port_t port;
  662. ipc_port_t nsrequest = IP_NULL;
  663. mach_port_mscount_t mscount = 0; /* '=0' to shut up lint */
  664. assert(IE_BITS_UREFS(bits) > 0);
  665. port = (ipc_port_t) entry->ie_object;
  666. assert(port != IP_NULL);
  667. ip_lock(port);
  668. assert(ip_active(port));
  669. assert(port->ip_receiver_name == name);
  670. assert(port->ip_receiver == space);
  671. assert(port->ip_srights > 0);
  672. if (IE_BITS_UREFS(bits) == 1) {
  673. if (--port->ip_srights == 0) {
  674. nsrequest = port->ip_nsrequest;
  675. if (nsrequest != IP_NULL) {
  676. port->ip_nsrequest = IP_NULL;
  677. mscount = port->ip_mscount;
  678. }
  679. }
  680. entry->ie_bits = bits &~ (IE_BITS_UREFS_MASK|
  681. MACH_PORT_TYPE_SEND);
  682. } else
  683. entry->ie_bits = bits-1; /* decrement urefs */
  684. ip_unlock(port);
  685. is_write_unlock(space);
  686. if (nsrequest != IP_NULL)
  687. ipc_notify_no_senders(nsrequest, mscount);
  688. break;
  689. }
  690. default:
  691. is_write_unlock(space);
  692. return KERN_INVALID_RIGHT;
  693. }
  694. return KERN_SUCCESS;
  695. }
  696. /*
  697. * Routine: ipc_right_delta
  698. * Purpose:
  699. * Modifies the user-reference count for a right.
  700. * May deallocate the right, if the count goes to zero.
  701. * Conditions:
  702. * The space is write-locked, and is unlocked upon return.
  703. * The space must be active.
  704. * Returns:
  705. * KERN_SUCCESS Count was modified.
  706. * KERN_INVALID_RIGHT Entry has wrong type.
  707. * KERN_INVALID_VALUE Bad delta for the right.
  708. * KERN_UREFS_OVERFLOW OK delta, except would overflow.
  709. */
  710. kern_return_t
  711. ipc_right_delta(space, name, entry, right, delta)
  712. ipc_space_t space;
  713. mach_port_t name;
  714. ipc_entry_t entry;
  715. mach_port_right_t right;
  716. mach_port_delta_t delta;
  717. {
  718. ipc_entry_bits_t bits = entry->ie_bits;
  719. assert(space->is_active);
  720. assert(right < MACH_PORT_RIGHT_NUMBER);
  721. /* Rights-specific restrictions and operations. */
  722. switch (right) {
  723. case MACH_PORT_RIGHT_PORT_SET: {
  724. ipc_pset_t pset;
  725. if ((bits & MACH_PORT_TYPE_PORT_SET) == 0)
  726. goto invalid_right;
  727. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_PORT_SET);
  728. assert(IE_BITS_UREFS(bits) == 0);
  729. assert((bits & IE_BITS_MAREQUEST) == 0);
  730. assert(entry->ie_request == 0);
  731. if (delta == 0)
  732. goto success;
  733. if (delta != -1)
  734. goto invalid_value;
  735. pset = (ipc_pset_t) entry->ie_object;
  736. assert(pset != IPS_NULL);
  737. entry->ie_object = IO_NULL;
  738. ipc_entry_dealloc(space, name, entry);
  739. ips_lock(pset);
  740. assert(ips_active(pset));
  741. is_write_unlock(space);
  742. ipc_pset_destroy(pset); /* consumes ref, unlocks */
  743. break;
  744. }
  745. case MACH_PORT_RIGHT_RECEIVE: {
  746. ipc_port_t port;
  747. ipc_port_t dnrequest = IP_NULL;
  748. if ((bits & MACH_PORT_TYPE_RECEIVE) == 0)
  749. goto invalid_right;
  750. if (delta == 0)
  751. goto success;
  752. if (delta != -1)
  753. goto invalid_value;
  754. if (bits & IE_BITS_MAREQUEST) {
  755. bits &= ~IE_BITS_MAREQUEST;
  756. ipc_marequest_cancel(space, name);
  757. }
  758. port = (ipc_port_t) entry->ie_object;
  759. assert(port != IP_NULL);
  760. /*
  761. * The port lock is needed for ipc_right_dncancel;
  762. * otherwise, we wouldn't have to take the lock
  763. * until just before dropping the space lock.
  764. */
  765. ip_lock(port);
  766. assert(ip_active(port));
  767. assert(port->ip_receiver_name == name);
  768. assert(port->ip_receiver == space);
  769. if (bits & MACH_PORT_TYPE_SEND) {
  770. assert(IE_BITS_TYPE(bits) ==
  771. MACH_PORT_TYPE_SEND_RECEIVE);
  772. assert(IE_BITS_UREFS(bits) > 0);
  773. assert(IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX);
  774. assert(port->ip_srights > 0);
  775. /*
  776. * The remaining send right turns into a
  777. * dead name. Notice we don't decrement
  778. * ip_srights, generate a no-senders notif,
  779. * or use ipc_right_dncancel, because the
  780. * port is destroyed "first".
  781. */
  782. bits &= ~IE_BITS_TYPE_MASK;
  783. bits |= MACH_PORT_TYPE_DEAD_NAME;
  784. if (entry->ie_request != 0) {
  785. entry->ie_request = 0;
  786. bits++; /* increment urefs */
  787. }
  788. entry->ie_bits = bits;
  789. entry->ie_object = IO_NULL;
  790. } else {
  791. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_RECEIVE);
  792. assert(IE_BITS_UREFS(bits) == 0);
  793. dnrequest = ipc_right_dncancel_macro(space, port,
  794. name, entry);
  795. entry->ie_object = IO_NULL;
  796. ipc_entry_dealloc(space, name, entry);
  797. }
  798. is_write_unlock(space);
  799. ipc_port_clear_receiver(port);
  800. ipc_port_destroy(port); /* consumes ref, unlocks */
  801. if (dnrequest != IP_NULL)
  802. ipc_notify_port_deleted(dnrequest, name);
  803. break;
  804. }
  805. case MACH_PORT_RIGHT_SEND_ONCE: {
  806. ipc_port_t port, dnrequest;
  807. if ((bits & MACH_PORT_TYPE_SEND_ONCE) == 0)
  808. goto invalid_right;
  809. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_ONCE);
  810. assert(IE_BITS_UREFS(bits) == 1);
  811. assert((bits & IE_BITS_MAREQUEST) == 0);
  812. if ((delta > 0) || (delta < -1))
  813. goto invalid_value;
  814. port = (ipc_port_t) entry->ie_object;
  815. assert(port != IP_NULL);
  816. if (ipc_right_check(space, port, name, entry)) {
  817. assert(!(entry->ie_bits & MACH_PORT_TYPE_SEND_ONCE));
  818. goto invalid_right;
  819. }
  820. /* port is locked and active */
  821. assert(port->ip_sorights > 0);
  822. if (delta == 0) {
  823. ip_unlock(port);
  824. goto success;
  825. }
  826. dnrequest = ipc_right_dncancel_macro(space, port, name, entry);
  827. ip_unlock(port);
  828. entry->ie_object = IO_NULL;
  829. ipc_entry_dealloc(space, name, entry);
  830. is_write_unlock(space);
  831. ipc_notify_send_once(port);
  832. if (dnrequest != IP_NULL)
  833. ipc_notify_port_deleted(dnrequest, name);
  834. break;
  835. }
  836. case MACH_PORT_RIGHT_DEAD_NAME: {
  837. mach_port_urefs_t urefs;
  838. if (bits & MACH_PORT_TYPE_SEND_RIGHTS) {
  839. ipc_port_t port;
  840. port = (ipc_port_t) entry->ie_object;
  841. assert(port != IP_NULL);
  842. if (!ipc_right_check(space, port, name, entry)) {
  843. /* port is locked and active */
  844. ip_unlock(port);
  845. goto invalid_right;
  846. }
  847. bits = entry->ie_bits;
  848. } else if ((bits & MACH_PORT_TYPE_DEAD_NAME) == 0)
  849. goto invalid_right;
  850. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME);
  851. assert(IE_BITS_UREFS(bits) > 0);
  852. assert((bits & IE_BITS_MAREQUEST) == 0);
  853. assert(entry->ie_object == IO_NULL);
  854. assert(entry->ie_request == 0);
  855. urefs = IE_BITS_UREFS(bits);
  856. if (MACH_PORT_UREFS_UNDERFLOW(urefs, delta))
  857. goto invalid_value;
  858. if (MACH_PORT_UREFS_OVERFLOW(urefs, delta))
  859. goto urefs_overflow;
  860. if ((urefs + delta) == 0)
  861. ipc_entry_dealloc(space, name, entry);
  862. else
  863. entry->ie_bits = bits + delta;
  864. is_write_unlock(space);
  865. break;
  866. }
  867. case MACH_PORT_RIGHT_SEND: {
  868. mach_port_urefs_t urefs;
  869. ipc_port_t port;
  870. ipc_port_t dnrequest = IP_NULL;
  871. ipc_port_t nsrequest = IP_NULL;
  872. mach_port_mscount_t mscount = 0; /* '=0' to shut up lint */
  873. if ((bits & MACH_PORT_TYPE_SEND) == 0)
  874. goto invalid_right;
  875. /* maximum urefs for send is MACH_PORT_UREFS_MAX-1 */
  876. urefs = IE_BITS_UREFS(bits);
  877. if (MACH_PORT_UREFS_UNDERFLOW(urefs, delta))
  878. goto invalid_value;
  879. if (MACH_PORT_UREFS_OVERFLOW(urefs+1, delta))
  880. goto urefs_overflow;
  881. port = (ipc_port_t) entry->ie_object;
  882. assert(port != IP_NULL);
  883. if (ipc_right_check(space, port, name, entry)) {
  884. assert((entry->ie_bits & MACH_PORT_TYPE_SEND) == 0);
  885. goto invalid_right;
  886. }
  887. /* port is locked and active */
  888. assert(port->ip_srights > 0);
  889. if ((urefs + delta) == 0) {
  890. if (--port->ip_srights == 0) {
  891. nsrequest = port->ip_nsrequest;
  892. if (nsrequest != IP_NULL) {
  893. port->ip_nsrequest = IP_NULL;
  894. mscount = port->ip_mscount;
  895. }
  896. }
  897. if (bits & MACH_PORT_TYPE_RECEIVE) {
  898. assert(port->ip_receiver_name == name);
  899. assert(port->ip_receiver == space);
  900. assert(IE_BITS_TYPE(bits) ==
  901. MACH_PORT_TYPE_SEND_RECEIVE);
  902. entry->ie_bits = bits &~ (IE_BITS_UREFS_MASK|
  903. MACH_PORT_TYPE_SEND);
  904. } else {
  905. assert(IE_BITS_TYPE(bits) ==
  906. MACH_PORT_TYPE_SEND);
  907. dnrequest = ipc_right_dncancel_macro(
  908. space, port, name, entry);
  909. ipc_hash_delete(space, (ipc_object_t) port,
  910. name, entry);
  911. if (bits & IE_BITS_MAREQUEST)
  912. ipc_marequest_cancel(space, name);
  913. ip_release(port);
  914. entry->ie_object = IO_NULL;
  915. ipc_entry_dealloc(space, name, entry);
  916. }
  917. } else
  918. entry->ie_bits = bits + delta;
  919. ip_unlock(port); /* even if dropped a ref, port is active */
  920. is_write_unlock(space);
  921. if (nsrequest != IP_NULL)
  922. ipc_notify_no_senders(nsrequest, mscount);
  923. if (dnrequest != IP_NULL)
  924. ipc_notify_port_deleted(dnrequest, name);
  925. break;
  926. }
  927. default:
  928. #if MACH_ASSERT
  929. assert(!"ipc_right_delta: strange right");
  930. #else
  931. panic("ipc_right_delta: strange right");
  932. #endif
  933. }
  934. return KERN_SUCCESS;
  935. success:
  936. is_write_unlock(space);
  937. return KERN_SUCCESS;
  938. invalid_right:
  939. is_write_unlock(space);
  940. return KERN_INVALID_RIGHT;
  941. invalid_value:
  942. is_write_unlock(space);
  943. return KERN_INVALID_VALUE;
  944. urefs_overflow:
  945. is_write_unlock(space);
  946. return KERN_UREFS_OVERFLOW;
  947. }
  948. /*
  949. * Routine: ipc_right_info
  950. * Purpose:
  951. * Retrieves information about the right.
  952. * Conditions:
  953. * The space is write-locked, and is unlocked upon return
  954. * if the call is unsuccessful. The space must be active.
  955. * Returns:
  956. * KERN_SUCCESS Retrieved info; space still locked.
  957. */
  958. kern_return_t
  959. ipc_right_info(
  960. ipc_space_t space,
  961. mach_port_t name,
  962. ipc_entry_t entry,
  963. mach_port_type_t *typep,
  964. mach_port_urefs_t *urefsp)
  965. {
  966. ipc_entry_bits_t bits = entry->ie_bits;
  967. ipc_port_request_index_t request;
  968. mach_port_type_t type;
  969. if (bits & MACH_PORT_TYPE_SEND_RIGHTS) {
  970. ipc_port_t port = (ipc_port_t) entry->ie_object;
  971. if (ipc_right_check(space, port, name, entry)) {
  972. bits = entry->ie_bits;
  973. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME);
  974. } else
  975. ip_unlock(port);
  976. }
  977. type = IE_BITS_TYPE(bits);
  978. request = entry->ie_request;
  979. if (request != 0)
  980. type |= MACH_PORT_TYPE_DNREQUEST;
  981. if (bits & IE_BITS_MAREQUEST)
  982. type |= MACH_PORT_TYPE_MAREQUEST;
  983. *typep = type;
  984. *urefsp = IE_BITS_UREFS(bits);
  985. return KERN_SUCCESS;
  986. }
  987. /*
  988. * Routine: ipc_right_copyin_check
  989. * Purpose:
  990. * Check if a subsequent ipc_right_copyin would succeed.
  991. * Conditions:
  992. * The space is locked (read or write) and active.
  993. */
  994. boolean_t
  995. ipc_right_copyin_check(
  996. ipc_space_t space,
  997. mach_port_t name,
  998. ipc_entry_t entry,
  999. mach_msg_type_name_t msgt_name)
  1000. {
  1001. ipc_entry_bits_t bits = entry->ie_bits;
  1002. assert(space->is_active);
  1003. switch (msgt_name) {
  1004. case MACH_MSG_TYPE_MAKE_SEND:
  1005. case MACH_MSG_TYPE_MAKE_SEND_ONCE:
  1006. case MACH_MSG_TYPE_MOVE_RECEIVE:
  1007. if ((bits & MACH_PORT_TYPE_RECEIVE) == 0)
  1008. return FALSE;
  1009. break;
  1010. case MACH_MSG_TYPE_COPY_SEND:
  1011. case MACH_MSG_TYPE_MOVE_SEND:
  1012. case MACH_MSG_TYPE_MOVE_SEND_ONCE: {
  1013. ipc_port_t port;
  1014. boolean_t active;
  1015. if (bits & MACH_PORT_TYPE_DEAD_NAME)
  1016. break;
  1017. if ((bits & MACH_PORT_TYPE_SEND_RIGHTS) == 0)
  1018. return FALSE;
  1019. port = (ipc_port_t) entry->ie_object;
  1020. assert(port != IP_NULL);
  1021. ip_lock(port);
  1022. active = ip_active(port);
  1023. ip_unlock(port);
  1024. if (!active) {
  1025. break;
  1026. }
  1027. if (msgt_name == MACH_MSG_TYPE_MOVE_SEND_ONCE) {
  1028. if ((bits & MACH_PORT_TYPE_SEND_ONCE) == 0)
  1029. return FALSE;
  1030. } else {
  1031. if ((bits & MACH_PORT_TYPE_SEND) == 0)
  1032. return FALSE;
  1033. }
  1034. break;
  1035. }
  1036. default:
  1037. #if MACH_ASSERT
  1038. assert(!"ipc_right_copyin_check: strange rights");
  1039. #else
  1040. panic("ipc_right_copyin_check: strange rights");
  1041. #endif
  1042. }
  1043. return TRUE;
  1044. }
  1045. /*
  1046. * Routine: ipc_right_copyin
  1047. * Purpose:
  1048. * Copyin a capability from a space.
  1049. * If successful, the caller gets a ref
  1050. * for the resulting object, unless it is IO_DEAD,
  1051. * and possibly a send-once right which should
  1052. * be used in a port-deleted notification.
  1053. *
  1054. * If deadok is not TRUE, the copyin operation
  1055. * will fail instead of producing IO_DEAD.
  1056. *
  1057. * The entry is never deallocated (except
  1058. * when KERN_INVALID_NAME), so the caller
  1059. * should deallocate the entry if its type
  1060. * is MACH_PORT_TYPE_NONE.
  1061. * Conditions:
  1062. * The space is write-locked and active.
  1063. * Returns:
  1064. * KERN_SUCCESS Acquired an object, possibly IO_DEAD.
  1065. * KERN_INVALID_RIGHT Name doesn't denote correct right.
  1066. */
  1067. kern_return_t
  1068. ipc_right_copyin(
  1069. ipc_space_t space,
  1070. mach_port_t name,
  1071. ipc_entry_t entry,
  1072. mach_msg_type_name_t msgt_name,
  1073. boolean_t deadok,
  1074. ipc_object_t *objectp,
  1075. ipc_port_t *sorightp)
  1076. {
  1077. ipc_entry_bits_t bits = entry->ie_bits;
  1078. assert(space->is_active);
  1079. switch (msgt_name) {
  1080. case MACH_MSG_TYPE_MAKE_SEND: {
  1081. ipc_port_t port;
  1082. if ((bits & MACH_PORT_TYPE_RECEIVE) == 0)
  1083. goto invalid_right;
  1084. port = (ipc_port_t) entry->ie_object;
  1085. assert(port != IP_NULL);
  1086. ip_lock(port);
  1087. assert(ip_active(port));
  1088. assert(port->ip_receiver_name == name);
  1089. assert(port->ip_receiver == space);
  1090. port->ip_mscount++;
  1091. port->ip_srights++;
  1092. ip_reference(port);
  1093. ip_unlock(port);
  1094. *objectp = (ipc_object_t) port;
  1095. *sorightp = IP_NULL;
  1096. break;
  1097. }
  1098. case MACH_MSG_TYPE_MAKE_SEND_ONCE: {
  1099. ipc_port_t port;
  1100. if ((bits & MACH_PORT_TYPE_RECEIVE) == 0)
  1101. goto invalid_right;
  1102. port = (ipc_port_t) entry->ie_object;
  1103. assert(port != IP_NULL);
  1104. ip_lock(port);
  1105. assert(ip_active(port));
  1106. assert(port->ip_receiver_name == name);
  1107. assert(port->ip_receiver == space);
  1108. port->ip_sorights++;
  1109. ip_reference(port);
  1110. ip_unlock(port);
  1111. *objectp = (ipc_object_t) port;
  1112. *sorightp = IP_NULL;
  1113. break;
  1114. }
  1115. case MACH_MSG_TYPE_MOVE_RECEIVE: {
  1116. ipc_port_t port;
  1117. ipc_port_t dnrequest = IP_NULL;
  1118. if ((bits & MACH_PORT_TYPE_RECEIVE) == 0)
  1119. goto invalid_right;
  1120. port = (ipc_port_t) entry->ie_object;
  1121. assert(port != IP_NULL);
  1122. ip_lock(port);
  1123. assert(ip_active(port));
  1124. assert(port->ip_receiver_name == name);
  1125. assert(port->ip_receiver == space);
  1126. if (bits & MACH_PORT_TYPE_SEND) {
  1127. assert(IE_BITS_TYPE(bits) ==
  1128. MACH_PORT_TYPE_SEND_RECEIVE);
  1129. assert(IE_BITS_UREFS(bits) > 0);
  1130. assert(port->ip_srights > 0);
  1131. ipc_hash_insert(space, (ipc_object_t) port,
  1132. name, entry);
  1133. ip_reference(port);
  1134. } else {
  1135. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_RECEIVE);
  1136. assert(IE_BITS_UREFS(bits) == 0);
  1137. dnrequest = ipc_right_dncancel_macro(space, port,
  1138. name, entry);
  1139. if (bits & IE_BITS_MAREQUEST)
  1140. ipc_marequest_cancel(space, name);
  1141. entry->ie_object = IO_NULL;
  1142. }
  1143. entry->ie_bits = bits &~ MACH_PORT_TYPE_RECEIVE;
  1144. ipc_port_clear_receiver(port);
  1145. port->ip_receiver_name = MACH_PORT_NULL;
  1146. port->ip_destination = IP_NULL;
  1147. ip_unlock(port);
  1148. *objectp = (ipc_object_t) port;
  1149. *sorightp = dnrequest;
  1150. break;
  1151. }
  1152. case MACH_MSG_TYPE_COPY_SEND: {
  1153. ipc_port_t port;
  1154. if (bits & MACH_PORT_TYPE_DEAD_NAME)
  1155. goto copy_dead;
  1156. /* allow for dead send-once rights */
  1157. if ((bits & MACH_PORT_TYPE_SEND_RIGHTS) == 0)
  1158. goto invalid_right;
  1159. assert(IE_BITS_UREFS(bits) > 0);
  1160. port = (ipc_port_t) entry->ie_object;
  1161. assert(port != IP_NULL);
  1162. if (ipc_right_check(space, port, name, entry)) {
  1163. bits = entry->ie_bits;
  1164. goto copy_dead;
  1165. }
  1166. /* port is locked and active */
  1167. if ((bits & MACH_PORT_TYPE_SEND) == 0) {
  1168. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_ONCE);
  1169. assert(port->ip_sorights > 0);
  1170. ip_unlock(port);
  1171. goto invalid_right;
  1172. }
  1173. assert(port->ip_srights > 0);
  1174. port->ip_srights++;
  1175. ip_reference(port);
  1176. ip_unlock(port);
  1177. *objectp = (ipc_object_t) port;
  1178. *sorightp = IP_NULL;
  1179. break;
  1180. }
  1181. case MACH_MSG_TYPE_MOVE_SEND: {
  1182. ipc_port_t port;
  1183. ipc_port_t dnrequest = IP_NULL;
  1184. if (bits & MACH_PORT_TYPE_DEAD_NAME)
  1185. goto move_dead;
  1186. /* allow for dead send-once rights */
  1187. if ((bits & MACH_PORT_TYPE_SEND_RIGHTS) == 0)
  1188. goto invalid_right;
  1189. assert(IE_BITS_UREFS(bits) > 0);
  1190. port = (ipc_port_t) entry->ie_object;
  1191. assert(port != IP_NULL);
  1192. if (ipc_right_check(space, port, name, entry)) {
  1193. bits = entry->ie_bits;
  1194. goto move_dead;
  1195. }
  1196. /* port is locked and active */
  1197. if ((bits & MACH_PORT_TYPE_SEND) == 0) {
  1198. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_ONCE);
  1199. assert(port->ip_sorights > 0);
  1200. ip_unlock(port);
  1201. goto invalid_right;
  1202. }
  1203. assert(port->ip_srights > 0);
  1204. if (IE_BITS_UREFS(bits) == 1) {
  1205. if (bits & MACH_PORT_TYPE_RECEIVE) {
  1206. assert(port->ip_receiver_name == name);
  1207. assert(port->ip_receiver == space);
  1208. assert(IE_BITS_TYPE(bits) ==
  1209. MACH_PORT_TYPE_SEND_RECEIVE);
  1210. ip_reference(port);
  1211. } else {
  1212. assert(IE_BITS_TYPE(bits) ==
  1213. MACH_PORT_TYPE_SEND);
  1214. dnrequest = ipc_right_dncancel_macro(
  1215. space, port, name, entry);
  1216. ipc_hash_delete(space, (ipc_object_t) port,
  1217. name, entry);
  1218. if (bits & IE_BITS_MAREQUEST)
  1219. ipc_marequest_cancel(space, name);
  1220. entry->ie_object = IO_NULL;
  1221. }
  1222. entry->ie_bits = bits &~
  1223. (IE_BITS_UREFS_MASK|MACH_PORT_TYPE_SEND);
  1224. } else {
  1225. port->ip_srights++;
  1226. ip_reference(port);
  1227. entry->ie_bits = bits-1; /* decrement urefs */
  1228. }
  1229. ip_unlock(port);
  1230. *objectp = (ipc_object_t) port;
  1231. *sorightp = dnrequest;
  1232. break;
  1233. }
  1234. case MACH_MSG_TYPE_MOVE_SEND_ONCE: {
  1235. ipc_port_t port;
  1236. ipc_port_t dnrequest;
  1237. if (bits & MACH_PORT_TYPE_DEAD_NAME)
  1238. goto move_dead;
  1239. /* allow for dead send rights */
  1240. if ((bits & MACH_PORT_TYPE_SEND_RIGHTS) == 0)
  1241. goto invalid_right;
  1242. assert(IE_BITS_UREFS(bits) > 0);
  1243. port = (ipc_port_t) entry->ie_object;
  1244. assert(port != IP_NULL);
  1245. if (ipc_right_check(space, port, name, entry)) {
  1246. bits = entry->ie_bits;
  1247. goto move_dead;
  1248. }
  1249. /* port is locked and active */
  1250. if ((bits & MACH_PORT_TYPE_SEND_ONCE) == 0) {
  1251. assert(bits & MACH_PORT_TYPE_SEND);
  1252. assert(port->ip_srights > 0);
  1253. ip_unlock(port);
  1254. goto invalid_right;
  1255. }
  1256. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_ONCE);
  1257. assert(IE_BITS_UREFS(bits) == 1);
  1258. assert((bits & IE_BITS_MAREQUEST) == 0);
  1259. assert(port->ip_sorights > 0);
  1260. dnrequest = ipc_right_dncancel_macro(space, port, name, entry);
  1261. ip_unlock(port);
  1262. entry->ie_object = IO_NULL;
  1263. entry->ie_bits = bits &~ MACH_PORT_TYPE_SEND_ONCE;
  1264. *objectp = (ipc_object_t) port;
  1265. *sorightp = dnrequest;
  1266. break;
  1267. }
  1268. default:
  1269. #if MACH_ASSERT
  1270. assert(!"ipc_right_copyin: strange rights");
  1271. #else
  1272. panic("ipc_right_copyin: strange rights");
  1273. #endif
  1274. }
  1275. return KERN_SUCCESS;
  1276. copy_dead:
  1277. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME);
  1278. assert(IE_BITS_UREFS(bits) > 0);
  1279. assert((bits & IE_BITS_MAREQUEST) == 0);
  1280. assert(entry->ie_request == 0);
  1281. assert(entry->ie_object == 0);
  1282. if (!deadok)
  1283. goto invalid_right;
  1284. *objectp = IO_DEAD;
  1285. *sorightp = IP_NULL;
  1286. return KERN_SUCCESS;
  1287. move_dead:
  1288. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME);
  1289. assert(IE_BITS_UREFS(bits) > 0);
  1290. assert((bits & IE_BITS_MAREQUEST) == 0);
  1291. assert(entry->ie_request == 0);
  1292. assert(entry->ie_object == 0);
  1293. if (!deadok)
  1294. goto invalid_right;
  1295. if (IE_BITS_UREFS(bits) == 1)
  1296. entry->ie_bits = bits &~ MACH_PORT_TYPE_DEAD_NAME;
  1297. else
  1298. entry->ie_bits = bits-1; /* decrement urefs */
  1299. *objectp = IO_DEAD;
  1300. *sorightp = IP_NULL;
  1301. return KERN_SUCCESS;
  1302. invalid_right:
  1303. return KERN_INVALID_RIGHT;
  1304. }
  1305. /*
  1306. * Routine: ipc_right_copyin_undo
  1307. * Purpose:
  1308. * Undoes the effects of an ipc_right_copyin
  1309. * of a send/send-once right that is dead.
  1310. * (Object is either IO_DEAD or a dead port.)
  1311. * Conditions:
  1312. * The space is write-locked and active.
  1313. */
  1314. void
  1315. ipc_right_copyin_undo(
  1316. ipc_space_t space,
  1317. mach_port_t name,
  1318. ipc_entry_t entry,
  1319. mach_msg_type_name_t msgt_name,
  1320. ipc_object_t object,
  1321. ipc_port_t soright)
  1322. {
  1323. ipc_entry_bits_t bits = entry->ie_bits;
  1324. assert(space->is_active);
  1325. assert((msgt_name == MACH_MSG_TYPE_MOVE_SEND) ||
  1326. (msgt_name == MACH_MSG_TYPE_COPY_SEND) ||
  1327. (msgt_name == MACH_MSG_TYPE_MOVE_SEND_ONCE));
  1328. if (soright != IP_NULL) {
  1329. assert((msgt_name == MACH_MSG_TYPE_MOVE_SEND) ||
  1330. (msgt_name == MACH_MSG_TYPE_MOVE_SEND_ONCE));
  1331. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_NONE);
  1332. assert(entry->ie_object == IO_NULL);
  1333. assert(object != IO_DEAD);
  1334. entry->ie_bits = ((bits &~ IE_BITS_RIGHT_MASK) |
  1335. MACH_PORT_TYPE_DEAD_NAME | 2);
  1336. } else if (IE_BITS_TYPE(bits) == MACH_PORT_TYPE_NONE) {
  1337. assert((msgt_name == MACH_MSG_TYPE_MOVE_SEND) ||
  1338. (msgt_name == MACH_MSG_TYPE_MOVE_SEND_ONCE));
  1339. assert(entry->ie_object == IO_NULL);
  1340. entry->ie_bits = ((bits &~ IE_BITS_RIGHT_MASK) |
  1341. MACH_PORT_TYPE_DEAD_NAME | 1);
  1342. } else if (IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME) {
  1343. assert(entry->ie_object == IO_NULL);
  1344. assert(object == IO_DEAD);
  1345. assert(IE_BITS_UREFS(bits) > 0);
  1346. if (msgt_name != MACH_MSG_TYPE_COPY_SEND) {
  1347. assert(IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX);
  1348. entry->ie_bits = bits+1; /* increment urefs */
  1349. }
  1350. } else {
  1351. assert((msgt_name == MACH_MSG_TYPE_MOVE_SEND) ||
  1352. (msgt_name == MACH_MSG_TYPE_COPY_SEND));
  1353. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND);
  1354. assert(object != IO_DEAD);
  1355. assert(entry->ie_object == object);
  1356. assert(IE_BITS_UREFS(bits) > 0);
  1357. if (msgt_name != MACH_MSG_TYPE_COPY_SEND) {
  1358. assert(IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX-1);
  1359. entry->ie_bits = bits+1; /* increment urefs */
  1360. }
  1361. /*
  1362. * May as well convert the entry to a dead name.
  1363. * (Or if it is a compat entry, destroy it.)
  1364. */
  1365. (void) ipc_right_check(space, (ipc_port_t) object,
  1366. name, entry);
  1367. /* object is dead so it is not locked */
  1368. }
  1369. /* release the reference acquired by copyin */
  1370. if (object != IO_DEAD)
  1371. ipc_object_release(object);
  1372. }
  1373. /*
  1374. * Routine: ipc_right_copyin_two
  1375. * Purpose:
  1376. * Like ipc_right_copyin with MACH_MSG_TYPE_MOVE_SEND
  1377. * and deadok == FALSE, except that this moves two
  1378. * send rights at once.
  1379. * Conditions:
  1380. * The space is write-locked and active.
  1381. * The object is returned with two refs/send rights.
  1382. * Returns:
  1383. * KERN_SUCCESS Acquired an object.
  1384. * KERN_INVALID_RIGHT Name doesn't denote correct right.
  1385. */
  1386. kern_return_t
  1387. ipc_right_copyin_two(
  1388. ipc_space_t space,
  1389. mach_port_t name,
  1390. ipc_entry_t entry,
  1391. ipc_object_t *objectp,
  1392. ipc_port_t *sorightp)
  1393. {
  1394. ipc_entry_bits_t bits = entry->ie_bits;
  1395. mach_port_urefs_t urefs;
  1396. ipc_port_t port;
  1397. ipc_port_t dnrequest = IP_NULL;
  1398. assert(space->is_active);
  1399. if ((bits & MACH_PORT_TYPE_SEND) == 0)
  1400. goto invalid_right;
  1401. urefs = IE_BITS_UREFS(bits);
  1402. if (urefs < 2)
  1403. goto invalid_right;
  1404. port = (ipc_port_t) entry->ie_object;
  1405. assert(port != IP_NULL);
  1406. if (ipc_right_check(space, port, name, entry)) {
  1407. goto invalid_right;
  1408. }
  1409. /* port is locked and active */
  1410. assert(port->ip_srights > 0);
  1411. if (urefs == 2) {
  1412. if (bits & MACH_PORT_TYPE_RECEIVE) {
  1413. assert(port->ip_receiver_name == name);
  1414. assert(port->ip_receiver == space);
  1415. assert(IE_BITS_TYPE(bits) ==
  1416. MACH_PORT_TYPE_SEND_RECEIVE);
  1417. port->ip_srights++;
  1418. ip_reference(port);
  1419. ip_reference(port);
  1420. } else {
  1421. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND);
  1422. dnrequest = ipc_right_dncancel_macro(space, port,
  1423. name, entry);
  1424. ipc_hash_delete(space, (ipc_object_t) port,
  1425. name, entry);
  1426. if (bits & IE_BITS_MAREQUEST)
  1427. ipc_marequest_cancel(space, name);
  1428. port->ip_srights++;
  1429. ip_reference(port);
  1430. entry->ie_object = IO_NULL;
  1431. }
  1432. entry->ie_bits = bits &~
  1433. (IE_BITS_UREFS_MASK|MACH_PORT_TYPE_SEND);
  1434. } else {
  1435. port->ip_srights += 2;
  1436. ip_reference(port);
  1437. ip_reference(port);
  1438. entry->ie_bits = bits-2; /* decrement urefs */
  1439. }
  1440. ip_unlock(port);
  1441. *objectp = (ipc_object_t) port;
  1442. *sorightp = dnrequest;
  1443. return KERN_SUCCESS;
  1444. invalid_right:
  1445. return KERN_INVALID_RIGHT;
  1446. }
  1447. /*
  1448. * Routine: ipc_right_copyout
  1449. * Purpose:
  1450. * Copyout a capability to a space.
  1451. * If successful, consumes a ref for the object.
  1452. *
  1453. * Always succeeds when given a newly-allocated entry,
  1454. * because user-reference overflow isn't a possibility.
  1455. *
  1456. * If copying out the object would cause the user-reference
  1457. * count in the entry to overflow, and overflow is TRUE,
  1458. * then instead the user-reference count is left pegged
  1459. * to its maximum value and the copyout succeeds anyway.
  1460. * Conditions:
  1461. * The space is write-locked and active.
  1462. * The object is locked and active.
  1463. * The object is unlocked; the space isn't.
  1464. * Returns:
  1465. * KERN_SUCCESS Copied out capability.
  1466. * KERN_UREFS_OVERFLOW User-refs would overflow;
  1467. * guaranteed not to happen with a fresh entry
  1468. * or if overflow=TRUE was specified.
  1469. */
  1470. kern_return_t
  1471. ipc_right_copyout(
  1472. ipc_space_t space,
  1473. mach_port_t name,
  1474. ipc_entry_t entry,
  1475. mach_msg_type_name_t msgt_name,
  1476. boolean_t overflow,
  1477. ipc_object_t object)
  1478. {
  1479. ipc_entry_bits_t bits = entry->ie_bits;
  1480. ipc_port_t port;
  1481. assert(IO_VALID(object));
  1482. assert(io_otype(object) == IOT_PORT);
  1483. assert(io_active(object));
  1484. assert(entry->ie_object == object);
  1485. port = (ipc_port_t) object;
  1486. switch (msgt_name) {
  1487. case MACH_MSG_TYPE_PORT_SEND_ONCE:
  1488. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_NONE);
  1489. assert(port->ip_sorights > 0);
  1490. /* transfer send-once right and ref to entry */
  1491. ip_unlock(port);
  1492. entry->ie_bits = bits | (MACH_PORT_TYPE_SEND_ONCE | 1);
  1493. break;
  1494. case MACH_MSG_TYPE_PORT_SEND:
  1495. assert(port->ip_srights > 0);
  1496. if (bits & MACH_PORT_TYPE_SEND) {
  1497. mach_port_urefs_t urefs = IE_BITS_UREFS(bits);
  1498. assert(port->ip_srights > 1);
  1499. assert(urefs > 0);
  1500. assert(urefs < MACH_PORT_UREFS_MAX);
  1501. if (urefs+1 == MACH_PORT_UREFS_MAX) {
  1502. if (overflow) {
  1503. /* leave urefs pegged to maximum */
  1504. port->ip_srights--;
  1505. ip_release(port);
  1506. ip_unlock(port);
  1507. return KERN_SUCCESS;
  1508. }
  1509. ip_unlock(port);
  1510. return KERN_UREFS_OVERFLOW;
  1511. }
  1512. port->ip_srights--;
  1513. ip_release(port);
  1514. ip_unlock(port);
  1515. } else if (bits & MACH_PORT_TYPE_RECEIVE) {
  1516. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_RECEIVE);
  1517. assert(IE_BITS_UREFS(bits) == 0);
  1518. /* transfer send right to entry */
  1519. ip_release(port);
  1520. ip_unlock(port);
  1521. } else {
  1522. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_NONE);
  1523. assert(IE_BITS_UREFS(bits) == 0);
  1524. /* transfer send right and ref to entry */
  1525. ip_unlock(port);
  1526. /* entry is locked holding ref, so can use port */
  1527. ipc_hash_insert(space, (ipc_object_t) port,
  1528. name, entry);
  1529. }
  1530. entry->ie_bits = (bits | MACH_PORT_TYPE_SEND) + 1;
  1531. break;
  1532. case MACH_MSG_TYPE_PORT_RECEIVE: {
  1533. ipc_port_t dest;
  1534. assert(port->ip_mscount == 0);
  1535. assert(port->ip_receiver_name == MACH_PORT_NULL);
  1536. dest = port->ip_destination;
  1537. port->ip_receiver_name = name;
  1538. port->ip_receiver = space;
  1539. assert((bits & MACH_PORT_TYPE_RECEIVE) == 0);
  1540. if (bits & MACH_PORT_TYPE_SEND) {
  1541. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND);
  1542. assert(IE_BITS_UREFS(bits) > 0);
  1543. assert(port->ip_srights > 0);
  1544. ip_release(port);
  1545. ip_unlock(port);
  1546. /* entry is locked holding ref, so can use port */
  1547. ipc_hash_delete(space, (ipc_object_t) port,
  1548. name, entry);
  1549. } else {
  1550. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_NONE);
  1551. assert(IE_BITS_UREFS(bits) == 0);
  1552. /* transfer ref to entry */
  1553. ip_unlock(port);
  1554. }
  1555. entry->ie_bits = bits | MACH_PORT_TYPE_RECEIVE;
  1556. if (dest != IP_NULL)
  1557. ipc_port_release(dest);
  1558. break;
  1559. }
  1560. default:
  1561. #if MACH_ASSERT
  1562. assert(!"ipc_right_copyout: strange rights");
  1563. #else
  1564. panic("ipc_right_copyout: strange rights");
  1565. #endif
  1566. }
  1567. return KERN_SUCCESS;
  1568. }
  1569. #if 0
  1570. /*XXX same, but allows multiple duplicate send rights */
  1571. kern_return_t
  1572. ipc_right_copyout_multiname(space, name, entry, object)
  1573. ipc_space_t space;
  1574. mach_port_t name;
  1575. ipc_entry_t entry;
  1576. ipc_object_t object;
  1577. {
  1578. ipc_entry_bits_t bits = entry->ie_bits;
  1579. ipc_port_t port;
  1580. assert(IO_VALID(object));
  1581. assert(io_otype(object) == IOT_PORT);
  1582. assert(io_active(object));
  1583. assert(entry->ie_object == object);
  1584. port = (ipc_port_t) object;
  1585. assert(port->ip_srights > 0);
  1586. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_NONE);
  1587. assert(IE_BITS_UREFS(bits) == 0);
  1588. /* transfer send right and ref to entry */
  1589. ip_unlock(port);
  1590. /* entry is locked holding ref, so can use port */
  1591. entry->ie_bits = (bits | MACH_PORT_TYPE_SEND) + 1;
  1592. return KERN_SUCCESS;
  1593. }
  1594. #endif
  1595. /*
  1596. * Routine: ipc_right_rename
  1597. * Purpose:
  1598. * Transfer an entry from one name to another.
  1599. * The old entry is deallocated.
  1600. * Conditions:
  1601. * The space is write-locked and active.
  1602. * The new entry is unused. Upon return,
  1603. * the space is unlocked.
  1604. * Returns:
  1605. * KERN_SUCCESS Moved entry to new name.
  1606. */
  1607. kern_return_t
  1608. ipc_right_rename(
  1609. ipc_space_t space,
  1610. mach_port_t oname,
  1611. ipc_entry_t oentry,
  1612. mach_port_t nname,
  1613. ipc_entry_t nentry)
  1614. {
  1615. ipc_entry_bits_t bits = oentry->ie_bits;
  1616. ipc_port_request_index_t request = oentry->ie_request;
  1617. ipc_object_t object = oentry->ie_object;
  1618. assert(space->is_active);
  1619. assert(oname != nname);
  1620. /*
  1621. * If IE_BITS_COMPAT, we can't allow the entry to be renamed
  1622. * if the port is dead. (This would foil ipc_port_destroy.)
  1623. * Instead we should fail because oentry shouldn't exist.
  1624. * Note IE_BITS_COMPAT implies ie_request != 0.
  1625. */
  1626. if (request != 0) {
  1627. ipc_port_t port;
  1628. assert(bits & MACH_PORT_TYPE_PORT_RIGHTS);
  1629. port = (ipc_port_t) object;
  1630. assert(port != IP_NULL);
  1631. if (ipc_right_check(space, port, oname, oentry)) {
  1632. bits = oentry->ie_bits;
  1633. assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME);
  1634. assert(oentry->ie_request == 0);
  1635. request = 0;
  1636. assert(oentry->ie_object == IO_NULL);
  1637. object = IO_NULL;
  1638. } else {
  1639. /* port is locked and active */
  1640. ipc_port_dnrename(port, request, oname, nname);
  1641. ip_unlock(port);
  1642. oentry->ie_request = 0;
  1643. }
  1644. }
  1645. if (bits & IE_BITS_MAREQUEST) {
  1646. assert(bits & MACH_PORT_TYPE_SEND_RECEIVE);
  1647. ipc_marequest_rename(space, oname, nname);
  1648. }
  1649. /* initialize nentry before letting ipc_hash_insert see it */
  1650. assert((nentry->ie_bits & IE_BITS_RIGHT_MASK) == 0);
  1651. nentry->ie_bits |= bits & IE_BITS_RIGHT_MASK;
  1652. nentry->ie_request = request;
  1653. nentry->ie_object = object;
  1654. switch (IE_BITS_TYPE(bits)) {
  1655. case MACH_PORT_TYPE_SEND: {
  1656. ipc_port_t port;
  1657. port = (ipc_port_t) object;
  1658. assert(port != IP_NULL);
  1659. ipc_hash_delete(space, (ipc_object_t) port, oname, oentry);
  1660. ipc_hash_insert(space, (ipc_object_t) port, nname, nentry);
  1661. break;
  1662. }
  1663. case MACH_PORT_TYPE_RECEIVE:
  1664. case MACH_PORT_TYPE_SEND_RECEIVE: {
  1665. ipc_port_t port;
  1666. port = (ipc_port_t) object;
  1667. assert(port != IP_NULL);
  1668. ip_lock(port);
  1669. assert(ip_active(port));
  1670. assert(port->ip_receiver_name == oname);
  1671. assert(port->ip_receiver == space);
  1672. port->ip_receiver_name = nname;
  1673. ip_unlock(port);
  1674. break;
  1675. }
  1676. case MACH_PORT_TYPE_PORT_SET: {
  1677. ipc_pset_t pset;
  1678. pset = (ipc_pset_t) object;
  1679. assert(pset != IPS_NULL);
  1680. ips_lock(pset);
  1681. assert(ips_active(pset));
  1682. assert(pset->ips_local_name == oname);
  1683. pset->ips_local_name = nname;
  1684. ips_unlock(pset);
  1685. break;
  1686. }
  1687. case MACH_PORT_TYPE_SEND_ONCE:
  1688. case MACH_PORT_TYPE_DEAD_NAME:
  1689. break;
  1690. default:
  1691. #if MACH_ASSERT
  1692. assert(!"ipc_right_rename: strange rights");
  1693. #else
  1694. panic("ipc_right_rename: strange rights");
  1695. #endif
  1696. }
  1697. assert(oentry->ie_request == 0);
  1698. oentry->ie_object = IO_NULL;
  1699. ipc_entry_dealloc(space, oname, oentry);
  1700. is_write_unlock(space);
  1701. return KERN_SUCCESS;
  1702. }