1
0

newchan 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. .TH NEWCHAN 9
  2. .SH NAME
  3. newchan, chanfree, cclose, eqqid, eqchan, isdir, fdtochan, namec \- channel operations
  4. .SH SYNOPSIS
  5. .ta \w'\fLChan* 'u
  6. .B
  7. Chan* newchan(void)
  8. .PP
  9. .B
  10. void chanfree(Chan *c)
  11. .PP
  12. .B
  13. int eqqid(Qid a, Qid b)
  14. .PP
  15. .B
  16. int eqchan(Chan *a, Chan *b, int pathonly)
  17. .PP
  18. .B
  19. void isdir(Chan *c)
  20. .PP
  21. .B
  22. Chan* fdtochan(Fgrp *f, int fd, int mode, int chkmnt, int iref)
  23. .PP
  24. .B
  25. Chan* namec(char *pathname, int amode, int omode, ulong perm)
  26. .PP
  27. .B
  28. void cclose(Chan *c)
  29. .SH DESCRIPTION
  30. A value of type
  31. .B Chan
  32. represents a kernel channel for I/O and name space operations.
  33. It has the following public structure:
  34. .IP
  35. .EX
  36. typedef struct Chan{
  37. ushort type; /* driver name */
  38. ulong dev; /* instance number */
  39. ushort mode; /* open mode */
  40. ushort flag; /* COPEN set once opened */
  41. ulong offset; /* current file offset */
  42. Qid qid; /* unique id (path, vers) */
  43. Path* path; /* name by which it was accessed */
  44. .EE
  45. .PP
  46. .I Newchan
  47. returns a pointer to a newly allocated channel (sleeping if necessary until memory is available).
  48. Device drivers do not normally call
  49. .IR newchan
  50. directly, but instead allocate channels using either
  51. .IR devattach ,
  52. when a process attaches to the device's root,
  53. or
  54. .IR devclone ,
  55. when an existing channel is cloned;
  56. see
  57. .IR devattach (9).
  58. .PP
  59. .I Chanfree
  60. frees the channel structure
  61. .I c
  62. for reuse.
  63. .PP
  64. .I Eqqid
  65. returns 1 if
  66. .B Qid
  67. values
  68. .I a
  69. and
  70. .I b
  71. are equal
  72. (ie,
  73. both their
  74. .B path
  75. and
  76. .B vers
  77. members are equal);
  78. it returns 0 otherwise.
  79. .PP
  80. .I Eqchan
  81. returns 1 if
  82. .I a
  83. and
  84. .I b
  85. have the same
  86. .BR qid ,
  87. .BR type
  88. and
  89. .BR dev
  90. members
  91. (ie, they represent the same file);
  92. it returns 0 otherwise.
  93. If
  94. .I pathonly
  95. is non-zero, the comparison of the two
  96. .B qid
  97. members compares only their
  98. .B path
  99. values,
  100. ignoring the version field
  101. .BR vers .
  102. .PP
  103. .I Isdir
  104. checks that a given channel
  105. .I c
  106. is a directory.
  107. If so, it returns;
  108. otherwise, it generates an
  109. .IR error (9),
  110. .BR Enotdir .
  111. .PP
  112. The
  113. .B Fgrp
  114. structure represents an array of open files, each
  115. represented by a
  116. .BR Chan ,
  117. indexed by integer file descriptors.
  118. A given
  119. .B Fgrp
  120. can be shared between processes.
  121. .PP
  122. .I Fdtochan
  123. returns a pointer to the
  124. .B Chan
  125. corresponding to file descriptor
  126. .I fd
  127. in file descriptor group
  128. .I f
  129. (almost invariably
  130. .BR up->fgrp ,
  131. the file descriptor group for the current process).
  132. If
  133. .I mode
  134. is a valid mode for
  135. .IR sys-open (2),
  136. typically
  137. .BR OREAD ,
  138. .B OWRITE
  139. or
  140. .BR ORDWR ,
  141. it must correspond to the mode with which
  142. .I fd
  143. was originally opened; if
  144. .I mode
  145. is
  146. .BR -1 ,
  147. no check is made.
  148. If
  149. .I chkmnt
  150. is non-zero,
  151. .I c
  152. must not be a channel in use by the mount driver
  153. .IR mnt (3).
  154. On successful return, if
  155. .I iref
  156. is non-zero, the channel's reference count has been incremented.
  157. .I Fdtochan
  158. calls
  159. .IR error (9)
  160. if it detects invalid uses, in particular an invalid file descriptor
  161. .IR fd .
  162. .PP
  163. .I Namec
  164. looks up a
  165. .I pathname
  166. in the current name space and returns a channel.
  167. .I Amode
  168. determines the mode of look up, and must be one of the constants below:
  169. .TF Aaccess
  170. .PD
  171. .TP
  172. .B Aaccess
  173. Access file for information, as in the stat command or call.
  174. .TP
  175. .B Atodir
  176. Access file as directory (the
  177. .B QTDIR
  178. bit of its
  179. .B qid.type
  180. must be set).
  181. .TP
  182. .B Aopen
  183. Access for I/O.
  184. .TP
  185. .B Amount
  186. Access directory to be mounted upon.
  187. .TP
  188. .B Acreate
  189. File is to be created.
  190. .PP
  191. If
  192. .I amode
  193. is
  194. .B Aopen
  195. or
  196. .BR Acreate ,
  197. .I omode
  198. should be a mode suitable for
  199. .IR sys-open (2);
  200. if
  201. .BR Acreate ,
  202. .I perm
  203. should be valid file permissions.
  204. In all other cases,
  205. .I omode
  206. and
  207. .I perm
  208. can be zero.
  209. .PP
  210. .I Cclose
  211. decrements the reference count on
  212. .IR c ;
  213. if no further references remain, it
  214. calls the corresponding device's
  215. .B Dev.close
  216. to close the channel, and frees
  217. .IR c .
  218. .SH DIAGNOSTICS
  219. Most functions call
  220. .IR error (9)
  221. on any sort of error.
  222. .SH SOURCE
  223. .B /sys/src/9/port/chan.c
  224. .SH SEE ALSO
  225. .IR ref (9)