tty_io.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263
  1. /*
  2. * linux/drivers/char/tty_io.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
  8. * or rs-channels. It also implements echoing, cooked mode etc.
  9. *
  10. * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
  11. *
  12. * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
  13. * tty_struct and tty_queue structures. Previously there was an array
  14. * of 256 tty_struct's which was statically allocated, and the
  15. * tty_queue structures were allocated at boot time. Both are now
  16. * dynamically allocated only when the tty is open.
  17. *
  18. * Also restructured routines so that there is more of a separation
  19. * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
  20. * the low-level tty routines (serial.c, pty.c, console.c). This
  21. * makes for cleaner and more compact code. -TYT, 9/17/92
  22. *
  23. * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
  24. * which can be dynamically activated and de-activated by the line
  25. * discipline handling modules (like SLIP).
  26. *
  27. * NOTE: pay no attention to the line discipline code (yet); its
  28. * interface is still subject to change in this version...
  29. * -- TYT, 1/31/92
  30. *
  31. * Added functionality to the OPOST tty handling. No delays, but all
  32. * other bits should be there.
  33. * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
  34. *
  35. * Rewrote canonical mode and added more termios flags.
  36. * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
  37. *
  38. * Reorganized FASYNC support so mouse code can share it.
  39. * -- ctm@ardi.com, 9Sep95
  40. *
  41. * New TIOCLINUX variants added.
  42. * -- mj@k332.feld.cvut.cz, 19-Nov-95
  43. *
  44. * Restrict vt switching via ioctl()
  45. * -- grif@cs.ucr.edu, 5-Dec-95
  46. *
  47. * Move console and virtual terminal code to more appropriate files,
  48. * implement CONFIG_VT and generalize console device interface.
  49. * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
  50. *
  51. * Rewrote tty_init_dev and tty_release_dev to eliminate races.
  52. * -- Bill Hawes <whawes@star.net>, June 97
  53. *
  54. * Added devfs support.
  55. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
  56. *
  57. * Added support for a Unix98-style ptmx device.
  58. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
  59. *
  60. * Reduced memory usage for older ARM systems
  61. * -- Russell King <rmk@arm.linux.org.uk>
  62. *
  63. * Move do_SAK() into process context. Less stack use in devfs functions.
  64. * alloc_tty_struct() always uses kmalloc()
  65. * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
  66. */
  67. #include <linux/types.h>
  68. #include <linux/major.h>
  69. #include <linux/errno.h>
  70. #include <linux/signal.h>
  71. #include <linux/fcntl.h>
  72. #include <linux/sched.h>
  73. #include <linux/interrupt.h>
  74. #include <linux/tty.h>
  75. #include <linux/tty_driver.h>
  76. #include <linux/tty_flip.h>
  77. #include <linux/devpts_fs.h>
  78. #include <linux/file.h>
  79. #include <linux/fdtable.h>
  80. #include <linux/console.h>
  81. #include <linux/timer.h>
  82. #include <linux/ctype.h>
  83. #include <linux/kd.h>
  84. #include <linux/mm.h>
  85. #include <linux/string.h>
  86. #include <linux/slab.h>
  87. #include <linux/poll.h>
  88. #include <linux/proc_fs.h>
  89. #include <linux/init.h>
  90. #include <linux/module.h>
  91. #include <linux/smp_lock.h>
  92. #include <linux/device.h>
  93. #include <linux/wait.h>
  94. #include <linux/bitops.h>
  95. #include <linux/delay.h>
  96. #include <linux/seq_file.h>
  97. #include <linux/serial.h>
  98. #include <linux/uaccess.h>
  99. #include <asm/system.h>
  100. #include <linux/kbd_kern.h>
  101. #include <linux/vt_kern.h>
  102. #include <linux/selection.h>
  103. #include <linux/kmod.h>
  104. #include <linux/nsproxy.h>
  105. #undef TTY_DEBUG_HANGUP
  106. #define TTY_PARANOIA_CHECK 1
  107. #define CHECK_TTY_COUNT 1
  108. struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
  109. .c_iflag = ICRNL | IXON,
  110. .c_oflag = OPOST | ONLCR,
  111. .c_cflag = B38400 | CS8 | CREAD | HUPCL,
  112. .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
  113. ECHOCTL | ECHOKE | IEXTEN,
  114. .c_cc = INIT_C_CC,
  115. .c_ispeed = 38400,
  116. .c_ospeed = 38400
  117. };
  118. EXPORT_SYMBOL(tty_std_termios);
  119. /* This list gets poked at by procfs and various bits of boot up code. This
  120. could do with some rationalisation such as pulling the tty proc function
  121. into this file */
  122. LIST_HEAD(tty_drivers); /* linked list of tty drivers */
  123. /* Mutex to protect creating and releasing a tty. This is shared with
  124. vt.c for deeply disgusting hack reasons */
  125. DEFINE_MUTEX(tty_mutex);
  126. EXPORT_SYMBOL(tty_mutex);
  127. /* Spinlock to protect the tty->tty_files list */
  128. DEFINE_SPINLOCK(tty_files_lock);
  129. static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
  130. static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
  131. ssize_t redirected_tty_write(struct file *, const char __user *,
  132. size_t, loff_t *);
  133. static unsigned int tty_poll(struct file *, poll_table *);
  134. static int tty_open(struct inode *, struct file *);
  135. long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  136. #ifdef CONFIG_COMPAT
  137. static long tty_compat_ioctl(struct file *file, unsigned int cmd,
  138. unsigned long arg);
  139. #else
  140. #define tty_compat_ioctl NULL
  141. #endif
  142. static int __tty_fasync(int fd, struct file *filp, int on);
  143. static int tty_fasync(int fd, struct file *filp, int on);
  144. static void release_tty(struct tty_struct *tty, int idx);
  145. static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
  146. static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
  147. /**
  148. * alloc_tty_struct - allocate a tty object
  149. *
  150. * Return a new empty tty structure. The data fields have not
  151. * been initialized in any way but has been zeroed
  152. *
  153. * Locking: none
  154. */
  155. struct tty_struct *alloc_tty_struct(void)
  156. {
  157. return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
  158. }
  159. /**
  160. * free_tty_struct - free a disused tty
  161. * @tty: tty struct to free
  162. *
  163. * Free the write buffers, tty queue and tty memory itself.
  164. *
  165. * Locking: none. Must be called after tty is definitely unused
  166. */
  167. void free_tty_struct(struct tty_struct *tty)
  168. {
  169. if (tty->dev)
  170. put_device(tty->dev);
  171. kfree(tty->write_buf);
  172. tty_buffer_free_all(tty);
  173. kfree(tty);
  174. }
  175. static inline struct tty_struct *file_tty(struct file *file)
  176. {
  177. return ((struct tty_file_private *)file->private_data)->tty;
  178. }
  179. /* Associate a new file with the tty structure */
  180. int tty_add_file(struct tty_struct *tty, struct file *file)
  181. {
  182. struct tty_file_private *priv;
  183. priv = kmalloc(sizeof(*priv), GFP_KERNEL);
  184. if (!priv)
  185. return -ENOMEM;
  186. priv->tty = tty;
  187. priv->file = file;
  188. file->private_data = priv;
  189. spin_lock(&tty_files_lock);
  190. list_add(&priv->list, &tty->tty_files);
  191. spin_unlock(&tty_files_lock);
  192. return 0;
  193. }
  194. /* Delete file from its tty */
  195. void tty_del_file(struct file *file)
  196. {
  197. struct tty_file_private *priv = file->private_data;
  198. spin_lock(&tty_files_lock);
  199. list_del(&priv->list);
  200. spin_unlock(&tty_files_lock);
  201. file->private_data = NULL;
  202. kfree(priv);
  203. }
  204. #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
  205. /**
  206. * tty_name - return tty naming
  207. * @tty: tty structure
  208. * @buf: buffer for output
  209. *
  210. * Convert a tty structure into a name. The name reflects the kernel
  211. * naming policy and if udev is in use may not reflect user space
  212. *
  213. * Locking: none
  214. */
  215. char *tty_name(struct tty_struct *tty, char *buf)
  216. {
  217. if (!tty) /* Hmm. NULL pointer. That's fun. */
  218. strcpy(buf, "NULL tty");
  219. else
  220. strcpy(buf, tty->name);
  221. return buf;
  222. }
  223. EXPORT_SYMBOL(tty_name);
  224. int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
  225. const char *routine)
  226. {
  227. #ifdef TTY_PARANOIA_CHECK
  228. if (!tty) {
  229. printk(KERN_WARNING
  230. "null TTY for (%d:%d) in %s\n",
  231. imajor(inode), iminor(inode), routine);
  232. return 1;
  233. }
  234. if (tty->magic != TTY_MAGIC) {
  235. printk(KERN_WARNING
  236. "bad magic number for tty struct (%d:%d) in %s\n",
  237. imajor(inode), iminor(inode), routine);
  238. return 1;
  239. }
  240. #endif
  241. return 0;
  242. }
  243. static int check_tty_count(struct tty_struct *tty, const char *routine)
  244. {
  245. #ifdef CHECK_TTY_COUNT
  246. struct list_head *p;
  247. int count = 0;
  248. spin_lock(&tty_files_lock);
  249. list_for_each(p, &tty->tty_files) {
  250. count++;
  251. }
  252. spin_unlock(&tty_files_lock);
  253. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  254. tty->driver->subtype == PTY_TYPE_SLAVE &&
  255. tty->link && tty->link->count)
  256. count++;
  257. if (tty->count != count) {
  258. printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
  259. "!= #fd's(%d) in %s\n",
  260. tty->name, tty->count, count, routine);
  261. return count;
  262. }
  263. #endif
  264. return 0;
  265. }
  266. /**
  267. * get_tty_driver - find device of a tty
  268. * @dev_t: device identifier
  269. * @index: returns the index of the tty
  270. *
  271. * This routine returns a tty driver structure, given a device number
  272. * and also passes back the index number.
  273. *
  274. * Locking: caller must hold tty_mutex
  275. */
  276. static struct tty_driver *get_tty_driver(dev_t device, int *index)
  277. {
  278. struct tty_driver *p;
  279. list_for_each_entry(p, &tty_drivers, tty_drivers) {
  280. dev_t base = MKDEV(p->major, p->minor_start);
  281. if (device < base || device >= base + p->num)
  282. continue;
  283. *index = device - base;
  284. return tty_driver_kref_get(p);
  285. }
  286. return NULL;
  287. }
  288. #ifdef CONFIG_CONSOLE_POLL
  289. /**
  290. * tty_find_polling_driver - find device of a polled tty
  291. * @name: name string to match
  292. * @line: pointer to resulting tty line nr
  293. *
  294. * This routine returns a tty driver structure, given a name
  295. * and the condition that the tty driver is capable of polled
  296. * operation.
  297. */
  298. struct tty_driver *tty_find_polling_driver(char *name, int *line)
  299. {
  300. struct tty_driver *p, *res = NULL;
  301. int tty_line = 0;
  302. int len;
  303. char *str, *stp;
  304. for (str = name; *str; str++)
  305. if ((*str >= '0' && *str <= '9') || *str == ',')
  306. break;
  307. if (!*str)
  308. return NULL;
  309. len = str - name;
  310. tty_line = simple_strtoul(str, &str, 10);
  311. mutex_lock(&tty_mutex);
  312. /* Search through the tty devices to look for a match */
  313. list_for_each_entry(p, &tty_drivers, tty_drivers) {
  314. if (strncmp(name, p->name, len) != 0)
  315. continue;
  316. stp = str;
  317. if (*stp == ',')
  318. stp++;
  319. if (*stp == '\0')
  320. stp = NULL;
  321. if (tty_line >= 0 && tty_line < p->num && p->ops &&
  322. p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
  323. res = tty_driver_kref_get(p);
  324. *line = tty_line;
  325. break;
  326. }
  327. }
  328. mutex_unlock(&tty_mutex);
  329. return res;
  330. }
  331. EXPORT_SYMBOL_GPL(tty_find_polling_driver);
  332. #endif
  333. /**
  334. * tty_check_change - check for POSIX terminal changes
  335. * @tty: tty to check
  336. *
  337. * If we try to write to, or set the state of, a terminal and we're
  338. * not in the foreground, send a SIGTTOU. If the signal is blocked or
  339. * ignored, go ahead and perform the operation. (POSIX 7.2)
  340. *
  341. * Locking: ctrl_lock
  342. */
  343. int tty_check_change(struct tty_struct *tty)
  344. {
  345. unsigned long flags;
  346. int ret = 0;
  347. if (current->signal->tty != tty)
  348. return 0;
  349. spin_lock_irqsave(&tty->ctrl_lock, flags);
  350. if (!tty->pgrp) {
  351. printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
  352. goto out_unlock;
  353. }
  354. if (task_pgrp(current) == tty->pgrp)
  355. goto out_unlock;
  356. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  357. if (is_ignored(SIGTTOU))
  358. goto out;
  359. if (is_current_pgrp_orphaned()) {
  360. ret = -EIO;
  361. goto out;
  362. }
  363. kill_pgrp(task_pgrp(current), SIGTTOU, 1);
  364. set_thread_flag(TIF_SIGPENDING);
  365. ret = -ERESTARTSYS;
  366. out:
  367. return ret;
  368. out_unlock:
  369. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  370. return ret;
  371. }
  372. EXPORT_SYMBOL(tty_check_change);
  373. static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
  374. size_t count, loff_t *ppos)
  375. {
  376. return 0;
  377. }
  378. static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
  379. size_t count, loff_t *ppos)
  380. {
  381. return -EIO;
  382. }
  383. /* No kernel lock held - none needed ;) */
  384. static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
  385. {
  386. return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
  387. }
  388. static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
  389. unsigned long arg)
  390. {
  391. return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
  392. }
  393. static long hung_up_tty_compat_ioctl(struct file *file,
  394. unsigned int cmd, unsigned long arg)
  395. {
  396. return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
  397. }
  398. static const struct file_operations tty_fops = {
  399. .llseek = no_llseek,
  400. .read = tty_read,
  401. .write = tty_write,
  402. .poll = tty_poll,
  403. .unlocked_ioctl = tty_ioctl,
  404. .compat_ioctl = tty_compat_ioctl,
  405. .open = tty_open,
  406. .release = tty_release,
  407. .fasync = tty_fasync,
  408. };
  409. static const struct file_operations console_fops = {
  410. .llseek = no_llseek,
  411. .read = tty_read,
  412. .write = redirected_tty_write,
  413. .poll = tty_poll,
  414. .unlocked_ioctl = tty_ioctl,
  415. .compat_ioctl = tty_compat_ioctl,
  416. .open = tty_open,
  417. .release = tty_release,
  418. .fasync = tty_fasync,
  419. };
  420. static const struct file_operations hung_up_tty_fops = {
  421. .llseek = no_llseek,
  422. .read = hung_up_tty_read,
  423. .write = hung_up_tty_write,
  424. .poll = hung_up_tty_poll,
  425. .unlocked_ioctl = hung_up_tty_ioctl,
  426. .compat_ioctl = hung_up_tty_compat_ioctl,
  427. .release = tty_release,
  428. };
  429. static DEFINE_SPINLOCK(redirect_lock);
  430. static struct file *redirect;
  431. /**
  432. * tty_wakeup - request more data
  433. * @tty: terminal
  434. *
  435. * Internal and external helper for wakeups of tty. This function
  436. * informs the line discipline if present that the driver is ready
  437. * to receive more output data.
  438. */
  439. void tty_wakeup(struct tty_struct *tty)
  440. {
  441. struct tty_ldisc *ld;
  442. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
  443. ld = tty_ldisc_ref(tty);
  444. if (ld) {
  445. if (ld->ops->write_wakeup)
  446. ld->ops->write_wakeup(tty);
  447. tty_ldisc_deref(ld);
  448. }
  449. }
  450. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  451. }
  452. EXPORT_SYMBOL_GPL(tty_wakeup);
  453. /**
  454. * __tty_hangup - actual handler for hangup events
  455. * @work: tty device
  456. *
  457. * This can be called by the "eventd" kernel thread. That is process
  458. * synchronous but doesn't hold any locks, so we need to make sure we
  459. * have the appropriate locks for what we're doing.
  460. *
  461. * The hangup event clears any pending redirections onto the hung up
  462. * device. It ensures future writes will error and it does the needed
  463. * line discipline hangup and signal delivery. The tty object itself
  464. * remains intact.
  465. *
  466. * Locking:
  467. * BTM
  468. * redirect lock for undoing redirection
  469. * file list lock for manipulating list of ttys
  470. * tty_ldisc_lock from called functions
  471. * termios_mutex resetting termios data
  472. * tasklist_lock to walk task list for hangup event
  473. * ->siglock to protect ->signal/->sighand
  474. */
  475. void __tty_hangup(struct tty_struct *tty)
  476. {
  477. struct file *cons_filp = NULL;
  478. struct file *filp, *f = NULL;
  479. struct task_struct *p;
  480. struct tty_file_private *priv;
  481. int closecount = 0, n;
  482. unsigned long flags;
  483. int refs = 0;
  484. if (!tty)
  485. return;
  486. spin_lock(&redirect_lock);
  487. if (redirect && file_tty(redirect) == tty) {
  488. f = redirect;
  489. redirect = NULL;
  490. }
  491. spin_unlock(&redirect_lock);
  492. tty_lock();
  493. /* inuse_filps is protected by the single tty lock,
  494. this really needs to change if we want to flush the
  495. workqueue with the lock held */
  496. check_tty_count(tty, "tty_hangup");
  497. spin_lock(&tty_files_lock);
  498. /* This breaks for file handles being sent over AF_UNIX sockets ? */
  499. list_for_each_entry(priv, &tty->tty_files, list) {
  500. filp = priv->file;
  501. if (filp->f_op->write == redirected_tty_write)
  502. cons_filp = filp;
  503. if (filp->f_op->write != tty_write)
  504. continue;
  505. closecount++;
  506. __tty_fasync(-1, filp, 0); /* can't block */
  507. filp->f_op = &hung_up_tty_fops;
  508. }
  509. spin_unlock(&tty_files_lock);
  510. tty_ldisc_hangup(tty);
  511. read_lock(&tasklist_lock);
  512. if (tty->session) {
  513. do_each_pid_task(tty->session, PIDTYPE_SID, p) {
  514. spin_lock_irq(&p->sighand->siglock);
  515. if (p->signal->tty == tty) {
  516. p->signal->tty = NULL;
  517. /* We defer the dereferences outside fo
  518. the tasklist lock */
  519. refs++;
  520. }
  521. if (!p->signal->leader) {
  522. spin_unlock_irq(&p->sighand->siglock);
  523. continue;
  524. }
  525. __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
  526. __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
  527. put_pid(p->signal->tty_old_pgrp); /* A noop */
  528. spin_lock_irqsave(&tty->ctrl_lock, flags);
  529. if (tty->pgrp)
  530. p->signal->tty_old_pgrp = get_pid(tty->pgrp);
  531. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  532. spin_unlock_irq(&p->sighand->siglock);
  533. } while_each_pid_task(tty->session, PIDTYPE_SID, p);
  534. }
  535. read_unlock(&tasklist_lock);
  536. spin_lock_irqsave(&tty->ctrl_lock, flags);
  537. clear_bit(TTY_THROTTLED, &tty->flags);
  538. clear_bit(TTY_PUSH, &tty->flags);
  539. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  540. put_pid(tty->session);
  541. put_pid(tty->pgrp);
  542. tty->session = NULL;
  543. tty->pgrp = NULL;
  544. tty->ctrl_status = 0;
  545. set_bit(TTY_HUPPED, &tty->flags);
  546. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  547. /* Account for the p->signal references we killed */
  548. while (refs--)
  549. tty_kref_put(tty);
  550. /*
  551. * If one of the devices matches a console pointer, we
  552. * cannot just call hangup() because that will cause
  553. * tty->count and state->count to go out of sync.
  554. * So we just call close() the right number of times.
  555. */
  556. if (cons_filp) {
  557. if (tty->ops->close)
  558. for (n = 0; n < closecount; n++)
  559. tty->ops->close(tty, cons_filp);
  560. } else if (tty->ops->hangup)
  561. (tty->ops->hangup)(tty);
  562. /*
  563. * We don't want to have driver/ldisc interactions beyond
  564. * the ones we did here. The driver layer expects no
  565. * calls after ->hangup() from the ldisc side. However we
  566. * can't yet guarantee all that.
  567. */
  568. set_bit(TTY_HUPPED, &tty->flags);
  569. tty_ldisc_enable(tty);
  570. tty_unlock();
  571. if (f)
  572. fput(f);
  573. }
  574. static void do_tty_hangup(struct work_struct *work)
  575. {
  576. struct tty_struct *tty =
  577. container_of(work, struct tty_struct, hangup_work);
  578. __tty_hangup(tty);
  579. }
  580. /**
  581. * tty_hangup - trigger a hangup event
  582. * @tty: tty to hangup
  583. *
  584. * A carrier loss (virtual or otherwise) has occurred on this like
  585. * schedule a hangup sequence to run after this event.
  586. */
  587. void tty_hangup(struct tty_struct *tty)
  588. {
  589. #ifdef TTY_DEBUG_HANGUP
  590. char buf[64];
  591. printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
  592. #endif
  593. schedule_work(&tty->hangup_work);
  594. }
  595. EXPORT_SYMBOL(tty_hangup);
  596. /**
  597. * tty_vhangup - process vhangup
  598. * @tty: tty to hangup
  599. *
  600. * The user has asked via system call for the terminal to be hung up.
  601. * We do this synchronously so that when the syscall returns the process
  602. * is complete. That guarantee is necessary for security reasons.
  603. */
  604. void tty_vhangup(struct tty_struct *tty)
  605. {
  606. #ifdef TTY_DEBUG_HANGUP
  607. char buf[64];
  608. printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
  609. #endif
  610. __tty_hangup(tty);
  611. }
  612. EXPORT_SYMBOL(tty_vhangup);
  613. /**
  614. * tty_vhangup_self - process vhangup for own ctty
  615. *
  616. * Perform a vhangup on the current controlling tty
  617. */
  618. void tty_vhangup_self(void)
  619. {
  620. struct tty_struct *tty;
  621. tty = get_current_tty();
  622. if (tty) {
  623. tty_vhangup(tty);
  624. tty_kref_put(tty);
  625. }
  626. }
  627. /**
  628. * tty_hung_up_p - was tty hung up
  629. * @filp: file pointer of tty
  630. *
  631. * Return true if the tty has been subject to a vhangup or a carrier
  632. * loss
  633. */
  634. int tty_hung_up_p(struct file *filp)
  635. {
  636. return (filp->f_op == &hung_up_tty_fops);
  637. }
  638. EXPORT_SYMBOL(tty_hung_up_p);
  639. static void session_clear_tty(struct pid *session)
  640. {
  641. struct task_struct *p;
  642. do_each_pid_task(session, PIDTYPE_SID, p) {
  643. proc_clear_tty(p);
  644. } while_each_pid_task(session, PIDTYPE_SID, p);
  645. }
  646. /**
  647. * disassociate_ctty - disconnect controlling tty
  648. * @on_exit: true if exiting so need to "hang up" the session
  649. *
  650. * This function is typically called only by the session leader, when
  651. * it wants to disassociate itself from its controlling tty.
  652. *
  653. * It performs the following functions:
  654. * (1) Sends a SIGHUP and SIGCONT to the foreground process group
  655. * (2) Clears the tty from being controlling the session
  656. * (3) Clears the controlling tty for all processes in the
  657. * session group.
  658. *
  659. * The argument on_exit is set to 1 if called when a process is
  660. * exiting; it is 0 if called by the ioctl TIOCNOTTY.
  661. *
  662. * Locking:
  663. * BTM is taken for hysterical raisins, and held when
  664. * called from no_tty().
  665. * tty_mutex is taken to protect tty
  666. * ->siglock is taken to protect ->signal/->sighand
  667. * tasklist_lock is taken to walk process list for sessions
  668. * ->siglock is taken to protect ->signal/->sighand
  669. */
  670. void disassociate_ctty(int on_exit)
  671. {
  672. struct tty_struct *tty;
  673. struct pid *tty_pgrp = NULL;
  674. if (!current->signal->leader)
  675. return;
  676. tty = get_current_tty();
  677. if (tty) {
  678. tty_pgrp = get_pid(tty->pgrp);
  679. if (on_exit) {
  680. if (tty->driver->type != TTY_DRIVER_TYPE_PTY)
  681. tty_vhangup(tty);
  682. }
  683. tty_kref_put(tty);
  684. } else if (on_exit) {
  685. struct pid *old_pgrp;
  686. spin_lock_irq(&current->sighand->siglock);
  687. old_pgrp = current->signal->tty_old_pgrp;
  688. current->signal->tty_old_pgrp = NULL;
  689. spin_unlock_irq(&current->sighand->siglock);
  690. if (old_pgrp) {
  691. kill_pgrp(old_pgrp, SIGHUP, on_exit);
  692. kill_pgrp(old_pgrp, SIGCONT, on_exit);
  693. put_pid(old_pgrp);
  694. }
  695. return;
  696. }
  697. if (tty_pgrp) {
  698. kill_pgrp(tty_pgrp, SIGHUP, on_exit);
  699. if (!on_exit)
  700. kill_pgrp(tty_pgrp, SIGCONT, on_exit);
  701. put_pid(tty_pgrp);
  702. }
  703. spin_lock_irq(&current->sighand->siglock);
  704. put_pid(current->signal->tty_old_pgrp);
  705. current->signal->tty_old_pgrp = NULL;
  706. spin_unlock_irq(&current->sighand->siglock);
  707. tty = get_current_tty();
  708. if (tty) {
  709. unsigned long flags;
  710. spin_lock_irqsave(&tty->ctrl_lock, flags);
  711. put_pid(tty->session);
  712. put_pid(tty->pgrp);
  713. tty->session = NULL;
  714. tty->pgrp = NULL;
  715. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  716. tty_kref_put(tty);
  717. } else {
  718. #ifdef TTY_DEBUG_HANGUP
  719. printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
  720. " = NULL", tty);
  721. #endif
  722. }
  723. /* Now clear signal->tty under the lock */
  724. read_lock(&tasklist_lock);
  725. session_clear_tty(task_session(current));
  726. read_unlock(&tasklist_lock);
  727. }
  728. /**
  729. *
  730. * no_tty - Ensure the current process does not have a controlling tty
  731. */
  732. void no_tty(void)
  733. {
  734. struct task_struct *tsk = current;
  735. tty_lock();
  736. disassociate_ctty(0);
  737. tty_unlock();
  738. proc_clear_tty(tsk);
  739. }
  740. /**
  741. * stop_tty - propagate flow control
  742. * @tty: tty to stop
  743. *
  744. * Perform flow control to the driver. For PTY/TTY pairs we
  745. * must also propagate the TIOCKPKT status. May be called
  746. * on an already stopped device and will not re-call the driver
  747. * method.
  748. *
  749. * This functionality is used by both the line disciplines for
  750. * halting incoming flow and by the driver. It may therefore be
  751. * called from any context, may be under the tty atomic_write_lock
  752. * but not always.
  753. *
  754. * Locking:
  755. * Uses the tty control lock internally
  756. */
  757. void stop_tty(struct tty_struct *tty)
  758. {
  759. unsigned long flags;
  760. spin_lock_irqsave(&tty->ctrl_lock, flags);
  761. if (tty->stopped) {
  762. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  763. return;
  764. }
  765. tty->stopped = 1;
  766. if (tty->link && tty->link->packet) {
  767. tty->ctrl_status &= ~TIOCPKT_START;
  768. tty->ctrl_status |= TIOCPKT_STOP;
  769. wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
  770. }
  771. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  772. if (tty->ops->stop)
  773. (tty->ops->stop)(tty);
  774. }
  775. EXPORT_SYMBOL(stop_tty);
  776. /**
  777. * start_tty - propagate flow control
  778. * @tty: tty to start
  779. *
  780. * Start a tty that has been stopped if at all possible. Perform
  781. * any necessary wakeups and propagate the TIOCPKT status. If this
  782. * is the tty was previous stopped and is being started then the
  783. * driver start method is invoked and the line discipline woken.
  784. *
  785. * Locking:
  786. * ctrl_lock
  787. */
  788. void start_tty(struct tty_struct *tty)
  789. {
  790. unsigned long flags;
  791. spin_lock_irqsave(&tty->ctrl_lock, flags);
  792. if (!tty->stopped || tty->flow_stopped) {
  793. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  794. return;
  795. }
  796. tty->stopped = 0;
  797. if (tty->link && tty->link->packet) {
  798. tty->ctrl_status &= ~TIOCPKT_STOP;
  799. tty->ctrl_status |= TIOCPKT_START;
  800. wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
  801. }
  802. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  803. if (tty->ops->start)
  804. (tty->ops->start)(tty);
  805. /* If we have a running line discipline it may need kicking */
  806. tty_wakeup(tty);
  807. }
  808. EXPORT_SYMBOL(start_tty);
  809. /**
  810. * tty_read - read method for tty device files
  811. * @file: pointer to tty file
  812. * @buf: user buffer
  813. * @count: size of user buffer
  814. * @ppos: unused
  815. *
  816. * Perform the read system call function on this terminal device. Checks
  817. * for hung up devices before calling the line discipline method.
  818. *
  819. * Locking:
  820. * Locks the line discipline internally while needed. Multiple
  821. * read calls may be outstanding in parallel.
  822. */
  823. static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
  824. loff_t *ppos)
  825. {
  826. int i;
  827. struct inode *inode = file->f_path.dentry->d_inode;
  828. struct tty_struct *tty = file_tty(file);
  829. struct tty_ldisc *ld;
  830. if (tty_paranoia_check(tty, inode, "tty_read"))
  831. return -EIO;
  832. if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
  833. return -EIO;
  834. /* We want to wait for the line discipline to sort out in this
  835. situation */
  836. ld = tty_ldisc_ref_wait(tty);
  837. if (ld->ops->read)
  838. i = (ld->ops->read)(tty, file, buf, count);
  839. else
  840. i = -EIO;
  841. tty_ldisc_deref(ld);
  842. if (i > 0)
  843. inode->i_atime = current_fs_time(inode->i_sb);
  844. return i;
  845. }
  846. void tty_write_unlock(struct tty_struct *tty)
  847. {
  848. mutex_unlock(&tty->atomic_write_lock);
  849. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  850. }
  851. int tty_write_lock(struct tty_struct *tty, int ndelay)
  852. {
  853. if (!mutex_trylock(&tty->atomic_write_lock)) {
  854. if (ndelay)
  855. return -EAGAIN;
  856. if (mutex_lock_interruptible(&tty->atomic_write_lock))
  857. return -ERESTARTSYS;
  858. }
  859. return 0;
  860. }
  861. /*
  862. * Split writes up in sane blocksizes to avoid
  863. * denial-of-service type attacks
  864. */
  865. static inline ssize_t do_tty_write(
  866. ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
  867. struct tty_struct *tty,
  868. struct file *file,
  869. const char __user *buf,
  870. size_t count)
  871. {
  872. ssize_t ret, written = 0;
  873. unsigned int chunk;
  874. ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
  875. if (ret < 0)
  876. return ret;
  877. /*
  878. * We chunk up writes into a temporary buffer. This
  879. * simplifies low-level drivers immensely, since they
  880. * don't have locking issues and user mode accesses.
  881. *
  882. * But if TTY_NO_WRITE_SPLIT is set, we should use a
  883. * big chunk-size..
  884. *
  885. * The default chunk-size is 2kB, because the NTTY
  886. * layer has problems with bigger chunks. It will
  887. * claim to be able to handle more characters than
  888. * it actually does.
  889. *
  890. * FIXME: This can probably go away now except that 64K chunks
  891. * are too likely to fail unless switched to vmalloc...
  892. */
  893. chunk = 2048;
  894. if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
  895. chunk = 65536;
  896. if (count < chunk)
  897. chunk = count;
  898. /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
  899. if (tty->write_cnt < chunk) {
  900. unsigned char *buf_chunk;
  901. if (chunk < 1024)
  902. chunk = 1024;
  903. buf_chunk = kmalloc(chunk, GFP_KERNEL);
  904. if (!buf_chunk) {
  905. ret = -ENOMEM;
  906. goto out;
  907. }
  908. kfree(tty->write_buf);
  909. tty->write_cnt = chunk;
  910. tty->write_buf = buf_chunk;
  911. }
  912. /* Do the write .. */
  913. for (;;) {
  914. size_t size = count;
  915. if (size > chunk)
  916. size = chunk;
  917. ret = -EFAULT;
  918. if (copy_from_user(tty->write_buf, buf, size))
  919. break;
  920. ret = write(tty, file, tty->write_buf, size);
  921. if (ret <= 0)
  922. break;
  923. written += ret;
  924. buf += ret;
  925. count -= ret;
  926. if (!count)
  927. break;
  928. ret = -ERESTARTSYS;
  929. if (signal_pending(current))
  930. break;
  931. cond_resched();
  932. }
  933. if (written) {
  934. struct inode *inode = file->f_path.dentry->d_inode;
  935. inode->i_mtime = current_fs_time(inode->i_sb);
  936. ret = written;
  937. }
  938. out:
  939. tty_write_unlock(tty);
  940. return ret;
  941. }
  942. /**
  943. * tty_write_message - write a message to a certain tty, not just the console.
  944. * @tty: the destination tty_struct
  945. * @msg: the message to write
  946. *
  947. * This is used for messages that need to be redirected to a specific tty.
  948. * We don't put it into the syslog queue right now maybe in the future if
  949. * really needed.
  950. *
  951. * We must still hold the BTM and test the CLOSING flag for the moment.
  952. */
  953. void tty_write_message(struct tty_struct *tty, char *msg)
  954. {
  955. if (tty) {
  956. mutex_lock(&tty->atomic_write_lock);
  957. tty_lock();
  958. if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
  959. tty_unlock();
  960. tty->ops->write(tty, msg, strlen(msg));
  961. } else
  962. tty_unlock();
  963. tty_write_unlock(tty);
  964. }
  965. return;
  966. }
  967. /**
  968. * tty_write - write method for tty device file
  969. * @file: tty file pointer
  970. * @buf: user data to write
  971. * @count: bytes to write
  972. * @ppos: unused
  973. *
  974. * Write data to a tty device via the line discipline.
  975. *
  976. * Locking:
  977. * Locks the line discipline as required
  978. * Writes to the tty driver are serialized by the atomic_write_lock
  979. * and are then processed in chunks to the device. The line discipline
  980. * write method will not be invoked in parallel for each device.
  981. */
  982. static ssize_t tty_write(struct file *file, const char __user *buf,
  983. size_t count, loff_t *ppos)
  984. {
  985. struct inode *inode = file->f_path.dentry->d_inode;
  986. struct tty_struct *tty = file_tty(file);
  987. struct tty_ldisc *ld;
  988. ssize_t ret;
  989. if (tty_paranoia_check(tty, inode, "tty_write"))
  990. return -EIO;
  991. if (!tty || !tty->ops->write ||
  992. (test_bit(TTY_IO_ERROR, &tty->flags)))
  993. return -EIO;
  994. /* Short term debug to catch buggy drivers */
  995. if (tty->ops->write_room == NULL)
  996. printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
  997. tty->driver->name);
  998. ld = tty_ldisc_ref_wait(tty);
  999. if (!ld->ops->write)
  1000. ret = -EIO;
  1001. else
  1002. ret = do_tty_write(ld->ops->write, tty, file, buf, count);
  1003. tty_ldisc_deref(ld);
  1004. return ret;
  1005. }
  1006. ssize_t redirected_tty_write(struct file *file, const char __user *buf,
  1007. size_t count, loff_t *ppos)
  1008. {
  1009. struct file *p = NULL;
  1010. spin_lock(&redirect_lock);
  1011. if (redirect) {
  1012. get_file(redirect);
  1013. p = redirect;
  1014. }
  1015. spin_unlock(&redirect_lock);
  1016. if (p) {
  1017. ssize_t res;
  1018. res = vfs_write(p, buf, count, &p->f_pos);
  1019. fput(p);
  1020. return res;
  1021. }
  1022. return tty_write(file, buf, count, ppos);
  1023. }
  1024. static char ptychar[] = "pqrstuvwxyzabcde";
  1025. /**
  1026. * pty_line_name - generate name for a pty
  1027. * @driver: the tty driver in use
  1028. * @index: the minor number
  1029. * @p: output buffer of at least 6 bytes
  1030. *
  1031. * Generate a name from a driver reference and write it to the output
  1032. * buffer.
  1033. *
  1034. * Locking: None
  1035. */
  1036. static void pty_line_name(struct tty_driver *driver, int index, char *p)
  1037. {
  1038. int i = index + driver->name_base;
  1039. /* ->name is initialized to "ttyp", but "tty" is expected */
  1040. sprintf(p, "%s%c%x",
  1041. driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
  1042. ptychar[i >> 4 & 0xf], i & 0xf);
  1043. }
  1044. /**
  1045. * tty_line_name - generate name for a tty
  1046. * @driver: the tty driver in use
  1047. * @index: the minor number
  1048. * @p: output buffer of at least 7 bytes
  1049. *
  1050. * Generate a name from a driver reference and write it to the output
  1051. * buffer.
  1052. *
  1053. * Locking: None
  1054. */
  1055. static void tty_line_name(struct tty_driver *driver, int index, char *p)
  1056. {
  1057. sprintf(p, "%s%d", driver->name, index + driver->name_base);
  1058. }
  1059. /**
  1060. * tty_driver_lookup_tty() - find an existing tty, if any
  1061. * @driver: the driver for the tty
  1062. * @idx: the minor number
  1063. *
  1064. * Return the tty, if found or ERR_PTR() otherwise.
  1065. *
  1066. * Locking: tty_mutex must be held. If tty is found, the mutex must
  1067. * be held until the 'fast-open' is also done. Will change once we
  1068. * have refcounting in the driver and per driver locking
  1069. */
  1070. static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
  1071. struct inode *inode, int idx)
  1072. {
  1073. struct tty_struct *tty;
  1074. if (driver->ops->lookup)
  1075. return driver->ops->lookup(driver, inode, idx);
  1076. tty = driver->ttys[idx];
  1077. return tty;
  1078. }
  1079. /**
  1080. * tty_init_termios - helper for termios setup
  1081. * @tty: the tty to set up
  1082. *
  1083. * Initialise the termios structures for this tty. Thus runs under
  1084. * the tty_mutex currently so we can be relaxed about ordering.
  1085. */
  1086. int tty_init_termios(struct tty_struct *tty)
  1087. {
  1088. struct ktermios *tp;
  1089. int idx = tty->index;
  1090. tp = tty->driver->termios[idx];
  1091. if (tp == NULL) {
  1092. tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
  1093. if (tp == NULL)
  1094. return -ENOMEM;
  1095. memcpy(tp, &tty->driver->init_termios,
  1096. sizeof(struct ktermios));
  1097. tty->driver->termios[idx] = tp;
  1098. }
  1099. tty->termios = tp;
  1100. tty->termios_locked = tp + 1;
  1101. /* Compatibility until drivers always set this */
  1102. tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
  1103. tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
  1104. return 0;
  1105. }
  1106. EXPORT_SYMBOL_GPL(tty_init_termios);
  1107. /**
  1108. * tty_driver_install_tty() - install a tty entry in the driver
  1109. * @driver: the driver for the tty
  1110. * @tty: the tty
  1111. *
  1112. * Install a tty object into the driver tables. The tty->index field
  1113. * will be set by the time this is called. This method is responsible
  1114. * for ensuring any need additional structures are allocated and
  1115. * configured.
  1116. *
  1117. * Locking: tty_mutex for now
  1118. */
  1119. static int tty_driver_install_tty(struct tty_driver *driver,
  1120. struct tty_struct *tty)
  1121. {
  1122. int idx = tty->index;
  1123. int ret;
  1124. if (driver->ops->install) {
  1125. ret = driver->ops->install(driver, tty);
  1126. return ret;
  1127. }
  1128. if (tty_init_termios(tty) == 0) {
  1129. tty_driver_kref_get(driver);
  1130. tty->count++;
  1131. driver->ttys[idx] = tty;
  1132. return 0;
  1133. }
  1134. return -ENOMEM;
  1135. }
  1136. /**
  1137. * tty_driver_remove_tty() - remove a tty from the driver tables
  1138. * @driver: the driver for the tty
  1139. * @idx: the minor number
  1140. *
  1141. * Remvoe a tty object from the driver tables. The tty->index field
  1142. * will be set by the time this is called.
  1143. *
  1144. * Locking: tty_mutex for now
  1145. */
  1146. static void tty_driver_remove_tty(struct tty_driver *driver,
  1147. struct tty_struct *tty)
  1148. {
  1149. if (driver->ops->remove)
  1150. driver->ops->remove(driver, tty);
  1151. else
  1152. driver->ttys[tty->index] = NULL;
  1153. }
  1154. /*
  1155. * tty_reopen() - fast re-open of an open tty
  1156. * @tty - the tty to open
  1157. *
  1158. * Return 0 on success, -errno on error.
  1159. *
  1160. * Locking: tty_mutex must be held from the time the tty was found
  1161. * till this open completes.
  1162. */
  1163. static int tty_reopen(struct tty_struct *tty)
  1164. {
  1165. struct tty_driver *driver = tty->driver;
  1166. if (test_bit(TTY_CLOSING, &tty->flags))
  1167. return -EIO;
  1168. if (driver->type == TTY_DRIVER_TYPE_PTY &&
  1169. driver->subtype == PTY_TYPE_MASTER) {
  1170. /*
  1171. * special case for PTY masters: only one open permitted,
  1172. * and the slave side open count is incremented as well.
  1173. */
  1174. if (tty->count)
  1175. return -EIO;
  1176. tty->link->count++;
  1177. }
  1178. tty->count++;
  1179. tty->driver = driver; /* N.B. why do this every time?? */
  1180. mutex_lock(&tty->ldisc_mutex);
  1181. WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
  1182. mutex_unlock(&tty->ldisc_mutex);
  1183. return 0;
  1184. }
  1185. /**
  1186. * tty_init_dev - initialise a tty device
  1187. * @driver: tty driver we are opening a device on
  1188. * @idx: device index
  1189. * @ret_tty: returned tty structure
  1190. * @first_ok: ok to open a new device (used by ptmx)
  1191. *
  1192. * Prepare a tty device. This may not be a "new" clean device but
  1193. * could also be an active device. The pty drivers require special
  1194. * handling because of this.
  1195. *
  1196. * Locking:
  1197. * The function is called under the tty_mutex, which
  1198. * protects us from the tty struct or driver itself going away.
  1199. *
  1200. * On exit the tty device has the line discipline attached and
  1201. * a reference count of 1. If a pair was created for pty/tty use
  1202. * and the other was a pty master then it too has a reference count of 1.
  1203. *
  1204. * WSH 06/09/97: Rewritten to remove races and properly clean up after a
  1205. * failed open. The new code protects the open with a mutex, so it's
  1206. * really quite straightforward. The mutex locking can probably be
  1207. * relaxed for the (most common) case of reopening a tty.
  1208. */
  1209. struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
  1210. int first_ok)
  1211. {
  1212. struct tty_struct *tty;
  1213. int retval;
  1214. /* Check if pty master is being opened multiple times */
  1215. if (driver->subtype == PTY_TYPE_MASTER &&
  1216. (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) {
  1217. return ERR_PTR(-EIO);
  1218. }
  1219. /*
  1220. * First time open is complex, especially for PTY devices.
  1221. * This code guarantees that either everything succeeds and the
  1222. * TTY is ready for operation, or else the table slots are vacated
  1223. * and the allocated memory released. (Except that the termios
  1224. * and locked termios may be retained.)
  1225. */
  1226. if (!try_module_get(driver->owner))
  1227. return ERR_PTR(-ENODEV);
  1228. tty = alloc_tty_struct();
  1229. if (!tty)
  1230. goto fail_no_mem;
  1231. initialize_tty_struct(tty, driver, idx);
  1232. retval = tty_driver_install_tty(driver, tty);
  1233. if (retval < 0) {
  1234. free_tty_struct(tty);
  1235. module_put(driver->owner);
  1236. return ERR_PTR(retval);
  1237. }
  1238. /*
  1239. * Structures all installed ... call the ldisc open routines.
  1240. * If we fail here just call release_tty to clean up. No need
  1241. * to decrement the use counts, as release_tty doesn't care.
  1242. */
  1243. retval = tty_ldisc_setup(tty, tty->link);
  1244. if (retval)
  1245. goto release_mem_out;
  1246. return tty;
  1247. fail_no_mem:
  1248. module_put(driver->owner);
  1249. return ERR_PTR(-ENOMEM);
  1250. /* call the tty release_tty routine to clean out this slot */
  1251. release_mem_out:
  1252. if (printk_ratelimit())
  1253. printk(KERN_INFO "tty_init_dev: ldisc open failed, "
  1254. "clearing slot %d\n", idx);
  1255. release_tty(tty, idx);
  1256. return ERR_PTR(retval);
  1257. }
  1258. void tty_free_termios(struct tty_struct *tty)
  1259. {
  1260. struct ktermios *tp;
  1261. int idx = tty->index;
  1262. /* Kill this flag and push into drivers for locking etc */
  1263. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
  1264. /* FIXME: Locking on ->termios array */
  1265. tp = tty->termios;
  1266. tty->driver->termios[idx] = NULL;
  1267. kfree(tp);
  1268. }
  1269. }
  1270. EXPORT_SYMBOL(tty_free_termios);
  1271. void tty_shutdown(struct tty_struct *tty)
  1272. {
  1273. tty_driver_remove_tty(tty->driver, tty);
  1274. tty_free_termios(tty);
  1275. }
  1276. EXPORT_SYMBOL(tty_shutdown);
  1277. /**
  1278. * release_one_tty - release tty structure memory
  1279. * @kref: kref of tty we are obliterating
  1280. *
  1281. * Releases memory associated with a tty structure, and clears out the
  1282. * driver table slots. This function is called when a device is no longer
  1283. * in use. It also gets called when setup of a device fails.
  1284. *
  1285. * Locking:
  1286. * tty_mutex - sometimes only
  1287. * takes the file list lock internally when working on the list
  1288. * of ttys that the driver keeps.
  1289. *
  1290. * This method gets called from a work queue so that the driver private
  1291. * cleanup ops can sleep (needed for USB at least)
  1292. */
  1293. static void release_one_tty(struct work_struct *work)
  1294. {
  1295. struct tty_struct *tty =
  1296. container_of(work, struct tty_struct, hangup_work);
  1297. struct tty_driver *driver = tty->driver;
  1298. if (tty->ops->cleanup)
  1299. tty->ops->cleanup(tty);
  1300. tty->magic = 0;
  1301. tty_driver_kref_put(driver);
  1302. module_put(driver->owner);
  1303. spin_lock(&tty_files_lock);
  1304. list_del_init(&tty->tty_files);
  1305. spin_unlock(&tty_files_lock);
  1306. put_pid(tty->pgrp);
  1307. put_pid(tty->session);
  1308. free_tty_struct(tty);
  1309. }
  1310. static void queue_release_one_tty(struct kref *kref)
  1311. {
  1312. struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
  1313. if (tty->ops->shutdown)
  1314. tty->ops->shutdown(tty);
  1315. else
  1316. tty_shutdown(tty);
  1317. /* The hangup queue is now free so we can reuse it rather than
  1318. waste a chunk of memory for each port */
  1319. INIT_WORK(&tty->hangup_work, release_one_tty);
  1320. schedule_work(&tty->hangup_work);
  1321. }
  1322. /**
  1323. * tty_kref_put - release a tty kref
  1324. * @tty: tty device
  1325. *
  1326. * Release a reference to a tty device and if need be let the kref
  1327. * layer destruct the object for us
  1328. */
  1329. void tty_kref_put(struct tty_struct *tty)
  1330. {
  1331. if (tty)
  1332. kref_put(&tty->kref, queue_release_one_tty);
  1333. }
  1334. EXPORT_SYMBOL(tty_kref_put);
  1335. /**
  1336. * release_tty - release tty structure memory
  1337. *
  1338. * Release both @tty and a possible linked partner (think pty pair),
  1339. * and decrement the refcount of the backing module.
  1340. *
  1341. * Locking:
  1342. * tty_mutex - sometimes only
  1343. * takes the file list lock internally when working on the list
  1344. * of ttys that the driver keeps.
  1345. * FIXME: should we require tty_mutex is held here ??
  1346. *
  1347. */
  1348. static void release_tty(struct tty_struct *tty, int idx)
  1349. {
  1350. /* This should always be true but check for the moment */
  1351. WARN_ON(tty->index != idx);
  1352. if (tty->link)
  1353. tty_kref_put(tty->link);
  1354. tty_kref_put(tty);
  1355. }
  1356. /**
  1357. * tty_release - vfs callback for close
  1358. * @inode: inode of tty
  1359. * @filp: file pointer for handle to tty
  1360. *
  1361. * Called the last time each file handle is closed that references
  1362. * this tty. There may however be several such references.
  1363. *
  1364. * Locking:
  1365. * Takes bkl. See tty_release_dev
  1366. *
  1367. * Even releasing the tty structures is a tricky business.. We have
  1368. * to be very careful that the structures are all released at the
  1369. * same time, as interrupts might otherwise get the wrong pointers.
  1370. *
  1371. * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
  1372. * lead to double frees or releasing memory still in use.
  1373. */
  1374. int tty_release(struct inode *inode, struct file *filp)
  1375. {
  1376. struct tty_struct *tty = file_tty(filp);
  1377. struct tty_struct *o_tty;
  1378. int pty_master, tty_closing, o_tty_closing, do_sleep;
  1379. int devpts;
  1380. int idx;
  1381. char buf[64];
  1382. if (tty_paranoia_check(tty, inode, "tty_release_dev"))
  1383. return 0;
  1384. tty_lock();
  1385. check_tty_count(tty, "tty_release_dev");
  1386. __tty_fasync(-1, filp, 0);
  1387. idx = tty->index;
  1388. pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  1389. tty->driver->subtype == PTY_TYPE_MASTER);
  1390. devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
  1391. o_tty = tty->link;
  1392. #ifdef TTY_PARANOIA_CHECK
  1393. if (idx < 0 || idx >= tty->driver->num) {
  1394. printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
  1395. "free (%s)\n", tty->name);
  1396. tty_unlock();
  1397. return 0;
  1398. }
  1399. if (!devpts) {
  1400. if (tty != tty->driver->ttys[idx]) {
  1401. tty_unlock();
  1402. printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
  1403. "for (%s)\n", idx, tty->name);
  1404. return 0;
  1405. }
  1406. if (tty->termios != tty->driver->termios[idx]) {
  1407. tty_unlock();
  1408. printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
  1409. "for (%s)\n",
  1410. idx, tty->name);
  1411. return 0;
  1412. }
  1413. }
  1414. #endif
  1415. #ifdef TTY_DEBUG_HANGUP
  1416. printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...",
  1417. tty_name(tty, buf), tty->count);
  1418. #endif
  1419. #ifdef TTY_PARANOIA_CHECK
  1420. if (tty->driver->other &&
  1421. !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  1422. if (o_tty != tty->driver->other->ttys[idx]) {
  1423. tty_unlock();
  1424. printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
  1425. "not o_tty for (%s)\n",
  1426. idx, tty->name);
  1427. return 0 ;
  1428. }
  1429. if (o_tty->termios != tty->driver->other->termios[idx]) {
  1430. tty_unlock();
  1431. printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
  1432. "not o_termios for (%s)\n",
  1433. idx, tty->name);
  1434. return 0;
  1435. }
  1436. if (o_tty->link != tty) {
  1437. tty_unlock();
  1438. printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
  1439. return 0;
  1440. }
  1441. }
  1442. #endif
  1443. if (tty->ops->close)
  1444. tty->ops->close(tty, filp);
  1445. tty_unlock();
  1446. /*
  1447. * Sanity check: if tty->count is going to zero, there shouldn't be
  1448. * any waiters on tty->read_wait or tty->write_wait. We test the
  1449. * wait queues and kick everyone out _before_ actually starting to
  1450. * close. This ensures that we won't block while releasing the tty
  1451. * structure.
  1452. *
  1453. * The test for the o_tty closing is necessary, since the master and
  1454. * slave sides may close in any order. If the slave side closes out
  1455. * first, its count will be one, since the master side holds an open.
  1456. * Thus this test wouldn't be triggered at the time the slave closes,
  1457. * so we do it now.
  1458. *
  1459. * Note that it's possible for the tty to be opened again while we're
  1460. * flushing out waiters. By recalculating the closing flags before
  1461. * each iteration we avoid any problems.
  1462. */
  1463. while (1) {
  1464. /* Guard against races with tty->count changes elsewhere and
  1465. opens on /dev/tty */
  1466. mutex_lock(&tty_mutex);
  1467. tty_lock();
  1468. tty_closing = tty->count <= 1;
  1469. o_tty_closing = o_tty &&
  1470. (o_tty->count <= (pty_master ? 1 : 0));
  1471. do_sleep = 0;
  1472. if (tty_closing) {
  1473. if (waitqueue_active(&tty->read_wait)) {
  1474. wake_up_poll(&tty->read_wait, POLLIN);
  1475. do_sleep++;
  1476. }
  1477. if (waitqueue_active(&tty->write_wait)) {
  1478. wake_up_poll(&tty->write_wait, POLLOUT);
  1479. do_sleep++;
  1480. }
  1481. }
  1482. if (o_tty_closing) {
  1483. if (waitqueue_active(&o_tty->read_wait)) {
  1484. wake_up_poll(&o_tty->read_wait, POLLIN);
  1485. do_sleep++;
  1486. }
  1487. if (waitqueue_active(&o_tty->write_wait)) {
  1488. wake_up_poll(&o_tty->write_wait, POLLOUT);
  1489. do_sleep++;
  1490. }
  1491. }
  1492. if (!do_sleep)
  1493. break;
  1494. printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
  1495. "active!\n", tty_name(tty, buf));
  1496. tty_unlock();
  1497. mutex_unlock(&tty_mutex);
  1498. schedule();
  1499. }
  1500. /*
  1501. * The closing flags are now consistent with the open counts on
  1502. * both sides, and we've completed the last operation that could
  1503. * block, so it's safe to proceed with closing.
  1504. */
  1505. if (pty_master) {
  1506. if (--o_tty->count < 0) {
  1507. printk(KERN_WARNING "tty_release_dev: bad pty slave count "
  1508. "(%d) for %s\n",
  1509. o_tty->count, tty_name(o_tty, buf));
  1510. o_tty->count = 0;
  1511. }
  1512. }
  1513. if (--tty->count < 0) {
  1514. printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n",
  1515. tty->count, tty_name(tty, buf));
  1516. tty->count = 0;
  1517. }
  1518. /*
  1519. * We've decremented tty->count, so we need to remove this file
  1520. * descriptor off the tty->tty_files list; this serves two
  1521. * purposes:
  1522. * - check_tty_count sees the correct number of file descriptors
  1523. * associated with this tty.
  1524. * - do_tty_hangup no longer sees this file descriptor as
  1525. * something that needs to be handled for hangups.
  1526. */
  1527. tty_del_file(filp);
  1528. /*
  1529. * Perform some housekeeping before deciding whether to return.
  1530. *
  1531. * Set the TTY_CLOSING flag if this was the last open. In the
  1532. * case of a pty we may have to wait around for the other side
  1533. * to close, and TTY_CLOSING makes sure we can't be reopened.
  1534. */
  1535. if (tty_closing)
  1536. set_bit(TTY_CLOSING, &tty->flags);
  1537. if (o_tty_closing)
  1538. set_bit(TTY_CLOSING, &o_tty->flags);
  1539. /*
  1540. * If _either_ side is closing, make sure there aren't any
  1541. * processes that still think tty or o_tty is their controlling
  1542. * tty.
  1543. */
  1544. if (tty_closing || o_tty_closing) {
  1545. read_lock(&tasklist_lock);
  1546. session_clear_tty(tty->session);
  1547. if (o_tty)
  1548. session_clear_tty(o_tty->session);
  1549. read_unlock(&tasklist_lock);
  1550. }
  1551. mutex_unlock(&tty_mutex);
  1552. /* check whether both sides are closing ... */
  1553. if (!tty_closing || (o_tty && !o_tty_closing)) {
  1554. tty_unlock();
  1555. return 0;
  1556. }
  1557. #ifdef TTY_DEBUG_HANGUP
  1558. printk(KERN_DEBUG "freeing tty structure...");
  1559. #endif
  1560. /*
  1561. * Ask the line discipline code to release its structures
  1562. */
  1563. tty_ldisc_release(tty, o_tty);
  1564. /*
  1565. * The release_tty function takes care of the details of clearing
  1566. * the slots and preserving the termios structure.
  1567. */
  1568. release_tty(tty, idx);
  1569. /* Make this pty number available for reallocation */
  1570. if (devpts)
  1571. devpts_kill_index(inode, idx);
  1572. tty_unlock();
  1573. return 0;
  1574. }
  1575. /**
  1576. * tty_open - open a tty device
  1577. * @inode: inode of device file
  1578. * @filp: file pointer to tty
  1579. *
  1580. * tty_open and tty_release keep up the tty count that contains the
  1581. * number of opens done on a tty. We cannot use the inode-count, as
  1582. * different inodes might point to the same tty.
  1583. *
  1584. * Open-counting is needed for pty masters, as well as for keeping
  1585. * track of serial lines: DTR is dropped when the last close happens.
  1586. * (This is not done solely through tty->count, now. - Ted 1/27/92)
  1587. *
  1588. * The termios state of a pty is reset on first open so that
  1589. * settings don't persist across reuse.
  1590. *
  1591. * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work.
  1592. * tty->count should protect the rest.
  1593. * ->siglock protects ->signal/->sighand
  1594. */
  1595. static int tty_open(struct inode *inode, struct file *filp)
  1596. {
  1597. struct tty_struct *tty = NULL;
  1598. int noctty, retval;
  1599. struct tty_driver *driver;
  1600. int index;
  1601. dev_t device = inode->i_rdev;
  1602. unsigned saved_flags = filp->f_flags;
  1603. nonseekable_open(inode, filp);
  1604. retry_open:
  1605. noctty = filp->f_flags & O_NOCTTY;
  1606. index = -1;
  1607. retval = 0;
  1608. mutex_lock(&tty_mutex);
  1609. tty_lock();
  1610. if (device == MKDEV(TTYAUX_MAJOR, 0)) {
  1611. tty = get_current_tty();
  1612. if (!tty) {
  1613. tty_unlock();
  1614. mutex_unlock(&tty_mutex);
  1615. return -ENXIO;
  1616. }
  1617. driver = tty_driver_kref_get(tty->driver);
  1618. index = tty->index;
  1619. filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
  1620. /* noctty = 1; */
  1621. /* FIXME: Should we take a driver reference ? */
  1622. tty_kref_put(tty);
  1623. goto got_driver;
  1624. }
  1625. #ifdef CONFIG_VT
  1626. if (device == MKDEV(TTY_MAJOR, 0)) {
  1627. extern struct tty_driver *console_driver;
  1628. driver = tty_driver_kref_get(console_driver);
  1629. index = fg_console;
  1630. noctty = 1;
  1631. goto got_driver;
  1632. }
  1633. #endif
  1634. if (device == MKDEV(TTYAUX_MAJOR, 1)) {
  1635. struct tty_driver *console_driver = console_device(&index);
  1636. if (console_driver) {
  1637. driver = tty_driver_kref_get(console_driver);
  1638. if (driver) {
  1639. /* Don't let /dev/console block */
  1640. filp->f_flags |= O_NONBLOCK;
  1641. noctty = 1;
  1642. goto got_driver;
  1643. }
  1644. }
  1645. tty_unlock();
  1646. mutex_unlock(&tty_mutex);
  1647. return -ENODEV;
  1648. }
  1649. driver = get_tty_driver(device, &index);
  1650. if (!driver) {
  1651. tty_unlock();
  1652. mutex_unlock(&tty_mutex);
  1653. return -ENODEV;
  1654. }
  1655. got_driver:
  1656. if (!tty) {
  1657. /* check whether we're reopening an existing tty */
  1658. tty = tty_driver_lookup_tty(driver, inode, index);
  1659. if (IS_ERR(tty)) {
  1660. tty_unlock();
  1661. mutex_unlock(&tty_mutex);
  1662. return PTR_ERR(tty);
  1663. }
  1664. }
  1665. if (tty) {
  1666. retval = tty_reopen(tty);
  1667. if (retval)
  1668. tty = ERR_PTR(retval);
  1669. } else
  1670. tty = tty_init_dev(driver, index, 0);
  1671. mutex_unlock(&tty_mutex);
  1672. tty_driver_kref_put(driver);
  1673. if (IS_ERR(tty)) {
  1674. tty_unlock();
  1675. return PTR_ERR(tty);
  1676. }
  1677. retval = tty_add_file(tty, filp);
  1678. if (retval) {
  1679. tty_unlock();
  1680. return retval;
  1681. }
  1682. check_tty_count(tty, "tty_open");
  1683. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  1684. tty->driver->subtype == PTY_TYPE_MASTER)
  1685. noctty = 1;
  1686. #ifdef TTY_DEBUG_HANGUP
  1687. printk(KERN_DEBUG "opening %s...", tty->name);
  1688. #endif
  1689. if (!retval) {
  1690. if (tty->ops->open)
  1691. retval = tty->ops->open(tty, filp);
  1692. else
  1693. retval = -ENODEV;
  1694. }
  1695. filp->f_flags = saved_flags;
  1696. if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
  1697. !capable(CAP_SYS_ADMIN))
  1698. retval = -EBUSY;
  1699. if (retval) {
  1700. #ifdef TTY_DEBUG_HANGUP
  1701. printk(KERN_DEBUG "error %d in opening %s...", retval,
  1702. tty->name);
  1703. #endif
  1704. tty_unlock(); /* need to call tty_release without BTM */
  1705. tty_release(inode, filp);
  1706. if (retval != -ERESTARTSYS)
  1707. return retval;
  1708. if (signal_pending(current))
  1709. return retval;
  1710. schedule();
  1711. /*
  1712. * Need to reset f_op in case a hangup happened.
  1713. */
  1714. tty_lock();
  1715. if (filp->f_op == &hung_up_tty_fops)
  1716. filp->f_op = &tty_fops;
  1717. tty_unlock();
  1718. goto retry_open;
  1719. }
  1720. tty_unlock();
  1721. mutex_lock(&tty_mutex);
  1722. tty_lock();
  1723. spin_lock_irq(&current->sighand->siglock);
  1724. if (!noctty &&
  1725. current->signal->leader &&
  1726. !current->signal->tty &&
  1727. tty->session == NULL)
  1728. __proc_set_tty(current, tty);
  1729. spin_unlock_irq(&current->sighand->siglock);
  1730. tty_unlock();
  1731. mutex_unlock(&tty_mutex);
  1732. return 0;
  1733. }
  1734. /**
  1735. * tty_poll - check tty status
  1736. * @filp: file being polled
  1737. * @wait: poll wait structures to update
  1738. *
  1739. * Call the line discipline polling method to obtain the poll
  1740. * status of the device.
  1741. *
  1742. * Locking: locks called line discipline but ldisc poll method
  1743. * may be re-entered freely by other callers.
  1744. */
  1745. static unsigned int tty_poll(struct file *filp, poll_table *wait)
  1746. {
  1747. struct tty_struct *tty = file_tty(filp);
  1748. struct tty_ldisc *ld;
  1749. int ret = 0;
  1750. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
  1751. return 0;
  1752. ld = tty_ldisc_ref_wait(tty);
  1753. if (ld->ops->poll)
  1754. ret = (ld->ops->poll)(tty, filp, wait);
  1755. tty_ldisc_deref(ld);
  1756. return ret;
  1757. }
  1758. static int __tty_fasync(int fd, struct file *filp, int on)
  1759. {
  1760. struct tty_struct *tty = file_tty(filp);
  1761. unsigned long flags;
  1762. int retval = 0;
  1763. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
  1764. goto out;
  1765. retval = fasync_helper(fd, filp, on, &tty->fasync);
  1766. if (retval <= 0)
  1767. goto out;
  1768. if (on) {
  1769. enum pid_type type;
  1770. struct pid *pid;
  1771. if (!waitqueue_active(&tty->read_wait))
  1772. tty->minimum_to_wake = 1;
  1773. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1774. if (tty->pgrp) {
  1775. pid = tty->pgrp;
  1776. type = PIDTYPE_PGID;
  1777. } else {
  1778. pid = task_pid(current);
  1779. type = PIDTYPE_PID;
  1780. }
  1781. get_pid(pid);
  1782. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1783. retval = __f_setown(filp, pid, type, 0);
  1784. put_pid(pid);
  1785. if (retval)
  1786. goto out;
  1787. } else {
  1788. if (!tty->fasync && !waitqueue_active(&tty->read_wait))
  1789. tty->minimum_to_wake = N_TTY_BUF_SIZE;
  1790. }
  1791. retval = 0;
  1792. out:
  1793. return retval;
  1794. }
  1795. static int tty_fasync(int fd, struct file *filp, int on)
  1796. {
  1797. int retval;
  1798. tty_lock();
  1799. retval = __tty_fasync(fd, filp, on);
  1800. tty_unlock();
  1801. return retval;
  1802. }
  1803. /**
  1804. * tiocsti - fake input character
  1805. * @tty: tty to fake input into
  1806. * @p: pointer to character
  1807. *
  1808. * Fake input to a tty device. Does the necessary locking and
  1809. * input management.
  1810. *
  1811. * FIXME: does not honour flow control ??
  1812. *
  1813. * Locking:
  1814. * Called functions take tty_ldisc_lock
  1815. * current->signal->tty check is safe without locks
  1816. *
  1817. * FIXME: may race normal receive processing
  1818. */
  1819. static int tiocsti(struct tty_struct *tty, char __user *p)
  1820. {
  1821. char ch, mbz = 0;
  1822. struct tty_ldisc *ld;
  1823. if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
  1824. return -EPERM;
  1825. if (get_user(ch, p))
  1826. return -EFAULT;
  1827. tty_audit_tiocsti(tty, ch);
  1828. ld = tty_ldisc_ref_wait(tty);
  1829. ld->ops->receive_buf(tty, &ch, &mbz, 1);
  1830. tty_ldisc_deref(ld);
  1831. return 0;
  1832. }
  1833. /**
  1834. * tiocgwinsz - implement window query ioctl
  1835. * @tty; tty
  1836. * @arg: user buffer for result
  1837. *
  1838. * Copies the kernel idea of the window size into the user buffer.
  1839. *
  1840. * Locking: tty->termios_mutex is taken to ensure the winsize data
  1841. * is consistent.
  1842. */
  1843. static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
  1844. {
  1845. int err;
  1846. mutex_lock(&tty->termios_mutex);
  1847. err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
  1848. mutex_unlock(&tty->termios_mutex);
  1849. return err ? -EFAULT: 0;
  1850. }
  1851. /**
  1852. * tty_do_resize - resize event
  1853. * @tty: tty being resized
  1854. * @rows: rows (character)
  1855. * @cols: cols (character)
  1856. *
  1857. * Update the termios variables and send the necessary signals to
  1858. * peform a terminal resize correctly
  1859. */
  1860. int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
  1861. {
  1862. struct pid *pgrp;
  1863. unsigned long flags;
  1864. /* Lock the tty */
  1865. mutex_lock(&tty->termios_mutex);
  1866. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  1867. goto done;
  1868. /* Get the PID values and reference them so we can
  1869. avoid holding the tty ctrl lock while sending signals */
  1870. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1871. pgrp = get_pid(tty->pgrp);
  1872. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1873. if (pgrp)
  1874. kill_pgrp(pgrp, SIGWINCH, 1);
  1875. put_pid(pgrp);
  1876. tty->winsize = *ws;
  1877. done:
  1878. mutex_unlock(&tty->termios_mutex);
  1879. return 0;
  1880. }
  1881. /**
  1882. * tiocswinsz - implement window size set ioctl
  1883. * @tty; tty side of tty
  1884. * @arg: user buffer for result
  1885. *
  1886. * Copies the user idea of the window size to the kernel. Traditionally
  1887. * this is just advisory information but for the Linux console it
  1888. * actually has driver level meaning and triggers a VC resize.
  1889. *
  1890. * Locking:
  1891. * Driver dependant. The default do_resize method takes the
  1892. * tty termios mutex and ctrl_lock. The console takes its own lock
  1893. * then calls into the default method.
  1894. */
  1895. static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
  1896. {
  1897. struct winsize tmp_ws;
  1898. if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
  1899. return -EFAULT;
  1900. if (tty->ops->resize)
  1901. return tty->ops->resize(tty, &tmp_ws);
  1902. else
  1903. return tty_do_resize(tty, &tmp_ws);
  1904. }
  1905. /**
  1906. * tioccons - allow admin to move logical console
  1907. * @file: the file to become console
  1908. *
  1909. * Allow the adminstrator to move the redirected console device
  1910. *
  1911. * Locking: uses redirect_lock to guard the redirect information
  1912. */
  1913. static int tioccons(struct file *file)
  1914. {
  1915. if (!capable(CAP_SYS_ADMIN))
  1916. return -EPERM;
  1917. if (file->f_op->write == redirected_tty_write) {
  1918. struct file *f;
  1919. spin_lock(&redirect_lock);
  1920. f = redirect;
  1921. redirect = NULL;
  1922. spin_unlock(&redirect_lock);
  1923. if (f)
  1924. fput(f);
  1925. return 0;
  1926. }
  1927. spin_lock(&redirect_lock);
  1928. if (redirect) {
  1929. spin_unlock(&redirect_lock);
  1930. return -EBUSY;
  1931. }
  1932. get_file(file);
  1933. redirect = file;
  1934. spin_unlock(&redirect_lock);
  1935. return 0;
  1936. }
  1937. /**
  1938. * fionbio - non blocking ioctl
  1939. * @file: file to set blocking value
  1940. * @p: user parameter
  1941. *
  1942. * Historical tty interfaces had a blocking control ioctl before
  1943. * the generic functionality existed. This piece of history is preserved
  1944. * in the expected tty API of posix OS's.
  1945. *
  1946. * Locking: none, the open file handle ensures it won't go away.
  1947. */
  1948. static int fionbio(struct file *file, int __user *p)
  1949. {
  1950. int nonblock;
  1951. if (get_user(nonblock, p))
  1952. return -EFAULT;
  1953. spin_lock(&file->f_lock);
  1954. if (nonblock)
  1955. file->f_flags |= O_NONBLOCK;
  1956. else
  1957. file->f_flags &= ~O_NONBLOCK;
  1958. spin_unlock(&file->f_lock);
  1959. return 0;
  1960. }
  1961. /**
  1962. * tiocsctty - set controlling tty
  1963. * @tty: tty structure
  1964. * @arg: user argument
  1965. *
  1966. * This ioctl is used to manage job control. It permits a session
  1967. * leader to set this tty as the controlling tty for the session.
  1968. *
  1969. * Locking:
  1970. * Takes tty_mutex() to protect tty instance
  1971. * Takes tasklist_lock internally to walk sessions
  1972. * Takes ->siglock() when updating signal->tty
  1973. */
  1974. static int tiocsctty(struct tty_struct *tty, int arg)
  1975. {
  1976. int ret = 0;
  1977. if (current->signal->leader && (task_session(current) == tty->session))
  1978. return ret;
  1979. mutex_lock(&tty_mutex);
  1980. /*
  1981. * The process must be a session leader and
  1982. * not have a controlling tty already.
  1983. */
  1984. if (!current->signal->leader || current->signal->tty) {
  1985. ret = -EPERM;
  1986. goto unlock;
  1987. }
  1988. if (tty->session) {
  1989. /*
  1990. * This tty is already the controlling
  1991. * tty for another session group!
  1992. */
  1993. if (arg == 1 && capable(CAP_SYS_ADMIN)) {
  1994. /*
  1995. * Steal it away
  1996. */
  1997. read_lock(&tasklist_lock);
  1998. session_clear_tty(tty->session);
  1999. read_unlock(&tasklist_lock);
  2000. } else {
  2001. ret = -EPERM;
  2002. goto unlock;
  2003. }
  2004. }
  2005. proc_set_tty(current, tty);
  2006. unlock:
  2007. mutex_unlock(&tty_mutex);
  2008. return ret;
  2009. }
  2010. /**
  2011. * tty_get_pgrp - return a ref counted pgrp pid
  2012. * @tty: tty to read
  2013. *
  2014. * Returns a refcounted instance of the pid struct for the process
  2015. * group controlling the tty.
  2016. */
  2017. struct pid *tty_get_pgrp(struct tty_struct *tty)
  2018. {
  2019. unsigned long flags;
  2020. struct pid *pgrp;
  2021. spin_lock_irqsave(&tty->ctrl_lock, flags);
  2022. pgrp = get_pid(tty->pgrp);
  2023. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  2024. return pgrp;
  2025. }
  2026. EXPORT_SYMBOL_GPL(tty_get_pgrp);
  2027. /**
  2028. * tiocgpgrp - get process group
  2029. * @tty: tty passed by user
  2030. * @real_tty: tty side of the tty pased by the user if a pty else the tty
  2031. * @p: returned pid
  2032. *
  2033. * Obtain the process group of the tty. If there is no process group
  2034. * return an error.
  2035. *
  2036. * Locking: none. Reference to current->signal->tty is safe.
  2037. */
  2038. static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2039. {
  2040. struct pid *pid;
  2041. int ret;
  2042. /*
  2043. * (tty == real_tty) is a cheap way of
  2044. * testing if the tty is NOT a master pty.
  2045. */
  2046. if (tty == real_tty && current->signal->tty != real_tty)
  2047. return -ENOTTY;
  2048. pid = tty_get_pgrp(real_tty);
  2049. ret = put_user(pid_vnr(pid), p);
  2050. put_pid(pid);
  2051. return ret;
  2052. }
  2053. /**
  2054. * tiocspgrp - attempt to set process group
  2055. * @tty: tty passed by user
  2056. * @real_tty: tty side device matching tty passed by user
  2057. * @p: pid pointer
  2058. *
  2059. * Set the process group of the tty to the session passed. Only
  2060. * permitted where the tty session is our session.
  2061. *
  2062. * Locking: RCU, ctrl lock
  2063. */
  2064. static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2065. {
  2066. struct pid *pgrp;
  2067. pid_t pgrp_nr;
  2068. int retval = tty_check_change(real_tty);
  2069. unsigned long flags;
  2070. if (retval == -EIO)
  2071. return -ENOTTY;
  2072. if (retval)
  2073. return retval;
  2074. if (!current->signal->tty ||
  2075. (current->signal->tty != real_tty) ||
  2076. (real_tty->session != task_session(current)))
  2077. return -ENOTTY;
  2078. if (get_user(pgrp_nr, p))
  2079. return -EFAULT;
  2080. if (pgrp_nr < 0)
  2081. return -EINVAL;
  2082. rcu_read_lock();
  2083. pgrp = find_vpid(pgrp_nr);
  2084. retval = -ESRCH;
  2085. if (!pgrp)
  2086. goto out_unlock;
  2087. retval = -EPERM;
  2088. if (session_of_pgrp(pgrp) != task_session(current))
  2089. goto out_unlock;
  2090. retval = 0;
  2091. spin_lock_irqsave(&tty->ctrl_lock, flags);
  2092. put_pid(real_tty->pgrp);
  2093. real_tty->pgrp = get_pid(pgrp);
  2094. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  2095. out_unlock:
  2096. rcu_read_unlock();
  2097. return retval;
  2098. }
  2099. /**
  2100. * tiocgsid - get session id
  2101. * @tty: tty passed by user
  2102. * @real_tty: tty side of the tty pased by the user if a pty else the tty
  2103. * @p: pointer to returned session id
  2104. *
  2105. * Obtain the session id of the tty. If there is no session
  2106. * return an error.
  2107. *
  2108. * Locking: none. Reference to current->signal->tty is safe.
  2109. */
  2110. static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2111. {
  2112. /*
  2113. * (tty == real_tty) is a cheap way of
  2114. * testing if the tty is NOT a master pty.
  2115. */
  2116. if (tty == real_tty && current->signal->tty != real_tty)
  2117. return -ENOTTY;
  2118. if (!real_tty->session)
  2119. return -ENOTTY;
  2120. return put_user(pid_vnr(real_tty->session), p);
  2121. }
  2122. /**
  2123. * tiocsetd - set line discipline
  2124. * @tty: tty device
  2125. * @p: pointer to user data
  2126. *
  2127. * Set the line discipline according to user request.
  2128. *
  2129. * Locking: see tty_set_ldisc, this function is just a helper
  2130. */
  2131. static int tiocsetd(struct tty_struct *tty, int __user *p)
  2132. {
  2133. int ldisc;
  2134. int ret;
  2135. if (get_user(ldisc, p))
  2136. return -EFAULT;
  2137. ret = tty_set_ldisc(tty, ldisc);
  2138. return ret;
  2139. }
  2140. /**
  2141. * send_break - performed time break
  2142. * @tty: device to break on
  2143. * @duration: timeout in mS
  2144. *
  2145. * Perform a timed break on hardware that lacks its own driver level
  2146. * timed break functionality.
  2147. *
  2148. * Locking:
  2149. * atomic_write_lock serializes
  2150. *
  2151. */
  2152. static int send_break(struct tty_struct *tty, unsigned int duration)
  2153. {
  2154. int retval;
  2155. if (tty->ops->break_ctl == NULL)
  2156. return 0;
  2157. if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
  2158. retval = tty->ops->break_ctl(tty, duration);
  2159. else {
  2160. /* Do the work ourselves */
  2161. if (tty_write_lock(tty, 0) < 0)
  2162. return -EINTR;
  2163. retval = tty->ops->break_ctl(tty, -1);
  2164. if (retval)
  2165. goto out;
  2166. if (!signal_pending(current))
  2167. msleep_interruptible(duration);
  2168. retval = tty->ops->break_ctl(tty, 0);
  2169. out:
  2170. tty_write_unlock(tty);
  2171. if (signal_pending(current))
  2172. retval = -EINTR;
  2173. }
  2174. return retval;
  2175. }
  2176. /**
  2177. * tty_tiocmget - get modem status
  2178. * @tty: tty device
  2179. * @file: user file pointer
  2180. * @p: pointer to result
  2181. *
  2182. * Obtain the modem status bits from the tty driver if the feature
  2183. * is supported. Return -EINVAL if it is not available.
  2184. *
  2185. * Locking: none (up to the driver)
  2186. */
  2187. static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
  2188. {
  2189. int retval = -EINVAL;
  2190. if (tty->ops->tiocmget) {
  2191. retval = tty->ops->tiocmget(tty, file);
  2192. if (retval >= 0)
  2193. retval = put_user(retval, p);
  2194. }
  2195. return retval;
  2196. }
  2197. /**
  2198. * tty_tiocmset - set modem status
  2199. * @tty: tty device
  2200. * @file: user file pointer
  2201. * @cmd: command - clear bits, set bits or set all
  2202. * @p: pointer to desired bits
  2203. *
  2204. * Set the modem status bits from the tty driver if the feature
  2205. * is supported. Return -EINVAL if it is not available.
  2206. *
  2207. * Locking: none (up to the driver)
  2208. */
  2209. static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
  2210. unsigned __user *p)
  2211. {
  2212. int retval;
  2213. unsigned int set, clear, val;
  2214. if (tty->ops->tiocmset == NULL)
  2215. return -EINVAL;
  2216. retval = get_user(val, p);
  2217. if (retval)
  2218. return retval;
  2219. set = clear = 0;
  2220. switch (cmd) {
  2221. case TIOCMBIS:
  2222. set = val;
  2223. break;
  2224. case TIOCMBIC:
  2225. clear = val;
  2226. break;
  2227. case TIOCMSET:
  2228. set = val;
  2229. clear = ~val;
  2230. break;
  2231. }
  2232. set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
  2233. clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
  2234. return tty->ops->tiocmset(tty, file, set, clear);
  2235. }
  2236. static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
  2237. {
  2238. int retval = -EINVAL;
  2239. struct serial_icounter_struct icount;
  2240. memset(&icount, 0, sizeof(icount));
  2241. if (tty->ops->get_icount)
  2242. retval = tty->ops->get_icount(tty, &icount);
  2243. if (retval != 0)
  2244. return retval;
  2245. if (copy_to_user(arg, &icount, sizeof(icount)))
  2246. return -EFAULT;
  2247. return 0;
  2248. }
  2249. struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
  2250. {
  2251. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  2252. tty->driver->subtype == PTY_TYPE_MASTER)
  2253. tty = tty->link;
  2254. return tty;
  2255. }
  2256. EXPORT_SYMBOL(tty_pair_get_tty);
  2257. struct tty_struct *tty_pair_get_pty(struct tty_struct *tty)
  2258. {
  2259. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  2260. tty->driver->subtype == PTY_TYPE_MASTER)
  2261. return tty;
  2262. return tty->link;
  2263. }
  2264. EXPORT_SYMBOL(tty_pair_get_pty);
  2265. /*
  2266. * Split this up, as gcc can choke on it otherwise..
  2267. */
  2268. long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2269. {
  2270. struct tty_struct *tty = file_tty(file);
  2271. struct tty_struct *real_tty;
  2272. void __user *p = (void __user *)arg;
  2273. int retval;
  2274. struct tty_ldisc *ld;
  2275. struct inode *inode = file->f_dentry->d_inode;
  2276. if (tty_paranoia_check(tty, inode, "tty_ioctl"))
  2277. return -EINVAL;
  2278. real_tty = tty_pair_get_tty(tty);
  2279. /*
  2280. * Factor out some common prep work
  2281. */
  2282. switch (cmd) {
  2283. case TIOCSETD:
  2284. case TIOCSBRK:
  2285. case TIOCCBRK:
  2286. case TCSBRK:
  2287. case TCSBRKP:
  2288. retval = tty_check_change(tty);
  2289. if (retval)
  2290. return retval;
  2291. if (cmd != TIOCCBRK) {
  2292. tty_wait_until_sent(tty, 0);
  2293. if (signal_pending(current))
  2294. return -EINTR;
  2295. }
  2296. break;
  2297. }
  2298. /*
  2299. * Now do the stuff.
  2300. */
  2301. switch (cmd) {
  2302. case TIOCSTI:
  2303. return tiocsti(tty, p);
  2304. case TIOCGWINSZ:
  2305. return tiocgwinsz(real_tty, p);
  2306. case TIOCSWINSZ:
  2307. return tiocswinsz(real_tty, p);
  2308. case TIOCCONS:
  2309. return real_tty != tty ? -EINVAL : tioccons(file);
  2310. case FIONBIO:
  2311. return fionbio(file, p);
  2312. case TIOCEXCL:
  2313. set_bit(TTY_EXCLUSIVE, &tty->flags);
  2314. return 0;
  2315. case TIOCNXCL:
  2316. clear_bit(TTY_EXCLUSIVE, &tty->flags);
  2317. return 0;
  2318. case TIOCNOTTY:
  2319. if (current->signal->tty != tty)
  2320. return -ENOTTY;
  2321. no_tty();
  2322. return 0;
  2323. case TIOCSCTTY:
  2324. return tiocsctty(tty, arg);
  2325. case TIOCGPGRP:
  2326. return tiocgpgrp(tty, real_tty, p);
  2327. case TIOCSPGRP:
  2328. return tiocspgrp(tty, real_tty, p);
  2329. case TIOCGSID:
  2330. return tiocgsid(tty, real_tty, p);
  2331. case TIOCGETD:
  2332. return put_user(tty->ldisc->ops->num, (int __user *)p);
  2333. case TIOCSETD:
  2334. return tiocsetd(tty, p);
  2335. /*
  2336. * Break handling
  2337. */
  2338. case TIOCSBRK: /* Turn break on, unconditionally */
  2339. if (tty->ops->break_ctl)
  2340. return tty->ops->break_ctl(tty, -1);
  2341. return 0;
  2342. case TIOCCBRK: /* Turn break off, unconditionally */
  2343. if (tty->ops->break_ctl)
  2344. return tty->ops->break_ctl(tty, 0);
  2345. return 0;
  2346. case TCSBRK: /* SVID version: non-zero arg --> no break */
  2347. /* non-zero arg means wait for all output data
  2348. * to be sent (performed above) but don't send break.
  2349. * This is used by the tcdrain() termios function.
  2350. */
  2351. if (!arg)
  2352. return send_break(tty, 250);
  2353. return 0;
  2354. case TCSBRKP: /* support for POSIX tcsendbreak() */
  2355. return send_break(tty, arg ? arg*100 : 250);
  2356. case TIOCMGET:
  2357. return tty_tiocmget(tty, file, p);
  2358. case TIOCMSET:
  2359. case TIOCMBIC:
  2360. case TIOCMBIS:
  2361. return tty_tiocmset(tty, file, cmd, p);
  2362. case TIOCGICOUNT:
  2363. retval = tty_tiocgicount(tty, p);
  2364. /* For the moment allow fall through to the old method */
  2365. if (retval != -EINVAL)
  2366. return retval;
  2367. break;
  2368. case TCFLSH:
  2369. switch (arg) {
  2370. case TCIFLUSH:
  2371. case TCIOFLUSH:
  2372. /* flush tty buffer and allow ldisc to process ioctl */
  2373. tty_buffer_flush(tty);
  2374. break;
  2375. }
  2376. break;
  2377. }
  2378. if (tty->ops->ioctl) {
  2379. retval = (tty->ops->ioctl)(tty, file, cmd, arg);
  2380. if (retval != -ENOIOCTLCMD)
  2381. return retval;
  2382. }
  2383. ld = tty_ldisc_ref_wait(tty);
  2384. retval = -EINVAL;
  2385. if (ld->ops->ioctl) {
  2386. retval = ld->ops->ioctl(tty, file, cmd, arg);
  2387. if (retval == -ENOIOCTLCMD)
  2388. retval = -EINVAL;
  2389. }
  2390. tty_ldisc_deref(ld);
  2391. return retval;
  2392. }
  2393. #ifdef CONFIG_COMPAT
  2394. static long tty_compat_ioctl(struct file *file, unsigned int cmd,
  2395. unsigned long arg)
  2396. {
  2397. struct inode *inode = file->f_dentry->d_inode;
  2398. struct tty_struct *tty = file_tty(file);
  2399. struct tty_ldisc *ld;
  2400. int retval = -ENOIOCTLCMD;
  2401. if (tty_paranoia_check(tty, inode, "tty_ioctl"))
  2402. return -EINVAL;
  2403. if (tty->ops->compat_ioctl) {
  2404. retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
  2405. if (retval != -ENOIOCTLCMD)
  2406. return retval;
  2407. }
  2408. ld = tty_ldisc_ref_wait(tty);
  2409. if (ld->ops->compat_ioctl)
  2410. retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
  2411. tty_ldisc_deref(ld);
  2412. return retval;
  2413. }
  2414. #endif
  2415. /*
  2416. * This implements the "Secure Attention Key" --- the idea is to
  2417. * prevent trojan horses by killing all processes associated with this
  2418. * tty when the user hits the "Secure Attention Key". Required for
  2419. * super-paranoid applications --- see the Orange Book for more details.
  2420. *
  2421. * This code could be nicer; ideally it should send a HUP, wait a few
  2422. * seconds, then send a INT, and then a KILL signal. But you then
  2423. * have to coordinate with the init process, since all processes associated
  2424. * with the current tty must be dead before the new getty is allowed
  2425. * to spawn.
  2426. *
  2427. * Now, if it would be correct ;-/ The current code has a nasty hole -
  2428. * it doesn't catch files in flight. We may send the descriptor to ourselves
  2429. * via AF_UNIX socket, close it and later fetch from socket. FIXME.
  2430. *
  2431. * Nasty bug: do_SAK is being called in interrupt context. This can
  2432. * deadlock. We punt it up to process context. AKPM - 16Mar2001
  2433. */
  2434. void __do_SAK(struct tty_struct *tty)
  2435. {
  2436. #ifdef TTY_SOFT_SAK
  2437. tty_hangup(tty);
  2438. #else
  2439. struct task_struct *g, *p;
  2440. struct pid *session;
  2441. int i;
  2442. struct file *filp;
  2443. struct fdtable *fdt;
  2444. if (!tty)
  2445. return;
  2446. session = tty->session;
  2447. tty_ldisc_flush(tty);
  2448. tty_driver_flush_buffer(tty);
  2449. read_lock(&tasklist_lock);
  2450. /* Kill the entire session */
  2451. do_each_pid_task(session, PIDTYPE_SID, p) {
  2452. printk(KERN_NOTICE "SAK: killed process %d"
  2453. " (%s): task_session(p)==tty->session\n",
  2454. task_pid_nr(p), p->comm);
  2455. send_sig(SIGKILL, p, 1);
  2456. } while_each_pid_task(session, PIDTYPE_SID, p);
  2457. /* Now kill any processes that happen to have the
  2458. * tty open.
  2459. */
  2460. do_each_thread(g, p) {
  2461. if (p->signal->tty == tty) {
  2462. printk(KERN_NOTICE "SAK: killed process %d"
  2463. " (%s): task_session(p)==tty->session\n",
  2464. task_pid_nr(p), p->comm);
  2465. send_sig(SIGKILL, p, 1);
  2466. continue;
  2467. }
  2468. task_lock(p);
  2469. if (p->files) {
  2470. /*
  2471. * We don't take a ref to the file, so we must
  2472. * hold ->file_lock instead.
  2473. */
  2474. spin_lock(&p->files->file_lock);
  2475. fdt = files_fdtable(p->files);
  2476. for (i = 0; i < fdt->max_fds; i++) {
  2477. filp = fcheck_files(p->files, i);
  2478. if (!filp)
  2479. continue;
  2480. if (filp->f_op->read == tty_read &&
  2481. file_tty(filp) == tty) {
  2482. printk(KERN_NOTICE "SAK: killed process %d"
  2483. " (%s): fd#%d opened to the tty\n",
  2484. task_pid_nr(p), p->comm, i);
  2485. force_sig(SIGKILL, p);
  2486. break;
  2487. }
  2488. }
  2489. spin_unlock(&p->files->file_lock);
  2490. }
  2491. task_unlock(p);
  2492. } while_each_thread(g, p);
  2493. read_unlock(&tasklist_lock);
  2494. #endif
  2495. }
  2496. static void do_SAK_work(struct work_struct *work)
  2497. {
  2498. struct tty_struct *tty =
  2499. container_of(work, struct tty_struct, SAK_work);
  2500. __do_SAK(tty);
  2501. }
  2502. /*
  2503. * The tq handling here is a little racy - tty->SAK_work may already be queued.
  2504. * Fortunately we don't need to worry, because if ->SAK_work is already queued,
  2505. * the values which we write to it will be identical to the values which it
  2506. * already has. --akpm
  2507. */
  2508. void do_SAK(struct tty_struct *tty)
  2509. {
  2510. if (!tty)
  2511. return;
  2512. schedule_work(&tty->SAK_work);
  2513. }
  2514. EXPORT_SYMBOL(do_SAK);
  2515. static int dev_match_devt(struct device *dev, void *data)
  2516. {
  2517. dev_t *devt = data;
  2518. return dev->devt == *devt;
  2519. }
  2520. /* Must put_device() after it's unused! */
  2521. static struct device *tty_get_device(struct tty_struct *tty)
  2522. {
  2523. dev_t devt = tty_devnum(tty);
  2524. return class_find_device(tty_class, NULL, &devt, dev_match_devt);
  2525. }
  2526. /**
  2527. * initialize_tty_struct
  2528. * @tty: tty to initialize
  2529. *
  2530. * This subroutine initializes a tty structure that has been newly
  2531. * allocated.
  2532. *
  2533. * Locking: none - tty in question must not be exposed at this point
  2534. */
  2535. void initialize_tty_struct(struct tty_struct *tty,
  2536. struct tty_driver *driver, int idx)
  2537. {
  2538. memset(tty, 0, sizeof(struct tty_struct));
  2539. kref_init(&tty->kref);
  2540. tty->magic = TTY_MAGIC;
  2541. tty_ldisc_init(tty);
  2542. tty->session = NULL;
  2543. tty->pgrp = NULL;
  2544. tty->overrun_time = jiffies;
  2545. tty->buf.head = tty->buf.tail = NULL;
  2546. tty_buffer_init(tty);
  2547. mutex_init(&tty->termios_mutex);
  2548. mutex_init(&tty->ldisc_mutex);
  2549. init_waitqueue_head(&tty->write_wait);
  2550. init_waitqueue_head(&tty->read_wait);
  2551. INIT_WORK(&tty->hangup_work, do_tty_hangup);
  2552. mutex_init(&tty->atomic_read_lock);
  2553. mutex_init(&tty->atomic_write_lock);
  2554. mutex_init(&tty->output_lock);
  2555. mutex_init(&tty->echo_lock);
  2556. spin_lock_init(&tty->read_lock);
  2557. spin_lock_init(&tty->ctrl_lock);
  2558. INIT_LIST_HEAD(&tty->tty_files);
  2559. INIT_WORK(&tty->SAK_work, do_SAK_work);
  2560. tty->driver = driver;
  2561. tty->ops = driver->ops;
  2562. tty->index = idx;
  2563. tty_line_name(driver, idx, tty->name);
  2564. tty->dev = tty_get_device(tty);
  2565. }
  2566. /**
  2567. * tty_put_char - write one character to a tty
  2568. * @tty: tty
  2569. * @ch: character
  2570. *
  2571. * Write one byte to the tty using the provided put_char method
  2572. * if present. Returns the number of characters successfully output.
  2573. *
  2574. * Note: the specific put_char operation in the driver layer may go
  2575. * away soon. Don't call it directly, use this method
  2576. */
  2577. int tty_put_char(struct tty_struct *tty, unsigned char ch)
  2578. {
  2579. if (tty->ops->put_char)
  2580. return tty->ops->put_char(tty, ch);
  2581. return tty->ops->write(tty, &ch, 1);
  2582. }
  2583. EXPORT_SYMBOL_GPL(tty_put_char);
  2584. struct class *tty_class;
  2585. /**
  2586. * tty_register_device - register a tty device
  2587. * @driver: the tty driver that describes the tty device
  2588. * @index: the index in the tty driver for this tty device
  2589. * @device: a struct device that is associated with this tty device.
  2590. * This field is optional, if there is no known struct device
  2591. * for this tty device it can be set to NULL safely.
  2592. *
  2593. * Returns a pointer to the struct device for this tty device
  2594. * (or ERR_PTR(-EFOO) on error).
  2595. *
  2596. * This call is required to be made to register an individual tty device
  2597. * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
  2598. * that bit is not set, this function should not be called by a tty
  2599. * driver.
  2600. *
  2601. * Locking: ??
  2602. */
  2603. struct device *tty_register_device(struct tty_driver *driver, unsigned index,
  2604. struct device *device)
  2605. {
  2606. char name[64];
  2607. dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
  2608. if (index >= driver->num) {
  2609. printk(KERN_ERR "Attempt to register invalid tty line number "
  2610. " (%d).\n", index);
  2611. return ERR_PTR(-EINVAL);
  2612. }
  2613. if (driver->type == TTY_DRIVER_TYPE_PTY)
  2614. pty_line_name(driver, index, name);
  2615. else
  2616. tty_line_name(driver, index, name);
  2617. return device_create(tty_class, device, dev, NULL, name);
  2618. }
  2619. EXPORT_SYMBOL(tty_register_device);
  2620. /**
  2621. * tty_unregister_device - unregister a tty device
  2622. * @driver: the tty driver that describes the tty device
  2623. * @index: the index in the tty driver for this tty device
  2624. *
  2625. * If a tty device is registered with a call to tty_register_device() then
  2626. * this function must be called when the tty device is gone.
  2627. *
  2628. * Locking: ??
  2629. */
  2630. void tty_unregister_device(struct tty_driver *driver, unsigned index)
  2631. {
  2632. device_destroy(tty_class,
  2633. MKDEV(driver->major, driver->minor_start) + index);
  2634. }
  2635. EXPORT_SYMBOL(tty_unregister_device);
  2636. struct tty_driver *alloc_tty_driver(int lines)
  2637. {
  2638. struct tty_driver *driver;
  2639. driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
  2640. if (driver) {
  2641. kref_init(&driver->kref);
  2642. driver->magic = TTY_DRIVER_MAGIC;
  2643. driver->num = lines;
  2644. /* later we'll move allocation of tables here */
  2645. }
  2646. return driver;
  2647. }
  2648. EXPORT_SYMBOL(alloc_tty_driver);
  2649. static void destruct_tty_driver(struct kref *kref)
  2650. {
  2651. struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
  2652. int i;
  2653. struct ktermios *tp;
  2654. void *p;
  2655. if (driver->flags & TTY_DRIVER_INSTALLED) {
  2656. /*
  2657. * Free the termios and termios_locked structures because
  2658. * we don't want to get memory leaks when modular tty
  2659. * drivers are removed from the kernel.
  2660. */
  2661. for (i = 0; i < driver->num; i++) {
  2662. tp = driver->termios[i];
  2663. if (tp) {
  2664. driver->termios[i] = NULL;
  2665. kfree(tp);
  2666. }
  2667. if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
  2668. tty_unregister_device(driver, i);
  2669. }
  2670. p = driver->ttys;
  2671. proc_tty_unregister_driver(driver);
  2672. driver->ttys = NULL;
  2673. driver->termios = NULL;
  2674. kfree(p);
  2675. cdev_del(&driver->cdev);
  2676. }
  2677. kfree(driver);
  2678. }
  2679. void tty_driver_kref_put(struct tty_driver *driver)
  2680. {
  2681. kref_put(&driver->kref, destruct_tty_driver);
  2682. }
  2683. EXPORT_SYMBOL(tty_driver_kref_put);
  2684. void tty_set_operations(struct tty_driver *driver,
  2685. const struct tty_operations *op)
  2686. {
  2687. driver->ops = op;
  2688. };
  2689. EXPORT_SYMBOL(tty_set_operations);
  2690. void put_tty_driver(struct tty_driver *d)
  2691. {
  2692. tty_driver_kref_put(d);
  2693. }
  2694. EXPORT_SYMBOL(put_tty_driver);
  2695. /*
  2696. * Called by a tty driver to register itself.
  2697. */
  2698. int tty_register_driver(struct tty_driver *driver)
  2699. {
  2700. int error;
  2701. int i;
  2702. dev_t dev;
  2703. void **p = NULL;
  2704. struct device *d;
  2705. if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
  2706. p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
  2707. if (!p)
  2708. return -ENOMEM;
  2709. }
  2710. if (!driver->major) {
  2711. error = alloc_chrdev_region(&dev, driver->minor_start,
  2712. driver->num, driver->name);
  2713. if (!error) {
  2714. driver->major = MAJOR(dev);
  2715. driver->minor_start = MINOR(dev);
  2716. }
  2717. } else {
  2718. dev = MKDEV(driver->major, driver->minor_start);
  2719. error = register_chrdev_region(dev, driver->num, driver->name);
  2720. }
  2721. if (error < 0) {
  2722. kfree(p);
  2723. return error;
  2724. }
  2725. if (p) {
  2726. driver->ttys = (struct tty_struct **)p;
  2727. driver->termios = (struct ktermios **)(p + driver->num);
  2728. } else {
  2729. driver->ttys = NULL;
  2730. driver->termios = NULL;
  2731. }
  2732. cdev_init(&driver->cdev, &tty_fops);
  2733. driver->cdev.owner = driver->owner;
  2734. error = cdev_add(&driver->cdev, dev, driver->num);
  2735. if (error) {
  2736. unregister_chrdev_region(dev, driver->num);
  2737. driver->ttys = NULL;
  2738. driver->termios = NULL;
  2739. kfree(p);
  2740. return error;
  2741. }
  2742. mutex_lock(&tty_mutex);
  2743. list_add(&driver->tty_drivers, &tty_drivers);
  2744. mutex_unlock(&tty_mutex);
  2745. if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
  2746. for (i = 0; i < driver->num; i++) {
  2747. d = tty_register_device(driver, i, NULL);
  2748. if (IS_ERR(d)) {
  2749. error = PTR_ERR(d);
  2750. goto err;
  2751. }
  2752. }
  2753. }
  2754. proc_tty_register_driver(driver);
  2755. driver->flags |= TTY_DRIVER_INSTALLED;
  2756. return 0;
  2757. err:
  2758. for (i--; i >= 0; i--)
  2759. tty_unregister_device(driver, i);
  2760. mutex_lock(&tty_mutex);
  2761. list_del(&driver->tty_drivers);
  2762. mutex_unlock(&tty_mutex);
  2763. unregister_chrdev_region(dev, driver->num);
  2764. driver->ttys = NULL;
  2765. driver->termios = NULL;
  2766. kfree(p);
  2767. return error;
  2768. }
  2769. EXPORT_SYMBOL(tty_register_driver);
  2770. /*
  2771. * Called by a tty driver to unregister itself.
  2772. */
  2773. int tty_unregister_driver(struct tty_driver *driver)
  2774. {
  2775. #if 0
  2776. /* FIXME */
  2777. if (driver->refcount)
  2778. return -EBUSY;
  2779. #endif
  2780. unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
  2781. driver->num);
  2782. mutex_lock(&tty_mutex);
  2783. list_del(&driver->tty_drivers);
  2784. mutex_unlock(&tty_mutex);
  2785. return 0;
  2786. }
  2787. EXPORT_SYMBOL(tty_unregister_driver);
  2788. dev_t tty_devnum(struct tty_struct *tty)
  2789. {
  2790. return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
  2791. }
  2792. EXPORT_SYMBOL(tty_devnum);
  2793. void proc_clear_tty(struct task_struct *p)
  2794. {
  2795. unsigned long flags;
  2796. struct tty_struct *tty;
  2797. spin_lock_irqsave(&p->sighand->siglock, flags);
  2798. tty = p->signal->tty;
  2799. p->signal->tty = NULL;
  2800. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  2801. tty_kref_put(tty);
  2802. }
  2803. /* Called under the sighand lock */
  2804. static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
  2805. {
  2806. if (tty) {
  2807. unsigned long flags;
  2808. /* We should not have a session or pgrp to put here but.... */
  2809. spin_lock_irqsave(&tty->ctrl_lock, flags);
  2810. put_pid(tty->session);
  2811. put_pid(tty->pgrp);
  2812. tty->pgrp = get_pid(task_pgrp(tsk));
  2813. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  2814. tty->session = get_pid(task_session(tsk));
  2815. if (tsk->signal->tty) {
  2816. printk(KERN_DEBUG "tty not NULL!!\n");
  2817. tty_kref_put(tsk->signal->tty);
  2818. }
  2819. }
  2820. put_pid(tsk->signal->tty_old_pgrp);
  2821. tsk->signal->tty = tty_kref_get(tty);
  2822. tsk->signal->tty_old_pgrp = NULL;
  2823. }
  2824. static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
  2825. {
  2826. spin_lock_irq(&tsk->sighand->siglock);
  2827. __proc_set_tty(tsk, tty);
  2828. spin_unlock_irq(&tsk->sighand->siglock);
  2829. }
  2830. struct tty_struct *get_current_tty(void)
  2831. {
  2832. struct tty_struct *tty;
  2833. unsigned long flags;
  2834. spin_lock_irqsave(&current->sighand->siglock, flags);
  2835. tty = tty_kref_get(current->signal->tty);
  2836. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  2837. return tty;
  2838. }
  2839. EXPORT_SYMBOL_GPL(get_current_tty);
  2840. void tty_default_fops(struct file_operations *fops)
  2841. {
  2842. *fops = tty_fops;
  2843. }
  2844. /*
  2845. * Initialize the console device. This is called *early*, so
  2846. * we can't necessarily depend on lots of kernel help here.
  2847. * Just do some early initializations, and do the complex setup
  2848. * later.
  2849. */
  2850. void __init console_init(void)
  2851. {
  2852. initcall_t *call;
  2853. /* Setup the default TTY line discipline. */
  2854. tty_ldisc_begin();
  2855. /*
  2856. * set up the console device so that later boot sequences can
  2857. * inform about problems etc..
  2858. */
  2859. call = __con_initcall_start;
  2860. while (call < __con_initcall_end) {
  2861. (*call)();
  2862. call++;
  2863. }
  2864. }
  2865. static char *tty_devnode(struct device *dev, mode_t *mode)
  2866. {
  2867. if (!mode)
  2868. return NULL;
  2869. if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
  2870. dev->devt == MKDEV(TTYAUX_MAJOR, 2))
  2871. *mode = 0666;
  2872. return NULL;
  2873. }
  2874. static int __init tty_class_init(void)
  2875. {
  2876. tty_class = class_create(THIS_MODULE, "tty");
  2877. if (IS_ERR(tty_class))
  2878. return PTR_ERR(tty_class);
  2879. tty_class->devnode = tty_devnode;
  2880. return 0;
  2881. }
  2882. postcore_initcall(tty_class_init);
  2883. /* 3/2004 jmc: why do these devices exist? */
  2884. static struct cdev tty_cdev, console_cdev;
  2885. /*
  2886. * Ok, now we can initialize the rest of the tty devices and can count
  2887. * on memory allocations, interrupts etc..
  2888. */
  2889. int __init tty_init(void)
  2890. {
  2891. cdev_init(&tty_cdev, &tty_fops);
  2892. if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
  2893. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
  2894. panic("Couldn't register /dev/tty driver\n");
  2895. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
  2896. "tty");
  2897. cdev_init(&console_cdev, &console_fops);
  2898. if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
  2899. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
  2900. panic("Couldn't register /dev/console driver\n");
  2901. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
  2902. "console");
  2903. #ifdef CONFIG_VT
  2904. vty_init(&console_fops);
  2905. #endif
  2906. return 0;
  2907. }