tty_io.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266
  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 init_dev and 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() -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
  65. */
  66. #include <linux/config.h>
  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/console.h>
  80. #include <linux/timer.h>
  81. #include <linux/ctype.h>
  82. #include <linux/kd.h>
  83. #include <linux/mm.h>
  84. #include <linux/string.h>
  85. #include <linux/slab.h>
  86. #include <linux/poll.h>
  87. #include <linux/proc_fs.h>
  88. #include <linux/init.h>
  89. #include <linux/module.h>
  90. #include <linux/smp_lock.h>
  91. #include <linux/device.h>
  92. #include <linux/idr.h>
  93. #include <linux/wait.h>
  94. #include <linux/bitops.h>
  95. #include <linux/delay.h>
  96. #include <asm/uaccess.h>
  97. #include <asm/system.h>
  98. #include <linux/kbd_kern.h>
  99. #include <linux/vt_kern.h>
  100. #include <linux/selection.h>
  101. #include <linux/devfs_fs_kernel.h>
  102. #include <linux/kmod.h>
  103. #undef TTY_DEBUG_HANGUP
  104. #define TTY_PARANOIA_CHECK 1
  105. #define CHECK_TTY_COUNT 1
  106. struct termios tty_std_termios = { /* for the benefit of tty drivers */
  107. .c_iflag = ICRNL | IXON,
  108. .c_oflag = OPOST | ONLCR,
  109. .c_cflag = B38400 | CS8 | CREAD | HUPCL,
  110. .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
  111. ECHOCTL | ECHOKE | IEXTEN,
  112. .c_cc = INIT_C_CC
  113. };
  114. EXPORT_SYMBOL(tty_std_termios);
  115. /* This list gets poked at by procfs and various bits of boot up code. This
  116. could do with some rationalisation such as pulling the tty proc function
  117. into this file */
  118. LIST_HEAD(tty_drivers); /* linked list of tty drivers */
  119. /* Semaphore to protect creating and releasing a tty. This is shared with
  120. vt.c for deeply disgusting hack reasons */
  121. DEFINE_MUTEX(tty_mutex);
  122. #ifdef CONFIG_UNIX98_PTYS
  123. extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
  124. extern int pty_limit; /* Config limit on Unix98 ptys */
  125. static DEFINE_IDR(allocated_ptys);
  126. static DECLARE_MUTEX(allocated_ptys_lock);
  127. static int ptmx_open(struct inode *, struct file *);
  128. #endif
  129. extern void disable_early_printk(void);
  130. static void initialize_tty_struct(struct tty_struct *tty);
  131. static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
  132. static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
  133. ssize_t redirected_tty_write(struct file *, const char __user *, size_t, loff_t *);
  134. static unsigned int tty_poll(struct file *, poll_table *);
  135. static int tty_open(struct inode *, struct file *);
  136. static int tty_release(struct inode *, struct file *);
  137. int tty_ioctl(struct inode * inode, struct file * file,
  138. unsigned int cmd, unsigned long arg);
  139. static int tty_fasync(int fd, struct file * filp, int on);
  140. static void release_mem(struct tty_struct *tty, int idx);
  141. static struct tty_struct *alloc_tty_struct(void)
  142. {
  143. struct tty_struct *tty;
  144. tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
  145. if (tty)
  146. memset(tty, 0, sizeof(struct tty_struct));
  147. return tty;
  148. }
  149. static void tty_buffer_free_all(struct tty_struct *);
  150. static inline void free_tty_struct(struct tty_struct *tty)
  151. {
  152. kfree(tty->write_buf);
  153. tty_buffer_free_all(tty);
  154. kfree(tty);
  155. }
  156. #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
  157. char *tty_name(struct tty_struct *tty, char *buf)
  158. {
  159. if (!tty) /* Hmm. NULL pointer. That's fun. */
  160. strcpy(buf, "NULL tty");
  161. else
  162. strcpy(buf, tty->name);
  163. return buf;
  164. }
  165. EXPORT_SYMBOL(tty_name);
  166. int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
  167. const char *routine)
  168. {
  169. #ifdef TTY_PARANOIA_CHECK
  170. if (!tty) {
  171. printk(KERN_WARNING
  172. "null TTY for (%d:%d) in %s\n",
  173. imajor(inode), iminor(inode), routine);
  174. return 1;
  175. }
  176. if (tty->magic != TTY_MAGIC) {
  177. printk(KERN_WARNING
  178. "bad magic number for tty struct (%d:%d) in %s\n",
  179. imajor(inode), iminor(inode), routine);
  180. return 1;
  181. }
  182. #endif
  183. return 0;
  184. }
  185. static int check_tty_count(struct tty_struct *tty, const char *routine)
  186. {
  187. #ifdef CHECK_TTY_COUNT
  188. struct list_head *p;
  189. int count = 0;
  190. file_list_lock();
  191. list_for_each(p, &tty->tty_files) {
  192. count++;
  193. }
  194. file_list_unlock();
  195. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  196. tty->driver->subtype == PTY_TYPE_SLAVE &&
  197. tty->link && tty->link->count)
  198. count++;
  199. if (tty->count != count) {
  200. printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
  201. "!= #fd's(%d) in %s\n",
  202. tty->name, tty->count, count, routine);
  203. return count;
  204. }
  205. #endif
  206. return 0;
  207. }
  208. /*
  209. * Tty buffer allocation management
  210. */
  211. static void tty_buffer_free_all(struct tty_struct *tty)
  212. {
  213. struct tty_buffer *thead;
  214. while((thead = tty->buf.head) != NULL) {
  215. tty->buf.head = thead->next;
  216. kfree(thead);
  217. }
  218. while((thead = tty->buf.free) != NULL) {
  219. tty->buf.free = thead->next;
  220. kfree(thead);
  221. }
  222. tty->buf.tail = NULL;
  223. }
  224. static void tty_buffer_init(struct tty_struct *tty)
  225. {
  226. spin_lock_init(&tty->buf.lock);
  227. tty->buf.head = NULL;
  228. tty->buf.tail = NULL;
  229. tty->buf.free = NULL;
  230. }
  231. static struct tty_buffer *tty_buffer_alloc(size_t size)
  232. {
  233. struct tty_buffer *p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
  234. if(p == NULL)
  235. return NULL;
  236. p->used = 0;
  237. p->size = size;
  238. p->next = NULL;
  239. p->commit = 0;
  240. p->read = 0;
  241. p->char_buf_ptr = (char *)(p->data);
  242. p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
  243. /* printk("Flip create %p\n", p); */
  244. return p;
  245. }
  246. /* Must be called with the tty_read lock held. This needs to acquire strategy
  247. code to decide if we should kfree or relink a given expired buffer */
  248. static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
  249. {
  250. /* Dumb strategy for now - should keep some stats */
  251. /* printk("Flip dispose %p\n", b); */
  252. if(b->size >= 512)
  253. kfree(b);
  254. else {
  255. b->next = tty->buf.free;
  256. tty->buf.free = b;
  257. }
  258. }
  259. static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
  260. {
  261. struct tty_buffer **tbh = &tty->buf.free;
  262. while((*tbh) != NULL) {
  263. struct tty_buffer *t = *tbh;
  264. if(t->size >= size) {
  265. *tbh = t->next;
  266. t->next = NULL;
  267. t->used = 0;
  268. t->commit = 0;
  269. t->read = 0;
  270. /* DEBUG ONLY */
  271. /* memset(t->data, '*', size); */
  272. /* printk("Flip recycle %p\n", t); */
  273. return t;
  274. }
  275. tbh = &((*tbh)->next);
  276. }
  277. /* Round the buffer size out */
  278. size = (size + 0xFF) & ~ 0xFF;
  279. return tty_buffer_alloc(size);
  280. /* Should possibly check if this fails for the largest buffer we
  281. have queued and recycle that ? */
  282. }
  283. int tty_buffer_request_room(struct tty_struct *tty, size_t size)
  284. {
  285. struct tty_buffer *b, *n;
  286. int left;
  287. unsigned long flags;
  288. spin_lock_irqsave(&tty->buf.lock, flags);
  289. /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
  290. remove this conditional if its worth it. This would be invisible
  291. to the callers */
  292. if ((b = tty->buf.tail) != NULL)
  293. left = b->size - b->used;
  294. else
  295. left = 0;
  296. if (left < size) {
  297. /* This is the slow path - looking for new buffers to use */
  298. if ((n = tty_buffer_find(tty, size)) != NULL) {
  299. if (b != NULL) {
  300. b->next = n;
  301. b->commit = b->used;
  302. } else
  303. tty->buf.head = n;
  304. tty->buf.tail = n;
  305. } else
  306. size = left;
  307. }
  308. spin_unlock_irqrestore(&tty->buf.lock, flags);
  309. return size;
  310. }
  311. EXPORT_SYMBOL_GPL(tty_buffer_request_room);
  312. int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars,
  313. size_t size)
  314. {
  315. int copied = 0;
  316. do {
  317. int space = tty_buffer_request_room(tty, size - copied);
  318. struct tty_buffer *tb = tty->buf.tail;
  319. /* If there is no space then tb may be NULL */
  320. if(unlikely(space == 0))
  321. break;
  322. memcpy(tb->char_buf_ptr + tb->used, chars, space);
  323. memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
  324. tb->used += space;
  325. copied += space;
  326. chars += space;
  327. }
  328. /* There is a small chance that we need to split the data over
  329. several buffers. If this is the case we must loop */
  330. while (unlikely(size > copied));
  331. return copied;
  332. }
  333. EXPORT_SYMBOL(tty_insert_flip_string);
  334. int tty_insert_flip_string_flags(struct tty_struct *tty,
  335. const unsigned char *chars, const char *flags, size_t size)
  336. {
  337. int copied = 0;
  338. do {
  339. int space = tty_buffer_request_room(tty, size - copied);
  340. struct tty_buffer *tb = tty->buf.tail;
  341. /* If there is no space then tb may be NULL */
  342. if(unlikely(space == 0))
  343. break;
  344. memcpy(tb->char_buf_ptr + tb->used, chars, space);
  345. memcpy(tb->flag_buf_ptr + tb->used, flags, space);
  346. tb->used += space;
  347. copied += space;
  348. chars += space;
  349. flags += space;
  350. }
  351. /* There is a small chance that we need to split the data over
  352. several buffers. If this is the case we must loop */
  353. while (unlikely(size > copied));
  354. return copied;
  355. }
  356. EXPORT_SYMBOL(tty_insert_flip_string_flags);
  357. void tty_schedule_flip(struct tty_struct *tty)
  358. {
  359. unsigned long flags;
  360. spin_lock_irqsave(&tty->buf.lock, flags);
  361. if (tty->buf.tail != NULL)
  362. tty->buf.tail->commit = tty->buf.tail->used;
  363. spin_unlock_irqrestore(&tty->buf.lock, flags);
  364. schedule_delayed_work(&tty->buf.work, 1);
  365. }
  366. EXPORT_SYMBOL(tty_schedule_flip);
  367. /*
  368. * Prepare a block of space in the buffer for data. Returns the length
  369. * available and buffer pointer to the space which is now allocated and
  370. * accounted for as ready for normal characters. This is used for drivers
  371. * that need their own block copy routines into the buffer. There is no
  372. * guarantee the buffer is a DMA target!
  373. */
  374. int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars, size_t size)
  375. {
  376. int space = tty_buffer_request_room(tty, size);
  377. if (likely(space)) {
  378. struct tty_buffer *tb = tty->buf.tail;
  379. *chars = tb->char_buf_ptr + tb->used;
  380. memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
  381. tb->used += space;
  382. }
  383. return space;
  384. }
  385. EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
  386. /*
  387. * Prepare a block of space in the buffer for data. Returns the length
  388. * available and buffer pointer to the space which is now allocated and
  389. * accounted for as ready for characters. This is used for drivers
  390. * that need their own block copy routines into the buffer. There is no
  391. * guarantee the buffer is a DMA target!
  392. */
  393. int tty_prepare_flip_string_flags(struct tty_struct *tty, unsigned char **chars, char **flags, size_t size)
  394. {
  395. int space = tty_buffer_request_room(tty, size);
  396. if (likely(space)) {
  397. struct tty_buffer *tb = tty->buf.tail;
  398. *chars = tb->char_buf_ptr + tb->used;
  399. *flags = tb->flag_buf_ptr + tb->used;
  400. tb->used += space;
  401. }
  402. return space;
  403. }
  404. EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
  405. /*
  406. * This is probably overkill for real world processors but
  407. * they are not on hot paths so a little discipline won't do
  408. * any harm.
  409. */
  410. static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
  411. {
  412. down(&tty->termios_sem);
  413. tty->termios->c_line = num;
  414. up(&tty->termios_sem);
  415. }
  416. /*
  417. * This guards the refcounted line discipline lists. The lock
  418. * must be taken with irqs off because there are hangup path
  419. * callers who will do ldisc lookups and cannot sleep.
  420. */
  421. static DEFINE_SPINLOCK(tty_ldisc_lock);
  422. static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
  423. static struct tty_ldisc tty_ldiscs[NR_LDISCS]; /* line disc dispatch table */
  424. int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
  425. {
  426. unsigned long flags;
  427. int ret = 0;
  428. if (disc < N_TTY || disc >= NR_LDISCS)
  429. return -EINVAL;
  430. spin_lock_irqsave(&tty_ldisc_lock, flags);
  431. tty_ldiscs[disc] = *new_ldisc;
  432. tty_ldiscs[disc].num = disc;
  433. tty_ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
  434. tty_ldiscs[disc].refcount = 0;
  435. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  436. return ret;
  437. }
  438. EXPORT_SYMBOL(tty_register_ldisc);
  439. int tty_unregister_ldisc(int disc)
  440. {
  441. unsigned long flags;
  442. int ret = 0;
  443. if (disc < N_TTY || disc >= NR_LDISCS)
  444. return -EINVAL;
  445. spin_lock_irqsave(&tty_ldisc_lock, flags);
  446. if (tty_ldiscs[disc].refcount)
  447. ret = -EBUSY;
  448. else
  449. tty_ldiscs[disc].flags &= ~LDISC_FLAG_DEFINED;
  450. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  451. return ret;
  452. }
  453. EXPORT_SYMBOL(tty_unregister_ldisc);
  454. struct tty_ldisc *tty_ldisc_get(int disc)
  455. {
  456. unsigned long flags;
  457. struct tty_ldisc *ld;
  458. if (disc < N_TTY || disc >= NR_LDISCS)
  459. return NULL;
  460. spin_lock_irqsave(&tty_ldisc_lock, flags);
  461. ld = &tty_ldiscs[disc];
  462. /* Check the entry is defined */
  463. if(ld->flags & LDISC_FLAG_DEFINED)
  464. {
  465. /* If the module is being unloaded we can't use it */
  466. if (!try_module_get(ld->owner))
  467. ld = NULL;
  468. else /* lock it */
  469. ld->refcount++;
  470. }
  471. else
  472. ld = NULL;
  473. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  474. return ld;
  475. }
  476. EXPORT_SYMBOL_GPL(tty_ldisc_get);
  477. void tty_ldisc_put(int disc)
  478. {
  479. struct tty_ldisc *ld;
  480. unsigned long flags;
  481. BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
  482. spin_lock_irqsave(&tty_ldisc_lock, flags);
  483. ld = &tty_ldiscs[disc];
  484. BUG_ON(ld->refcount == 0);
  485. ld->refcount--;
  486. module_put(ld->owner);
  487. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  488. }
  489. EXPORT_SYMBOL_GPL(tty_ldisc_put);
  490. static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
  491. {
  492. tty->ldisc = *ld;
  493. tty->ldisc.refcount = 0;
  494. }
  495. /**
  496. * tty_ldisc_try - internal helper
  497. * @tty: the tty
  498. *
  499. * Make a single attempt to grab and bump the refcount on
  500. * the tty ldisc. Return 0 on failure or 1 on success. This is
  501. * used to implement both the waiting and non waiting versions
  502. * of tty_ldisc_ref
  503. */
  504. static int tty_ldisc_try(struct tty_struct *tty)
  505. {
  506. unsigned long flags;
  507. struct tty_ldisc *ld;
  508. int ret = 0;
  509. spin_lock_irqsave(&tty_ldisc_lock, flags);
  510. ld = &tty->ldisc;
  511. if(test_bit(TTY_LDISC, &tty->flags))
  512. {
  513. ld->refcount++;
  514. ret = 1;
  515. }
  516. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  517. return ret;
  518. }
  519. /**
  520. * tty_ldisc_ref_wait - wait for the tty ldisc
  521. * @tty: tty device
  522. *
  523. * Dereference the line discipline for the terminal and take a
  524. * reference to it. If the line discipline is in flux then
  525. * wait patiently until it changes.
  526. *
  527. * Note: Must not be called from an IRQ/timer context. The caller
  528. * must also be careful not to hold other locks that will deadlock
  529. * against a discipline change, such as an existing ldisc reference
  530. * (which we check for)
  531. */
  532. struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
  533. {
  534. /* wait_event is a macro */
  535. wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
  536. if(tty->ldisc.refcount == 0)
  537. printk(KERN_ERR "tty_ldisc_ref_wait\n");
  538. return &tty->ldisc;
  539. }
  540. EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
  541. /**
  542. * tty_ldisc_ref - get the tty ldisc
  543. * @tty: tty device
  544. *
  545. * Dereference the line discipline for the terminal and take a
  546. * reference to it. If the line discipline is in flux then
  547. * return NULL. Can be called from IRQ and timer functions.
  548. */
  549. struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
  550. {
  551. if(tty_ldisc_try(tty))
  552. return &tty->ldisc;
  553. return NULL;
  554. }
  555. EXPORT_SYMBOL_GPL(tty_ldisc_ref);
  556. /**
  557. * tty_ldisc_deref - free a tty ldisc reference
  558. * @ld: reference to free up
  559. *
  560. * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
  561. * be called in IRQ context.
  562. */
  563. void tty_ldisc_deref(struct tty_ldisc *ld)
  564. {
  565. unsigned long flags;
  566. BUG_ON(ld == NULL);
  567. spin_lock_irqsave(&tty_ldisc_lock, flags);
  568. if(ld->refcount == 0)
  569. printk(KERN_ERR "tty_ldisc_deref: no references.\n");
  570. else
  571. ld->refcount--;
  572. if(ld->refcount == 0)
  573. wake_up(&tty_ldisc_wait);
  574. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  575. }
  576. EXPORT_SYMBOL_GPL(tty_ldisc_deref);
  577. /**
  578. * tty_ldisc_enable - allow ldisc use
  579. * @tty: terminal to activate ldisc on
  580. *
  581. * Set the TTY_LDISC flag when the line discipline can be called
  582. * again. Do neccessary wakeups for existing sleepers.
  583. *
  584. * Note: nobody should set this bit except via this function. Clearing
  585. * directly is allowed.
  586. */
  587. static void tty_ldisc_enable(struct tty_struct *tty)
  588. {
  589. set_bit(TTY_LDISC, &tty->flags);
  590. wake_up(&tty_ldisc_wait);
  591. }
  592. /**
  593. * tty_set_ldisc - set line discipline
  594. * @tty: the terminal to set
  595. * @ldisc: the line discipline
  596. *
  597. * Set the discipline of a tty line. Must be called from a process
  598. * context.
  599. */
  600. static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
  601. {
  602. int retval = 0;
  603. struct tty_ldisc o_ldisc;
  604. char buf[64];
  605. int work;
  606. unsigned long flags;
  607. struct tty_ldisc *ld;
  608. struct tty_struct *o_tty;
  609. if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
  610. return -EINVAL;
  611. restart:
  612. ld = tty_ldisc_get(ldisc);
  613. /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
  614. /* Cyrus Durgin <cider@speakeasy.org> */
  615. if (ld == NULL) {
  616. request_module("tty-ldisc-%d", ldisc);
  617. ld = tty_ldisc_get(ldisc);
  618. }
  619. if (ld == NULL)
  620. return -EINVAL;
  621. /*
  622. * No more input please, we are switching. The new ldisc
  623. * will update this value in the ldisc open function
  624. */
  625. tty->receive_room = 0;
  626. /*
  627. * Problem: What do we do if this blocks ?
  628. */
  629. tty_wait_until_sent(tty, 0);
  630. if (tty->ldisc.num == ldisc) {
  631. tty_ldisc_put(ldisc);
  632. return 0;
  633. }
  634. o_ldisc = tty->ldisc;
  635. o_tty = tty->link;
  636. /*
  637. * Make sure we don't change while someone holds a
  638. * reference to the line discipline. The TTY_LDISC bit
  639. * prevents anyone taking a reference once it is clear.
  640. * We need the lock to avoid racing reference takers.
  641. */
  642. spin_lock_irqsave(&tty_ldisc_lock, flags);
  643. if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
  644. if(tty->ldisc.refcount) {
  645. /* Free the new ldisc we grabbed. Must drop the lock
  646. first. */
  647. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  648. tty_ldisc_put(ldisc);
  649. /*
  650. * There are several reasons we may be busy, including
  651. * random momentary I/O traffic. We must therefore
  652. * retry. We could distinguish between blocking ops
  653. * and retries if we made tty_ldisc_wait() smarter. That
  654. * is up for discussion.
  655. */
  656. if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
  657. return -ERESTARTSYS;
  658. goto restart;
  659. }
  660. if(o_tty && o_tty->ldisc.refcount) {
  661. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  662. tty_ldisc_put(ldisc);
  663. if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
  664. return -ERESTARTSYS;
  665. goto restart;
  666. }
  667. }
  668. /* if the TTY_LDISC bit is set, then we are racing against another ldisc change */
  669. if (!test_bit(TTY_LDISC, &tty->flags)) {
  670. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  671. tty_ldisc_put(ldisc);
  672. ld = tty_ldisc_ref_wait(tty);
  673. tty_ldisc_deref(ld);
  674. goto restart;
  675. }
  676. clear_bit(TTY_LDISC, &tty->flags);
  677. if (o_tty)
  678. clear_bit(TTY_LDISC, &o_tty->flags);
  679. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  680. /*
  681. * From this point on we know nobody has an ldisc
  682. * usage reference, nor can they obtain one until
  683. * we say so later on.
  684. */
  685. work = cancel_delayed_work(&tty->buf.work);
  686. /*
  687. * Wait for ->hangup_work and ->buf.work handlers to terminate
  688. */
  689. flush_scheduled_work();
  690. /* Shutdown the current discipline. */
  691. if (tty->ldisc.close)
  692. (tty->ldisc.close)(tty);
  693. /* Now set up the new line discipline. */
  694. tty_ldisc_assign(tty, ld);
  695. tty_set_termios_ldisc(tty, ldisc);
  696. if (tty->ldisc.open)
  697. retval = (tty->ldisc.open)(tty);
  698. if (retval < 0) {
  699. tty_ldisc_put(ldisc);
  700. /* There is an outstanding reference here so this is safe */
  701. tty_ldisc_assign(tty, tty_ldisc_get(o_ldisc.num));
  702. tty_set_termios_ldisc(tty, tty->ldisc.num);
  703. if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
  704. tty_ldisc_put(o_ldisc.num);
  705. /* This driver is always present */
  706. tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
  707. tty_set_termios_ldisc(tty, N_TTY);
  708. if (tty->ldisc.open) {
  709. int r = tty->ldisc.open(tty);
  710. if (r < 0)
  711. panic("Couldn't open N_TTY ldisc for "
  712. "%s --- error %d.",
  713. tty_name(tty, buf), r);
  714. }
  715. }
  716. }
  717. /* At this point we hold a reference to the new ldisc and a
  718. a reference to the old ldisc. If we ended up flipping back
  719. to the existing ldisc we have two references to it */
  720. if (tty->ldisc.num != o_ldisc.num && tty->driver->set_ldisc)
  721. tty->driver->set_ldisc(tty);
  722. tty_ldisc_put(o_ldisc.num);
  723. /*
  724. * Allow ldisc referencing to occur as soon as the driver
  725. * ldisc callback completes.
  726. */
  727. tty_ldisc_enable(tty);
  728. if (o_tty)
  729. tty_ldisc_enable(o_tty);
  730. /* Restart it in case no characters kick it off. Safe if
  731. already running */
  732. if (work)
  733. schedule_delayed_work(&tty->buf.work, 1);
  734. return retval;
  735. }
  736. /*
  737. * This routine returns a tty driver structure, given a device number
  738. */
  739. static struct tty_driver *get_tty_driver(dev_t device, int *index)
  740. {
  741. struct tty_driver *p;
  742. list_for_each_entry(p, &tty_drivers, tty_drivers) {
  743. dev_t base = MKDEV(p->major, p->minor_start);
  744. if (device < base || device >= base + p->num)
  745. continue;
  746. *index = device - base;
  747. return p;
  748. }
  749. return NULL;
  750. }
  751. /*
  752. * If we try to write to, or set the state of, a terminal and we're
  753. * not in the foreground, send a SIGTTOU. If the signal is blocked or
  754. * ignored, go ahead and perform the operation. (POSIX 7.2)
  755. */
  756. int tty_check_change(struct tty_struct * tty)
  757. {
  758. if (current->signal->tty != tty)
  759. return 0;
  760. if (tty->pgrp <= 0) {
  761. printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n");
  762. return 0;
  763. }
  764. if (process_group(current) == tty->pgrp)
  765. return 0;
  766. if (is_ignored(SIGTTOU))
  767. return 0;
  768. if (is_orphaned_pgrp(process_group(current)))
  769. return -EIO;
  770. (void) kill_pg(process_group(current), SIGTTOU, 1);
  771. return -ERESTARTSYS;
  772. }
  773. EXPORT_SYMBOL(tty_check_change);
  774. static ssize_t hung_up_tty_read(struct file * file, char __user * buf,
  775. size_t count, loff_t *ppos)
  776. {
  777. return 0;
  778. }
  779. static ssize_t hung_up_tty_write(struct file * file, const char __user * buf,
  780. size_t count, loff_t *ppos)
  781. {
  782. return -EIO;
  783. }
  784. /* No kernel lock held - none needed ;) */
  785. static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
  786. {
  787. return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
  788. }
  789. static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
  790. unsigned int cmd, unsigned long arg)
  791. {
  792. return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
  793. }
  794. static struct file_operations tty_fops = {
  795. .llseek = no_llseek,
  796. .read = tty_read,
  797. .write = tty_write,
  798. .poll = tty_poll,
  799. .ioctl = tty_ioctl,
  800. .open = tty_open,
  801. .release = tty_release,
  802. .fasync = tty_fasync,
  803. };
  804. #ifdef CONFIG_UNIX98_PTYS
  805. static struct file_operations ptmx_fops = {
  806. .llseek = no_llseek,
  807. .read = tty_read,
  808. .write = tty_write,
  809. .poll = tty_poll,
  810. .ioctl = tty_ioctl,
  811. .open = ptmx_open,
  812. .release = tty_release,
  813. .fasync = tty_fasync,
  814. };
  815. #endif
  816. static struct file_operations console_fops = {
  817. .llseek = no_llseek,
  818. .read = tty_read,
  819. .write = redirected_tty_write,
  820. .poll = tty_poll,
  821. .ioctl = tty_ioctl,
  822. .open = tty_open,
  823. .release = tty_release,
  824. .fasync = tty_fasync,
  825. };
  826. static struct file_operations hung_up_tty_fops = {
  827. .llseek = no_llseek,
  828. .read = hung_up_tty_read,
  829. .write = hung_up_tty_write,
  830. .poll = hung_up_tty_poll,
  831. .ioctl = hung_up_tty_ioctl,
  832. .release = tty_release,
  833. };
  834. static DEFINE_SPINLOCK(redirect_lock);
  835. static struct file *redirect;
  836. /**
  837. * tty_wakeup - request more data
  838. * @tty: terminal
  839. *
  840. * Internal and external helper for wakeups of tty. This function
  841. * informs the line discipline if present that the driver is ready
  842. * to receive more output data.
  843. */
  844. void tty_wakeup(struct tty_struct *tty)
  845. {
  846. struct tty_ldisc *ld;
  847. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
  848. ld = tty_ldisc_ref(tty);
  849. if(ld) {
  850. if(ld->write_wakeup)
  851. ld->write_wakeup(tty);
  852. tty_ldisc_deref(ld);
  853. }
  854. }
  855. wake_up_interruptible(&tty->write_wait);
  856. }
  857. EXPORT_SYMBOL_GPL(tty_wakeup);
  858. /**
  859. * tty_ldisc_flush - flush line discipline queue
  860. * @tty: tty
  861. *
  862. * Flush the line discipline queue (if any) for this tty. If there
  863. * is no line discipline active this is a no-op.
  864. */
  865. void tty_ldisc_flush(struct tty_struct *tty)
  866. {
  867. struct tty_ldisc *ld = tty_ldisc_ref(tty);
  868. if(ld) {
  869. if(ld->flush_buffer)
  870. ld->flush_buffer(tty);
  871. tty_ldisc_deref(ld);
  872. }
  873. }
  874. EXPORT_SYMBOL_GPL(tty_ldisc_flush);
  875. /*
  876. * This can be called by the "eventd" kernel thread. That is process synchronous,
  877. * but doesn't hold any locks, so we need to make sure we have the appropriate
  878. * locks for what we're doing..
  879. */
  880. static void do_tty_hangup(void *data)
  881. {
  882. struct tty_struct *tty = (struct tty_struct *) data;
  883. struct file * cons_filp = NULL;
  884. struct file *filp, *f = NULL;
  885. struct task_struct *p;
  886. struct tty_ldisc *ld;
  887. int closecount = 0, n;
  888. if (!tty)
  889. return;
  890. /* inuse_filps is protected by the single kernel lock */
  891. lock_kernel();
  892. spin_lock(&redirect_lock);
  893. if (redirect && redirect->private_data == tty) {
  894. f = redirect;
  895. redirect = NULL;
  896. }
  897. spin_unlock(&redirect_lock);
  898. check_tty_count(tty, "do_tty_hangup");
  899. file_list_lock();
  900. /* This breaks for file handles being sent over AF_UNIX sockets ? */
  901. list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
  902. if (filp->f_op->write == redirected_tty_write)
  903. cons_filp = filp;
  904. if (filp->f_op->write != tty_write)
  905. continue;
  906. closecount++;
  907. tty_fasync(-1, filp, 0); /* can't block */
  908. filp->f_op = &hung_up_tty_fops;
  909. }
  910. file_list_unlock();
  911. /* FIXME! What are the locking issues here? This may me overdoing things..
  912. * this question is especially important now that we've removed the irqlock. */
  913. ld = tty_ldisc_ref(tty);
  914. if(ld != NULL) /* We may have no line discipline at this point */
  915. {
  916. if (ld->flush_buffer)
  917. ld->flush_buffer(tty);
  918. if (tty->driver->flush_buffer)
  919. tty->driver->flush_buffer(tty);
  920. if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
  921. ld->write_wakeup)
  922. ld->write_wakeup(tty);
  923. if (ld->hangup)
  924. ld->hangup(tty);
  925. }
  926. /* FIXME: Once we trust the LDISC code better we can wait here for
  927. ldisc completion and fix the driver call race */
  928. wake_up_interruptible(&tty->write_wait);
  929. wake_up_interruptible(&tty->read_wait);
  930. /*
  931. * Shutdown the current line discipline, and reset it to
  932. * N_TTY.
  933. */
  934. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
  935. {
  936. down(&tty->termios_sem);
  937. *tty->termios = tty->driver->init_termios;
  938. up(&tty->termios_sem);
  939. }
  940. /* Defer ldisc switch */
  941. /* tty_deferred_ldisc_switch(N_TTY);
  942. This should get done automatically when the port closes and
  943. tty_release is called */
  944. read_lock(&tasklist_lock);
  945. if (tty->session > 0) {
  946. do_each_task_pid(tty->session, PIDTYPE_SID, p) {
  947. if (p->signal->tty == tty)
  948. p->signal->tty = NULL;
  949. if (!p->signal->leader)
  950. continue;
  951. group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
  952. group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
  953. if (tty->pgrp > 0)
  954. p->signal->tty_old_pgrp = tty->pgrp;
  955. } while_each_task_pid(tty->session, PIDTYPE_SID, p);
  956. }
  957. read_unlock(&tasklist_lock);
  958. tty->flags = 0;
  959. tty->session = 0;
  960. tty->pgrp = -1;
  961. tty->ctrl_status = 0;
  962. /*
  963. * If one of the devices matches a console pointer, we
  964. * cannot just call hangup() because that will cause
  965. * tty->count and state->count to go out of sync.
  966. * So we just call close() the right number of times.
  967. */
  968. if (cons_filp) {
  969. if (tty->driver->close)
  970. for (n = 0; n < closecount; n++)
  971. tty->driver->close(tty, cons_filp);
  972. } else if (tty->driver->hangup)
  973. (tty->driver->hangup)(tty);
  974. /* We don't want to have driver/ldisc interactions beyond
  975. the ones we did here. The driver layer expects no
  976. calls after ->hangup() from the ldisc side. However we
  977. can't yet guarantee all that */
  978. set_bit(TTY_HUPPED, &tty->flags);
  979. if (ld) {
  980. tty_ldisc_enable(tty);
  981. tty_ldisc_deref(ld);
  982. }
  983. unlock_kernel();
  984. if (f)
  985. fput(f);
  986. }
  987. void tty_hangup(struct tty_struct * tty)
  988. {
  989. #ifdef TTY_DEBUG_HANGUP
  990. char buf[64];
  991. printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
  992. #endif
  993. schedule_work(&tty->hangup_work);
  994. }
  995. EXPORT_SYMBOL(tty_hangup);
  996. void tty_vhangup(struct tty_struct * tty)
  997. {
  998. #ifdef TTY_DEBUG_HANGUP
  999. char buf[64];
  1000. printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
  1001. #endif
  1002. do_tty_hangup((void *) tty);
  1003. }
  1004. EXPORT_SYMBOL(tty_vhangup);
  1005. int tty_hung_up_p(struct file * filp)
  1006. {
  1007. return (filp->f_op == &hung_up_tty_fops);
  1008. }
  1009. EXPORT_SYMBOL(tty_hung_up_p);
  1010. /*
  1011. * This function is typically called only by the session leader, when
  1012. * it wants to disassociate itself from its controlling tty.
  1013. *
  1014. * It performs the following functions:
  1015. * (1) Sends a SIGHUP and SIGCONT to the foreground process group
  1016. * (2) Clears the tty from being controlling the session
  1017. * (3) Clears the controlling tty for all processes in the
  1018. * session group.
  1019. *
  1020. * The argument on_exit is set to 1 if called when a process is
  1021. * exiting; it is 0 if called by the ioctl TIOCNOTTY.
  1022. */
  1023. void disassociate_ctty(int on_exit)
  1024. {
  1025. struct tty_struct *tty;
  1026. struct task_struct *p;
  1027. int tty_pgrp = -1;
  1028. lock_kernel();
  1029. mutex_lock(&tty_mutex);
  1030. tty = current->signal->tty;
  1031. if (tty) {
  1032. tty_pgrp = tty->pgrp;
  1033. mutex_unlock(&tty_mutex);
  1034. if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
  1035. tty_vhangup(tty);
  1036. } else {
  1037. if (current->signal->tty_old_pgrp) {
  1038. kill_pg(current->signal->tty_old_pgrp, SIGHUP, on_exit);
  1039. kill_pg(current->signal->tty_old_pgrp, SIGCONT, on_exit);
  1040. }
  1041. mutex_unlock(&tty_mutex);
  1042. unlock_kernel();
  1043. return;
  1044. }
  1045. if (tty_pgrp > 0) {
  1046. kill_pg(tty_pgrp, SIGHUP, on_exit);
  1047. if (!on_exit)
  1048. kill_pg(tty_pgrp, SIGCONT, on_exit);
  1049. }
  1050. /* Must lock changes to tty_old_pgrp */
  1051. mutex_lock(&tty_mutex);
  1052. current->signal->tty_old_pgrp = 0;
  1053. tty->session = 0;
  1054. tty->pgrp = -1;
  1055. /* Now clear signal->tty under the lock */
  1056. read_lock(&tasklist_lock);
  1057. do_each_task_pid(current->signal->session, PIDTYPE_SID, p) {
  1058. p->signal->tty = NULL;
  1059. } while_each_task_pid(current->signal->session, PIDTYPE_SID, p);
  1060. read_unlock(&tasklist_lock);
  1061. mutex_unlock(&tty_mutex);
  1062. unlock_kernel();
  1063. }
  1064. void stop_tty(struct tty_struct *tty)
  1065. {
  1066. if (tty->stopped)
  1067. return;
  1068. tty->stopped = 1;
  1069. if (tty->link && tty->link->packet) {
  1070. tty->ctrl_status &= ~TIOCPKT_START;
  1071. tty->ctrl_status |= TIOCPKT_STOP;
  1072. wake_up_interruptible(&tty->link->read_wait);
  1073. }
  1074. if (tty->driver->stop)
  1075. (tty->driver->stop)(tty);
  1076. }
  1077. EXPORT_SYMBOL(stop_tty);
  1078. void start_tty(struct tty_struct *tty)
  1079. {
  1080. if (!tty->stopped || tty->flow_stopped)
  1081. return;
  1082. tty->stopped = 0;
  1083. if (tty->link && tty->link->packet) {
  1084. tty->ctrl_status &= ~TIOCPKT_STOP;
  1085. tty->ctrl_status |= TIOCPKT_START;
  1086. wake_up_interruptible(&tty->link->read_wait);
  1087. }
  1088. if (tty->driver->start)
  1089. (tty->driver->start)(tty);
  1090. /* If we have a running line discipline it may need kicking */
  1091. tty_wakeup(tty);
  1092. wake_up_interruptible(&tty->write_wait);
  1093. }
  1094. EXPORT_SYMBOL(start_tty);
  1095. static ssize_t tty_read(struct file * file, char __user * buf, size_t count,
  1096. loff_t *ppos)
  1097. {
  1098. int i;
  1099. struct tty_struct * tty;
  1100. struct inode *inode;
  1101. struct tty_ldisc *ld;
  1102. tty = (struct tty_struct *)file->private_data;
  1103. inode = file->f_dentry->d_inode;
  1104. if (tty_paranoia_check(tty, inode, "tty_read"))
  1105. return -EIO;
  1106. if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
  1107. return -EIO;
  1108. /* We want to wait for the line discipline to sort out in this
  1109. situation */
  1110. ld = tty_ldisc_ref_wait(tty);
  1111. lock_kernel();
  1112. if (ld->read)
  1113. i = (ld->read)(tty,file,buf,count);
  1114. else
  1115. i = -EIO;
  1116. tty_ldisc_deref(ld);
  1117. unlock_kernel();
  1118. if (i > 0)
  1119. inode->i_atime = current_fs_time(inode->i_sb);
  1120. return i;
  1121. }
  1122. /*
  1123. * Split writes up in sane blocksizes to avoid
  1124. * denial-of-service type attacks
  1125. */
  1126. static inline ssize_t do_tty_write(
  1127. ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
  1128. struct tty_struct *tty,
  1129. struct file *file,
  1130. const char __user *buf,
  1131. size_t count)
  1132. {
  1133. ssize_t ret = 0, written = 0;
  1134. unsigned int chunk;
  1135. if (mutex_lock_interruptible(&tty->atomic_write_lock)) {
  1136. return -ERESTARTSYS;
  1137. }
  1138. /*
  1139. * We chunk up writes into a temporary buffer. This
  1140. * simplifies low-level drivers immensely, since they
  1141. * don't have locking issues and user mode accesses.
  1142. *
  1143. * But if TTY_NO_WRITE_SPLIT is set, we should use a
  1144. * big chunk-size..
  1145. *
  1146. * The default chunk-size is 2kB, because the NTTY
  1147. * layer has problems with bigger chunks. It will
  1148. * claim to be able to handle more characters than
  1149. * it actually does.
  1150. */
  1151. chunk = 2048;
  1152. if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
  1153. chunk = 65536;
  1154. if (count < chunk)
  1155. chunk = count;
  1156. /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
  1157. if (tty->write_cnt < chunk) {
  1158. unsigned char *buf;
  1159. if (chunk < 1024)
  1160. chunk = 1024;
  1161. buf = kmalloc(chunk, GFP_KERNEL);
  1162. if (!buf) {
  1163. mutex_unlock(&tty->atomic_write_lock);
  1164. return -ENOMEM;
  1165. }
  1166. kfree(tty->write_buf);
  1167. tty->write_cnt = chunk;
  1168. tty->write_buf = buf;
  1169. }
  1170. /* Do the write .. */
  1171. for (;;) {
  1172. size_t size = count;
  1173. if (size > chunk)
  1174. size = chunk;
  1175. ret = -EFAULT;
  1176. if (copy_from_user(tty->write_buf, buf, size))
  1177. break;
  1178. lock_kernel();
  1179. ret = write(tty, file, tty->write_buf, size);
  1180. unlock_kernel();
  1181. if (ret <= 0)
  1182. break;
  1183. written += ret;
  1184. buf += ret;
  1185. count -= ret;
  1186. if (!count)
  1187. break;
  1188. ret = -ERESTARTSYS;
  1189. if (signal_pending(current))
  1190. break;
  1191. cond_resched();
  1192. }
  1193. if (written) {
  1194. struct inode *inode = file->f_dentry->d_inode;
  1195. inode->i_mtime = current_fs_time(inode->i_sb);
  1196. ret = written;
  1197. }
  1198. mutex_unlock(&tty->atomic_write_lock);
  1199. return ret;
  1200. }
  1201. static ssize_t tty_write(struct file * file, const char __user * buf, size_t count,
  1202. loff_t *ppos)
  1203. {
  1204. struct tty_struct * tty;
  1205. struct inode *inode = file->f_dentry->d_inode;
  1206. ssize_t ret;
  1207. struct tty_ldisc *ld;
  1208. tty = (struct tty_struct *)file->private_data;
  1209. if (tty_paranoia_check(tty, inode, "tty_write"))
  1210. return -EIO;
  1211. if (!tty || !tty->driver->write || (test_bit(TTY_IO_ERROR, &tty->flags)))
  1212. return -EIO;
  1213. ld = tty_ldisc_ref_wait(tty);
  1214. if (!ld->write)
  1215. ret = -EIO;
  1216. else
  1217. ret = do_tty_write(ld->write, tty, file, buf, count);
  1218. tty_ldisc_deref(ld);
  1219. return ret;
  1220. }
  1221. ssize_t redirected_tty_write(struct file * file, const char __user * buf, size_t count,
  1222. loff_t *ppos)
  1223. {
  1224. struct file *p = NULL;
  1225. spin_lock(&redirect_lock);
  1226. if (redirect) {
  1227. get_file(redirect);
  1228. p = redirect;
  1229. }
  1230. spin_unlock(&redirect_lock);
  1231. if (p) {
  1232. ssize_t res;
  1233. res = vfs_write(p, buf, count, &p->f_pos);
  1234. fput(p);
  1235. return res;
  1236. }
  1237. return tty_write(file, buf, count, ppos);
  1238. }
  1239. static char ptychar[] = "pqrstuvwxyzabcde";
  1240. static inline void pty_line_name(struct tty_driver *driver, int index, char *p)
  1241. {
  1242. int i = index + driver->name_base;
  1243. /* ->name is initialized to "ttyp", but "tty" is expected */
  1244. sprintf(p, "%s%c%x",
  1245. driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
  1246. ptychar[i >> 4 & 0xf], i & 0xf);
  1247. }
  1248. static inline void tty_line_name(struct tty_driver *driver, int index, char *p)
  1249. {
  1250. sprintf(p, "%s%d", driver->name, index + driver->name_base);
  1251. }
  1252. /*
  1253. * WSH 06/09/97: Rewritten to remove races and properly clean up after a
  1254. * failed open. The new code protects the open with a mutex, so it's
  1255. * really quite straightforward. The mutex locking can probably be
  1256. * relaxed for the (most common) case of reopening a tty.
  1257. */
  1258. static int init_dev(struct tty_driver *driver, int idx,
  1259. struct tty_struct **ret_tty)
  1260. {
  1261. struct tty_struct *tty, *o_tty;
  1262. struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
  1263. struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
  1264. int retval=0;
  1265. /* check whether we're reopening an existing tty */
  1266. if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
  1267. tty = devpts_get_tty(idx);
  1268. if (tty && driver->subtype == PTY_TYPE_MASTER)
  1269. tty = tty->link;
  1270. } else {
  1271. tty = driver->ttys[idx];
  1272. }
  1273. if (tty) goto fast_track;
  1274. /*
  1275. * First time open is complex, especially for PTY devices.
  1276. * This code guarantees that either everything succeeds and the
  1277. * TTY is ready for operation, or else the table slots are vacated
  1278. * and the allocated memory released. (Except that the termios
  1279. * and locked termios may be retained.)
  1280. */
  1281. if (!try_module_get(driver->owner)) {
  1282. retval = -ENODEV;
  1283. goto end_init;
  1284. }
  1285. o_tty = NULL;
  1286. tp = o_tp = NULL;
  1287. ltp = o_ltp = NULL;
  1288. tty = alloc_tty_struct();
  1289. if(!tty)
  1290. goto fail_no_mem;
  1291. initialize_tty_struct(tty);
  1292. tty->driver = driver;
  1293. tty->index = idx;
  1294. tty_line_name(driver, idx, tty->name);
  1295. if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
  1296. tp_loc = &tty->termios;
  1297. ltp_loc = &tty->termios_locked;
  1298. } else {
  1299. tp_loc = &driver->termios[idx];
  1300. ltp_loc = &driver->termios_locked[idx];
  1301. }
  1302. if (!*tp_loc) {
  1303. tp = (struct termios *) kmalloc(sizeof(struct termios),
  1304. GFP_KERNEL);
  1305. if (!tp)
  1306. goto free_mem_out;
  1307. *tp = driver->init_termios;
  1308. }
  1309. if (!*ltp_loc) {
  1310. ltp = (struct termios *) kmalloc(sizeof(struct termios),
  1311. GFP_KERNEL);
  1312. if (!ltp)
  1313. goto free_mem_out;
  1314. memset(ltp, 0, sizeof(struct termios));
  1315. }
  1316. if (driver->type == TTY_DRIVER_TYPE_PTY) {
  1317. o_tty = alloc_tty_struct();
  1318. if (!o_tty)
  1319. goto free_mem_out;
  1320. initialize_tty_struct(o_tty);
  1321. o_tty->driver = driver->other;
  1322. o_tty->index = idx;
  1323. tty_line_name(driver->other, idx, o_tty->name);
  1324. if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
  1325. o_tp_loc = &o_tty->termios;
  1326. o_ltp_loc = &o_tty->termios_locked;
  1327. } else {
  1328. o_tp_loc = &driver->other->termios[idx];
  1329. o_ltp_loc = &driver->other->termios_locked[idx];
  1330. }
  1331. if (!*o_tp_loc) {
  1332. o_tp = (struct termios *)
  1333. kmalloc(sizeof(struct termios), GFP_KERNEL);
  1334. if (!o_tp)
  1335. goto free_mem_out;
  1336. *o_tp = driver->other->init_termios;
  1337. }
  1338. if (!*o_ltp_loc) {
  1339. o_ltp = (struct termios *)
  1340. kmalloc(sizeof(struct termios), GFP_KERNEL);
  1341. if (!o_ltp)
  1342. goto free_mem_out;
  1343. memset(o_ltp, 0, sizeof(struct termios));
  1344. }
  1345. /*
  1346. * Everything allocated ... set up the o_tty structure.
  1347. */
  1348. if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM)) {
  1349. driver->other->ttys[idx] = o_tty;
  1350. }
  1351. if (!*o_tp_loc)
  1352. *o_tp_loc = o_tp;
  1353. if (!*o_ltp_loc)
  1354. *o_ltp_loc = o_ltp;
  1355. o_tty->termios = *o_tp_loc;
  1356. o_tty->termios_locked = *o_ltp_loc;
  1357. driver->other->refcount++;
  1358. if (driver->subtype == PTY_TYPE_MASTER)
  1359. o_tty->count++;
  1360. /* Establish the links in both directions */
  1361. tty->link = o_tty;
  1362. o_tty->link = tty;
  1363. }
  1364. /*
  1365. * All structures have been allocated, so now we install them.
  1366. * Failures after this point use release_mem to clean up, so
  1367. * there's no need to null out the local pointers.
  1368. */
  1369. if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  1370. driver->ttys[idx] = tty;
  1371. }
  1372. if (!*tp_loc)
  1373. *tp_loc = tp;
  1374. if (!*ltp_loc)
  1375. *ltp_loc = ltp;
  1376. tty->termios = *tp_loc;
  1377. tty->termios_locked = *ltp_loc;
  1378. driver->refcount++;
  1379. tty->count++;
  1380. /*
  1381. * Structures all installed ... call the ldisc open routines.
  1382. * If we fail here just call release_mem to clean up. No need
  1383. * to decrement the use counts, as release_mem doesn't care.
  1384. */
  1385. if (tty->ldisc.open) {
  1386. retval = (tty->ldisc.open)(tty);
  1387. if (retval)
  1388. goto release_mem_out;
  1389. }
  1390. if (o_tty && o_tty->ldisc.open) {
  1391. retval = (o_tty->ldisc.open)(o_tty);
  1392. if (retval) {
  1393. if (tty->ldisc.close)
  1394. (tty->ldisc.close)(tty);
  1395. goto release_mem_out;
  1396. }
  1397. tty_ldisc_enable(o_tty);
  1398. }
  1399. tty_ldisc_enable(tty);
  1400. goto success;
  1401. /*
  1402. * This fast open can be used if the tty is already open.
  1403. * No memory is allocated, and the only failures are from
  1404. * attempting to open a closing tty or attempting multiple
  1405. * opens on a pty master.
  1406. */
  1407. fast_track:
  1408. if (test_bit(TTY_CLOSING, &tty->flags)) {
  1409. retval = -EIO;
  1410. goto end_init;
  1411. }
  1412. if (driver->type == TTY_DRIVER_TYPE_PTY &&
  1413. driver->subtype == PTY_TYPE_MASTER) {
  1414. /*
  1415. * special case for PTY masters: only one open permitted,
  1416. * and the slave side open count is incremented as well.
  1417. */
  1418. if (tty->count) {
  1419. retval = -EIO;
  1420. goto end_init;
  1421. }
  1422. tty->link->count++;
  1423. }
  1424. tty->count++;
  1425. tty->driver = driver; /* N.B. why do this every time?? */
  1426. /* FIXME */
  1427. if(!test_bit(TTY_LDISC, &tty->flags))
  1428. printk(KERN_ERR "init_dev but no ldisc\n");
  1429. success:
  1430. *ret_tty = tty;
  1431. /* All paths come through here to release the mutex */
  1432. end_init:
  1433. return retval;
  1434. /* Release locally allocated memory ... nothing placed in slots */
  1435. free_mem_out:
  1436. kfree(o_tp);
  1437. if (o_tty)
  1438. free_tty_struct(o_tty);
  1439. kfree(ltp);
  1440. kfree(tp);
  1441. free_tty_struct(tty);
  1442. fail_no_mem:
  1443. module_put(driver->owner);
  1444. retval = -ENOMEM;
  1445. goto end_init;
  1446. /* call the tty release_mem routine to clean out this slot */
  1447. release_mem_out:
  1448. printk(KERN_INFO "init_dev: ldisc open failed, "
  1449. "clearing slot %d\n", idx);
  1450. release_mem(tty, idx);
  1451. goto end_init;
  1452. }
  1453. /*
  1454. * Releases memory associated with a tty structure, and clears out the
  1455. * driver table slots.
  1456. */
  1457. static void release_mem(struct tty_struct *tty, int idx)
  1458. {
  1459. struct tty_struct *o_tty;
  1460. struct termios *tp;
  1461. int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
  1462. if ((o_tty = tty->link) != NULL) {
  1463. if (!devpts)
  1464. o_tty->driver->ttys[idx] = NULL;
  1465. if (o_tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
  1466. tp = o_tty->termios;
  1467. if (!devpts)
  1468. o_tty->driver->termios[idx] = NULL;
  1469. kfree(tp);
  1470. tp = o_tty->termios_locked;
  1471. if (!devpts)
  1472. o_tty->driver->termios_locked[idx] = NULL;
  1473. kfree(tp);
  1474. }
  1475. o_tty->magic = 0;
  1476. o_tty->driver->refcount--;
  1477. file_list_lock();
  1478. list_del_init(&o_tty->tty_files);
  1479. file_list_unlock();
  1480. free_tty_struct(o_tty);
  1481. }
  1482. if (!devpts)
  1483. tty->driver->ttys[idx] = NULL;
  1484. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
  1485. tp = tty->termios;
  1486. if (!devpts)
  1487. tty->driver->termios[idx] = NULL;
  1488. kfree(tp);
  1489. tp = tty->termios_locked;
  1490. if (!devpts)
  1491. tty->driver->termios_locked[idx] = NULL;
  1492. kfree(tp);
  1493. }
  1494. tty->magic = 0;
  1495. tty->driver->refcount--;
  1496. file_list_lock();
  1497. list_del_init(&tty->tty_files);
  1498. file_list_unlock();
  1499. module_put(tty->driver->owner);
  1500. free_tty_struct(tty);
  1501. }
  1502. /*
  1503. * Even releasing the tty structures is a tricky business.. We have
  1504. * to be very careful that the structures are all released at the
  1505. * same time, as interrupts might otherwise get the wrong pointers.
  1506. *
  1507. * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
  1508. * lead to double frees or releasing memory still in use.
  1509. */
  1510. static void release_dev(struct file * filp)
  1511. {
  1512. struct tty_struct *tty, *o_tty;
  1513. int pty_master, tty_closing, o_tty_closing, do_sleep;
  1514. int devpts;
  1515. int idx;
  1516. char buf[64];
  1517. unsigned long flags;
  1518. tty = (struct tty_struct *)filp->private_data;
  1519. if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "release_dev"))
  1520. return;
  1521. check_tty_count(tty, "release_dev");
  1522. tty_fasync(-1, filp, 0);
  1523. idx = tty->index;
  1524. pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  1525. tty->driver->subtype == PTY_TYPE_MASTER);
  1526. devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
  1527. o_tty = tty->link;
  1528. #ifdef TTY_PARANOIA_CHECK
  1529. if (idx < 0 || idx >= tty->driver->num) {
  1530. printk(KERN_DEBUG "release_dev: bad idx when trying to "
  1531. "free (%s)\n", tty->name);
  1532. return;
  1533. }
  1534. if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  1535. if (tty != tty->driver->ttys[idx]) {
  1536. printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
  1537. "for (%s)\n", idx, tty->name);
  1538. return;
  1539. }
  1540. if (tty->termios != tty->driver->termios[idx]) {
  1541. printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
  1542. "for (%s)\n",
  1543. idx, tty->name);
  1544. return;
  1545. }
  1546. if (tty->termios_locked != tty->driver->termios_locked[idx]) {
  1547. printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
  1548. "termios_locked for (%s)\n",
  1549. idx, tty->name);
  1550. return;
  1551. }
  1552. }
  1553. #endif
  1554. #ifdef TTY_DEBUG_HANGUP
  1555. printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
  1556. tty_name(tty, buf), tty->count);
  1557. #endif
  1558. #ifdef TTY_PARANOIA_CHECK
  1559. if (tty->driver->other &&
  1560. !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  1561. if (o_tty != tty->driver->other->ttys[idx]) {
  1562. printk(KERN_DEBUG "release_dev: other->table[%d] "
  1563. "not o_tty for (%s)\n",
  1564. idx, tty->name);
  1565. return;
  1566. }
  1567. if (o_tty->termios != tty->driver->other->termios[idx]) {
  1568. printk(KERN_DEBUG "release_dev: other->termios[%d] "
  1569. "not o_termios for (%s)\n",
  1570. idx, tty->name);
  1571. return;
  1572. }
  1573. if (o_tty->termios_locked !=
  1574. tty->driver->other->termios_locked[idx]) {
  1575. printk(KERN_DEBUG "release_dev: other->termios_locked["
  1576. "%d] not o_termios_locked for (%s)\n",
  1577. idx, tty->name);
  1578. return;
  1579. }
  1580. if (o_tty->link != tty) {
  1581. printk(KERN_DEBUG "release_dev: bad pty pointers\n");
  1582. return;
  1583. }
  1584. }
  1585. #endif
  1586. if (tty->driver->close)
  1587. tty->driver->close(tty, filp);
  1588. /*
  1589. * Sanity check: if tty->count is going to zero, there shouldn't be
  1590. * any waiters on tty->read_wait or tty->write_wait. We test the
  1591. * wait queues and kick everyone out _before_ actually starting to
  1592. * close. This ensures that we won't block while releasing the tty
  1593. * structure.
  1594. *
  1595. * The test for the o_tty closing is necessary, since the master and
  1596. * slave sides may close in any order. If the slave side closes out
  1597. * first, its count will be one, since the master side holds an open.
  1598. * Thus this test wouldn't be triggered at the time the slave closes,
  1599. * so we do it now.
  1600. *
  1601. * Note that it's possible for the tty to be opened again while we're
  1602. * flushing out waiters. By recalculating the closing flags before
  1603. * each iteration we avoid any problems.
  1604. */
  1605. while (1) {
  1606. /* Guard against races with tty->count changes elsewhere and
  1607. opens on /dev/tty */
  1608. mutex_lock(&tty_mutex);
  1609. tty_closing = tty->count <= 1;
  1610. o_tty_closing = o_tty &&
  1611. (o_tty->count <= (pty_master ? 1 : 0));
  1612. do_sleep = 0;
  1613. if (tty_closing) {
  1614. if (waitqueue_active(&tty->read_wait)) {
  1615. wake_up(&tty->read_wait);
  1616. do_sleep++;
  1617. }
  1618. if (waitqueue_active(&tty->write_wait)) {
  1619. wake_up(&tty->write_wait);
  1620. do_sleep++;
  1621. }
  1622. }
  1623. if (o_tty_closing) {
  1624. if (waitqueue_active(&o_tty->read_wait)) {
  1625. wake_up(&o_tty->read_wait);
  1626. do_sleep++;
  1627. }
  1628. if (waitqueue_active(&o_tty->write_wait)) {
  1629. wake_up(&o_tty->write_wait);
  1630. do_sleep++;
  1631. }
  1632. }
  1633. if (!do_sleep)
  1634. break;
  1635. printk(KERN_WARNING "release_dev: %s: read/write wait queue "
  1636. "active!\n", tty_name(tty, buf));
  1637. mutex_unlock(&tty_mutex);
  1638. schedule();
  1639. }
  1640. /*
  1641. * The closing flags are now consistent with the open counts on
  1642. * both sides, and we've completed the last operation that could
  1643. * block, so it's safe to proceed with closing.
  1644. */
  1645. if (pty_master) {
  1646. if (--o_tty->count < 0) {
  1647. printk(KERN_WARNING "release_dev: bad pty slave count "
  1648. "(%d) for %s\n",
  1649. o_tty->count, tty_name(o_tty, buf));
  1650. o_tty->count = 0;
  1651. }
  1652. }
  1653. if (--tty->count < 0) {
  1654. printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
  1655. tty->count, tty_name(tty, buf));
  1656. tty->count = 0;
  1657. }
  1658. /*
  1659. * We've decremented tty->count, so we need to remove this file
  1660. * descriptor off the tty->tty_files list; this serves two
  1661. * purposes:
  1662. * - check_tty_count sees the correct number of file descriptors
  1663. * associated with this tty.
  1664. * - do_tty_hangup no longer sees this file descriptor as
  1665. * something that needs to be handled for hangups.
  1666. */
  1667. file_kill(filp);
  1668. filp->private_data = NULL;
  1669. /*
  1670. * Perform some housekeeping before deciding whether to return.
  1671. *
  1672. * Set the TTY_CLOSING flag if this was the last open. In the
  1673. * case of a pty we may have to wait around for the other side
  1674. * to close, and TTY_CLOSING makes sure we can't be reopened.
  1675. */
  1676. if(tty_closing)
  1677. set_bit(TTY_CLOSING, &tty->flags);
  1678. if(o_tty_closing)
  1679. set_bit(TTY_CLOSING, &o_tty->flags);
  1680. /*
  1681. * If _either_ side is closing, make sure there aren't any
  1682. * processes that still think tty or o_tty is their controlling
  1683. * tty.
  1684. */
  1685. if (tty_closing || o_tty_closing) {
  1686. struct task_struct *p;
  1687. read_lock(&tasklist_lock);
  1688. do_each_task_pid(tty->session, PIDTYPE_SID, p) {
  1689. p->signal->tty = NULL;
  1690. } while_each_task_pid(tty->session, PIDTYPE_SID, p);
  1691. if (o_tty)
  1692. do_each_task_pid(o_tty->session, PIDTYPE_SID, p) {
  1693. p->signal->tty = NULL;
  1694. } while_each_task_pid(o_tty->session, PIDTYPE_SID, p);
  1695. read_unlock(&tasklist_lock);
  1696. }
  1697. mutex_unlock(&tty_mutex);
  1698. /* check whether both sides are closing ... */
  1699. if (!tty_closing || (o_tty && !o_tty_closing))
  1700. return;
  1701. #ifdef TTY_DEBUG_HANGUP
  1702. printk(KERN_DEBUG "freeing tty structure...");
  1703. #endif
  1704. /*
  1705. * Prevent flush_to_ldisc() from rescheduling the work for later. Then
  1706. * kill any delayed work. As this is the final close it does not
  1707. * race with the set_ldisc code path.
  1708. */
  1709. clear_bit(TTY_LDISC, &tty->flags);
  1710. cancel_delayed_work(&tty->buf.work);
  1711. /*
  1712. * Wait for ->hangup_work and ->buf.work handlers to terminate
  1713. */
  1714. flush_scheduled_work();
  1715. /*
  1716. * Wait for any short term users (we know they are just driver
  1717. * side waiters as the file is closing so user count on the file
  1718. * side is zero.
  1719. */
  1720. spin_lock_irqsave(&tty_ldisc_lock, flags);
  1721. while(tty->ldisc.refcount)
  1722. {
  1723. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  1724. wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
  1725. spin_lock_irqsave(&tty_ldisc_lock, flags);
  1726. }
  1727. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  1728. /*
  1729. * Shutdown the current line discipline, and reset it to N_TTY.
  1730. * N.B. why reset ldisc when we're releasing the memory??
  1731. *
  1732. * FIXME: this MUST get fixed for the new reflocking
  1733. */
  1734. if (tty->ldisc.close)
  1735. (tty->ldisc.close)(tty);
  1736. tty_ldisc_put(tty->ldisc.num);
  1737. /*
  1738. * Switch the line discipline back
  1739. */
  1740. tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
  1741. tty_set_termios_ldisc(tty,N_TTY);
  1742. if (o_tty) {
  1743. /* FIXME: could o_tty be in setldisc here ? */
  1744. clear_bit(TTY_LDISC, &o_tty->flags);
  1745. if (o_tty->ldisc.close)
  1746. (o_tty->ldisc.close)(o_tty);
  1747. tty_ldisc_put(o_tty->ldisc.num);
  1748. tty_ldisc_assign(o_tty, tty_ldisc_get(N_TTY));
  1749. tty_set_termios_ldisc(o_tty,N_TTY);
  1750. }
  1751. /*
  1752. * The release_mem function takes care of the details of clearing
  1753. * the slots and preserving the termios structure.
  1754. */
  1755. release_mem(tty, idx);
  1756. #ifdef CONFIG_UNIX98_PTYS
  1757. /* Make this pty number available for reallocation */
  1758. if (devpts) {
  1759. down(&allocated_ptys_lock);
  1760. idr_remove(&allocated_ptys, idx);
  1761. up(&allocated_ptys_lock);
  1762. }
  1763. #endif
  1764. }
  1765. /*
  1766. * tty_open and tty_release keep up the tty count that contains the
  1767. * number of opens done on a tty. We cannot use the inode-count, as
  1768. * different inodes might point to the same tty.
  1769. *
  1770. * Open-counting is needed for pty masters, as well as for keeping
  1771. * track of serial lines: DTR is dropped when the last close happens.
  1772. * (This is not done solely through tty->count, now. - Ted 1/27/92)
  1773. *
  1774. * The termios state of a pty is reset on first open so that
  1775. * settings don't persist across reuse.
  1776. */
  1777. static int tty_open(struct inode * inode, struct file * filp)
  1778. {
  1779. struct tty_struct *tty;
  1780. int noctty, retval;
  1781. struct tty_driver *driver;
  1782. int index;
  1783. dev_t device = inode->i_rdev;
  1784. unsigned short saved_flags = filp->f_flags;
  1785. nonseekable_open(inode, filp);
  1786. retry_open:
  1787. noctty = filp->f_flags & O_NOCTTY;
  1788. index = -1;
  1789. retval = 0;
  1790. mutex_lock(&tty_mutex);
  1791. if (device == MKDEV(TTYAUX_MAJOR,0)) {
  1792. if (!current->signal->tty) {
  1793. mutex_unlock(&tty_mutex);
  1794. return -ENXIO;
  1795. }
  1796. driver = current->signal->tty->driver;
  1797. index = current->signal->tty->index;
  1798. filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
  1799. /* noctty = 1; */
  1800. goto got_driver;
  1801. }
  1802. #ifdef CONFIG_VT
  1803. if (device == MKDEV(TTY_MAJOR,0)) {
  1804. extern struct tty_driver *console_driver;
  1805. driver = console_driver;
  1806. index = fg_console;
  1807. noctty = 1;
  1808. goto got_driver;
  1809. }
  1810. #endif
  1811. if (device == MKDEV(TTYAUX_MAJOR,1)) {
  1812. driver = console_device(&index);
  1813. if (driver) {
  1814. /* Don't let /dev/console block */
  1815. filp->f_flags |= O_NONBLOCK;
  1816. noctty = 1;
  1817. goto got_driver;
  1818. }
  1819. mutex_unlock(&tty_mutex);
  1820. return -ENODEV;
  1821. }
  1822. driver = get_tty_driver(device, &index);
  1823. if (!driver) {
  1824. mutex_unlock(&tty_mutex);
  1825. return -ENODEV;
  1826. }
  1827. got_driver:
  1828. retval = init_dev(driver, index, &tty);
  1829. mutex_unlock(&tty_mutex);
  1830. if (retval)
  1831. return retval;
  1832. filp->private_data = tty;
  1833. file_move(filp, &tty->tty_files);
  1834. check_tty_count(tty, "tty_open");
  1835. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  1836. tty->driver->subtype == PTY_TYPE_MASTER)
  1837. noctty = 1;
  1838. #ifdef TTY_DEBUG_HANGUP
  1839. printk(KERN_DEBUG "opening %s...", tty->name);
  1840. #endif
  1841. if (!retval) {
  1842. if (tty->driver->open)
  1843. retval = tty->driver->open(tty, filp);
  1844. else
  1845. retval = -ENODEV;
  1846. }
  1847. filp->f_flags = saved_flags;
  1848. if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
  1849. retval = -EBUSY;
  1850. if (retval) {
  1851. #ifdef TTY_DEBUG_HANGUP
  1852. printk(KERN_DEBUG "error %d in opening %s...", retval,
  1853. tty->name);
  1854. #endif
  1855. release_dev(filp);
  1856. if (retval != -ERESTARTSYS)
  1857. return retval;
  1858. if (signal_pending(current))
  1859. return retval;
  1860. schedule();
  1861. /*
  1862. * Need to reset f_op in case a hangup happened.
  1863. */
  1864. if (filp->f_op == &hung_up_tty_fops)
  1865. filp->f_op = &tty_fops;
  1866. goto retry_open;
  1867. }
  1868. if (!noctty &&
  1869. current->signal->leader &&
  1870. !current->signal->tty &&
  1871. tty->session == 0) {
  1872. task_lock(current);
  1873. current->signal->tty = tty;
  1874. task_unlock(current);
  1875. current->signal->tty_old_pgrp = 0;
  1876. tty->session = current->signal->session;
  1877. tty->pgrp = process_group(current);
  1878. }
  1879. return 0;
  1880. }
  1881. #ifdef CONFIG_UNIX98_PTYS
  1882. static int ptmx_open(struct inode * inode, struct file * filp)
  1883. {
  1884. struct tty_struct *tty;
  1885. int retval;
  1886. int index;
  1887. int idr_ret;
  1888. nonseekable_open(inode, filp);
  1889. /* find a device that is not in use. */
  1890. down(&allocated_ptys_lock);
  1891. if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) {
  1892. up(&allocated_ptys_lock);
  1893. return -ENOMEM;
  1894. }
  1895. idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
  1896. if (idr_ret < 0) {
  1897. up(&allocated_ptys_lock);
  1898. if (idr_ret == -EAGAIN)
  1899. return -ENOMEM;
  1900. return -EIO;
  1901. }
  1902. if (index >= pty_limit) {
  1903. idr_remove(&allocated_ptys, index);
  1904. up(&allocated_ptys_lock);
  1905. return -EIO;
  1906. }
  1907. up(&allocated_ptys_lock);
  1908. mutex_lock(&tty_mutex);
  1909. retval = init_dev(ptm_driver, index, &tty);
  1910. mutex_unlock(&tty_mutex);
  1911. if (retval)
  1912. goto out;
  1913. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  1914. filp->private_data = tty;
  1915. file_move(filp, &tty->tty_files);
  1916. retval = -ENOMEM;
  1917. if (devpts_pty_new(tty->link))
  1918. goto out1;
  1919. check_tty_count(tty, "tty_open");
  1920. retval = ptm_driver->open(tty, filp);
  1921. if (!retval)
  1922. return 0;
  1923. out1:
  1924. release_dev(filp);
  1925. return retval;
  1926. out:
  1927. down(&allocated_ptys_lock);
  1928. idr_remove(&allocated_ptys, index);
  1929. up(&allocated_ptys_lock);
  1930. return retval;
  1931. }
  1932. #endif
  1933. static int tty_release(struct inode * inode, struct file * filp)
  1934. {
  1935. lock_kernel();
  1936. release_dev(filp);
  1937. unlock_kernel();
  1938. return 0;
  1939. }
  1940. /* No kernel lock held - fine */
  1941. static unsigned int tty_poll(struct file * filp, poll_table * wait)
  1942. {
  1943. struct tty_struct * tty;
  1944. struct tty_ldisc *ld;
  1945. int ret = 0;
  1946. tty = (struct tty_struct *)filp->private_data;
  1947. if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_poll"))
  1948. return 0;
  1949. ld = tty_ldisc_ref_wait(tty);
  1950. if (ld->poll)
  1951. ret = (ld->poll)(tty, filp, wait);
  1952. tty_ldisc_deref(ld);
  1953. return ret;
  1954. }
  1955. static int tty_fasync(int fd, struct file * filp, int on)
  1956. {
  1957. struct tty_struct * tty;
  1958. int retval;
  1959. tty = (struct tty_struct *)filp->private_data;
  1960. if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_fasync"))
  1961. return 0;
  1962. retval = fasync_helper(fd, filp, on, &tty->fasync);
  1963. if (retval <= 0)
  1964. return retval;
  1965. if (on) {
  1966. if (!waitqueue_active(&tty->read_wait))
  1967. tty->minimum_to_wake = 1;
  1968. retval = f_setown(filp, (-tty->pgrp) ? : current->pid, 0);
  1969. if (retval)
  1970. return retval;
  1971. } else {
  1972. if (!tty->fasync && !waitqueue_active(&tty->read_wait))
  1973. tty->minimum_to_wake = N_TTY_BUF_SIZE;
  1974. }
  1975. return 0;
  1976. }
  1977. static int tiocsti(struct tty_struct *tty, char __user *p)
  1978. {
  1979. char ch, mbz = 0;
  1980. struct tty_ldisc *ld;
  1981. if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
  1982. return -EPERM;
  1983. if (get_user(ch, p))
  1984. return -EFAULT;
  1985. ld = tty_ldisc_ref_wait(tty);
  1986. ld->receive_buf(tty, &ch, &mbz, 1);
  1987. tty_ldisc_deref(ld);
  1988. return 0;
  1989. }
  1990. static int tiocgwinsz(struct tty_struct *tty, struct winsize __user * arg)
  1991. {
  1992. if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
  1993. return -EFAULT;
  1994. return 0;
  1995. }
  1996. static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
  1997. struct winsize __user * arg)
  1998. {
  1999. struct winsize tmp_ws;
  2000. if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
  2001. return -EFAULT;
  2002. if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
  2003. return 0;
  2004. #ifdef CONFIG_VT
  2005. if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
  2006. int rc;
  2007. acquire_console_sem();
  2008. rc = vc_resize(tty->driver_data, tmp_ws.ws_col, tmp_ws.ws_row);
  2009. release_console_sem();
  2010. if (rc)
  2011. return -ENXIO;
  2012. }
  2013. #endif
  2014. if (tty->pgrp > 0)
  2015. kill_pg(tty->pgrp, SIGWINCH, 1);
  2016. if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
  2017. kill_pg(real_tty->pgrp, SIGWINCH, 1);
  2018. tty->winsize = tmp_ws;
  2019. real_tty->winsize = tmp_ws;
  2020. return 0;
  2021. }
  2022. static int tioccons(struct file *file)
  2023. {
  2024. if (!capable(CAP_SYS_ADMIN))
  2025. return -EPERM;
  2026. if (file->f_op->write == redirected_tty_write) {
  2027. struct file *f;
  2028. spin_lock(&redirect_lock);
  2029. f = redirect;
  2030. redirect = NULL;
  2031. spin_unlock(&redirect_lock);
  2032. if (f)
  2033. fput(f);
  2034. return 0;
  2035. }
  2036. spin_lock(&redirect_lock);
  2037. if (redirect) {
  2038. spin_unlock(&redirect_lock);
  2039. return -EBUSY;
  2040. }
  2041. get_file(file);
  2042. redirect = file;
  2043. spin_unlock(&redirect_lock);
  2044. return 0;
  2045. }
  2046. static int fionbio(struct file *file, int __user *p)
  2047. {
  2048. int nonblock;
  2049. if (get_user(nonblock, p))
  2050. return -EFAULT;
  2051. if (nonblock)
  2052. file->f_flags |= O_NONBLOCK;
  2053. else
  2054. file->f_flags &= ~O_NONBLOCK;
  2055. return 0;
  2056. }
  2057. static int tiocsctty(struct tty_struct *tty, int arg)
  2058. {
  2059. task_t *p;
  2060. if (current->signal->leader &&
  2061. (current->signal->session == tty->session))
  2062. return 0;
  2063. /*
  2064. * The process must be a session leader and
  2065. * not have a controlling tty already.
  2066. */
  2067. if (!current->signal->leader || current->signal->tty)
  2068. return -EPERM;
  2069. if (tty->session > 0) {
  2070. /*
  2071. * This tty is already the controlling
  2072. * tty for another session group!
  2073. */
  2074. if ((arg == 1) && capable(CAP_SYS_ADMIN)) {
  2075. /*
  2076. * Steal it away
  2077. */
  2078. read_lock(&tasklist_lock);
  2079. do_each_task_pid(tty->session, PIDTYPE_SID, p) {
  2080. p->signal->tty = NULL;
  2081. } while_each_task_pid(tty->session, PIDTYPE_SID, p);
  2082. read_unlock(&tasklist_lock);
  2083. } else
  2084. return -EPERM;
  2085. }
  2086. task_lock(current);
  2087. current->signal->tty = tty;
  2088. task_unlock(current);
  2089. current->signal->tty_old_pgrp = 0;
  2090. tty->session = current->signal->session;
  2091. tty->pgrp = process_group(current);
  2092. return 0;
  2093. }
  2094. static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2095. {
  2096. /*
  2097. * (tty == real_tty) is a cheap way of
  2098. * testing if the tty is NOT a master pty.
  2099. */
  2100. if (tty == real_tty && current->signal->tty != real_tty)
  2101. return -ENOTTY;
  2102. return put_user(real_tty->pgrp, p);
  2103. }
  2104. static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2105. {
  2106. pid_t pgrp;
  2107. int retval = tty_check_change(real_tty);
  2108. if (retval == -EIO)
  2109. return -ENOTTY;
  2110. if (retval)
  2111. return retval;
  2112. if (!current->signal->tty ||
  2113. (current->signal->tty != real_tty) ||
  2114. (real_tty->session != current->signal->session))
  2115. return -ENOTTY;
  2116. if (get_user(pgrp, p))
  2117. return -EFAULT;
  2118. if (pgrp < 0)
  2119. return -EINVAL;
  2120. if (session_of_pgrp(pgrp) != current->signal->session)
  2121. return -EPERM;
  2122. real_tty->pgrp = pgrp;
  2123. return 0;
  2124. }
  2125. static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2126. {
  2127. /*
  2128. * (tty == real_tty) is a cheap way of
  2129. * testing if the tty is NOT a master pty.
  2130. */
  2131. if (tty == real_tty && current->signal->tty != real_tty)
  2132. return -ENOTTY;
  2133. if (real_tty->session <= 0)
  2134. return -ENOTTY;
  2135. return put_user(real_tty->session, p);
  2136. }
  2137. static int tiocsetd(struct tty_struct *tty, int __user *p)
  2138. {
  2139. int ldisc;
  2140. if (get_user(ldisc, p))
  2141. return -EFAULT;
  2142. return tty_set_ldisc(tty, ldisc);
  2143. }
  2144. static int send_break(struct tty_struct *tty, unsigned int duration)
  2145. {
  2146. tty->driver->break_ctl(tty, -1);
  2147. if (!signal_pending(current)) {
  2148. msleep_interruptible(duration);
  2149. }
  2150. tty->driver->break_ctl(tty, 0);
  2151. if (signal_pending(current))
  2152. return -EINTR;
  2153. return 0;
  2154. }
  2155. static int
  2156. tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
  2157. {
  2158. int retval = -EINVAL;
  2159. if (tty->driver->tiocmget) {
  2160. retval = tty->driver->tiocmget(tty, file);
  2161. if (retval >= 0)
  2162. retval = put_user(retval, p);
  2163. }
  2164. return retval;
  2165. }
  2166. static int
  2167. tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
  2168. unsigned __user *p)
  2169. {
  2170. int retval = -EINVAL;
  2171. if (tty->driver->tiocmset) {
  2172. unsigned int set, clear, val;
  2173. retval = get_user(val, p);
  2174. if (retval)
  2175. return retval;
  2176. set = clear = 0;
  2177. switch (cmd) {
  2178. case TIOCMBIS:
  2179. set = val;
  2180. break;
  2181. case TIOCMBIC:
  2182. clear = val;
  2183. break;
  2184. case TIOCMSET:
  2185. set = val;
  2186. clear = ~val;
  2187. break;
  2188. }
  2189. set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
  2190. clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
  2191. retval = tty->driver->tiocmset(tty, file, set, clear);
  2192. }
  2193. return retval;
  2194. }
  2195. /*
  2196. * Split this up, as gcc can choke on it otherwise..
  2197. */
  2198. int tty_ioctl(struct inode * inode, struct file * file,
  2199. unsigned int cmd, unsigned long arg)
  2200. {
  2201. struct tty_struct *tty, *real_tty;
  2202. void __user *p = (void __user *)arg;
  2203. int retval;
  2204. struct tty_ldisc *ld;
  2205. tty = (struct tty_struct *)file->private_data;
  2206. if (tty_paranoia_check(tty, inode, "tty_ioctl"))
  2207. return -EINVAL;
  2208. real_tty = tty;
  2209. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  2210. tty->driver->subtype == PTY_TYPE_MASTER)
  2211. real_tty = tty->link;
  2212. /*
  2213. * Break handling by driver
  2214. */
  2215. if (!tty->driver->break_ctl) {
  2216. switch(cmd) {
  2217. case TIOCSBRK:
  2218. case TIOCCBRK:
  2219. if (tty->driver->ioctl)
  2220. return tty->driver->ioctl(tty, file, cmd, arg);
  2221. return -EINVAL;
  2222. /* These two ioctl's always return success; even if */
  2223. /* the driver doesn't support them. */
  2224. case TCSBRK:
  2225. case TCSBRKP:
  2226. if (!tty->driver->ioctl)
  2227. return 0;
  2228. retval = tty->driver->ioctl(tty, file, cmd, arg);
  2229. if (retval == -ENOIOCTLCMD)
  2230. retval = 0;
  2231. return retval;
  2232. }
  2233. }
  2234. /*
  2235. * Factor out some common prep work
  2236. */
  2237. switch (cmd) {
  2238. case TIOCSETD:
  2239. case TIOCSBRK:
  2240. case TIOCCBRK:
  2241. case TCSBRK:
  2242. case TCSBRKP:
  2243. retval = tty_check_change(tty);
  2244. if (retval)
  2245. return retval;
  2246. if (cmd != TIOCCBRK) {
  2247. tty_wait_until_sent(tty, 0);
  2248. if (signal_pending(current))
  2249. return -EINTR;
  2250. }
  2251. break;
  2252. }
  2253. switch (cmd) {
  2254. case TIOCSTI:
  2255. return tiocsti(tty, p);
  2256. case TIOCGWINSZ:
  2257. return tiocgwinsz(tty, p);
  2258. case TIOCSWINSZ:
  2259. return tiocswinsz(tty, real_tty, p);
  2260. case TIOCCONS:
  2261. return real_tty!=tty ? -EINVAL : tioccons(file);
  2262. case FIONBIO:
  2263. return fionbio(file, p);
  2264. case TIOCEXCL:
  2265. set_bit(TTY_EXCLUSIVE, &tty->flags);
  2266. return 0;
  2267. case TIOCNXCL:
  2268. clear_bit(TTY_EXCLUSIVE, &tty->flags);
  2269. return 0;
  2270. case TIOCNOTTY:
  2271. if (current->signal->tty != tty)
  2272. return -ENOTTY;
  2273. if (current->signal->leader)
  2274. disassociate_ctty(0);
  2275. task_lock(current);
  2276. current->signal->tty = NULL;
  2277. task_unlock(current);
  2278. return 0;
  2279. case TIOCSCTTY:
  2280. return tiocsctty(tty, arg);
  2281. case TIOCGPGRP:
  2282. return tiocgpgrp(tty, real_tty, p);
  2283. case TIOCSPGRP:
  2284. return tiocspgrp(tty, real_tty, p);
  2285. case TIOCGSID:
  2286. return tiocgsid(tty, real_tty, p);
  2287. case TIOCGETD:
  2288. /* FIXME: check this is ok */
  2289. return put_user(tty->ldisc.num, (int __user *)p);
  2290. case TIOCSETD:
  2291. return tiocsetd(tty, p);
  2292. #ifdef CONFIG_VT
  2293. case TIOCLINUX:
  2294. return tioclinux(tty, arg);
  2295. #endif
  2296. /*
  2297. * Break handling
  2298. */
  2299. case TIOCSBRK: /* Turn break on, unconditionally */
  2300. tty->driver->break_ctl(tty, -1);
  2301. return 0;
  2302. case TIOCCBRK: /* Turn break off, unconditionally */
  2303. tty->driver->break_ctl(tty, 0);
  2304. return 0;
  2305. case TCSBRK: /* SVID version: non-zero arg --> no break */
  2306. /* non-zero arg means wait for all output data
  2307. * to be sent (performed above) but don't send break.
  2308. * This is used by the tcdrain() termios function.
  2309. */
  2310. if (!arg)
  2311. return send_break(tty, 250);
  2312. return 0;
  2313. case TCSBRKP: /* support for POSIX tcsendbreak() */
  2314. return send_break(tty, arg ? arg*100 : 250);
  2315. case TIOCMGET:
  2316. return tty_tiocmget(tty, file, p);
  2317. case TIOCMSET:
  2318. case TIOCMBIC:
  2319. case TIOCMBIS:
  2320. return tty_tiocmset(tty, file, cmd, p);
  2321. }
  2322. if (tty->driver->ioctl) {
  2323. retval = (tty->driver->ioctl)(tty, file, cmd, arg);
  2324. if (retval != -ENOIOCTLCMD)
  2325. return retval;
  2326. }
  2327. ld = tty_ldisc_ref_wait(tty);
  2328. retval = -EINVAL;
  2329. if (ld->ioctl) {
  2330. retval = ld->ioctl(tty, file, cmd, arg);
  2331. if (retval == -ENOIOCTLCMD)
  2332. retval = -EINVAL;
  2333. }
  2334. tty_ldisc_deref(ld);
  2335. return retval;
  2336. }
  2337. /*
  2338. * This implements the "Secure Attention Key" --- the idea is to
  2339. * prevent trojan horses by killing all processes associated with this
  2340. * tty when the user hits the "Secure Attention Key". Required for
  2341. * super-paranoid applications --- see the Orange Book for more details.
  2342. *
  2343. * This code could be nicer; ideally it should send a HUP, wait a few
  2344. * seconds, then send a INT, and then a KILL signal. But you then
  2345. * have to coordinate with the init process, since all processes associated
  2346. * with the current tty must be dead before the new getty is allowed
  2347. * to spawn.
  2348. *
  2349. * Now, if it would be correct ;-/ The current code has a nasty hole -
  2350. * it doesn't catch files in flight. We may send the descriptor to ourselves
  2351. * via AF_UNIX socket, close it and later fetch from socket. FIXME.
  2352. *
  2353. * Nasty bug: do_SAK is being called in interrupt context. This can
  2354. * deadlock. We punt it up to process context. AKPM - 16Mar2001
  2355. */
  2356. static void __do_SAK(void *arg)
  2357. {
  2358. #ifdef TTY_SOFT_SAK
  2359. tty_hangup(tty);
  2360. #else
  2361. struct tty_struct *tty = arg;
  2362. struct task_struct *g, *p;
  2363. int session;
  2364. int i;
  2365. struct file *filp;
  2366. struct tty_ldisc *disc;
  2367. struct fdtable *fdt;
  2368. if (!tty)
  2369. return;
  2370. session = tty->session;
  2371. /* We don't want an ldisc switch during this */
  2372. disc = tty_ldisc_ref(tty);
  2373. if (disc && disc->flush_buffer)
  2374. disc->flush_buffer(tty);
  2375. tty_ldisc_deref(disc);
  2376. if (tty->driver->flush_buffer)
  2377. tty->driver->flush_buffer(tty);
  2378. read_lock(&tasklist_lock);
  2379. /* Kill the entire session */
  2380. do_each_task_pid(session, PIDTYPE_SID, p) {
  2381. printk(KERN_NOTICE "SAK: killed process %d"
  2382. " (%s): p->signal->session==tty->session\n",
  2383. p->pid, p->comm);
  2384. send_sig(SIGKILL, p, 1);
  2385. } while_each_task_pid(session, PIDTYPE_SID, p);
  2386. /* Now kill any processes that happen to have the
  2387. * tty open.
  2388. */
  2389. do_each_thread(g, p) {
  2390. if (p->signal->tty == tty) {
  2391. printk(KERN_NOTICE "SAK: killed process %d"
  2392. " (%s): p->signal->session==tty->session\n",
  2393. p->pid, p->comm);
  2394. send_sig(SIGKILL, p, 1);
  2395. continue;
  2396. }
  2397. task_lock(p);
  2398. if (p->files) {
  2399. /*
  2400. * We don't take a ref to the file, so we must
  2401. * hold ->file_lock instead.
  2402. */
  2403. spin_lock(&p->files->file_lock);
  2404. fdt = files_fdtable(p->files);
  2405. for (i=0; i < fdt->max_fds; i++) {
  2406. filp = fcheck_files(p->files, i);
  2407. if (!filp)
  2408. continue;
  2409. if (filp->f_op->read == tty_read &&
  2410. filp->private_data == tty) {
  2411. printk(KERN_NOTICE "SAK: killed process %d"
  2412. " (%s): fd#%d opened to the tty\n",
  2413. p->pid, p->comm, i);
  2414. force_sig(SIGKILL, p);
  2415. break;
  2416. }
  2417. }
  2418. spin_unlock(&p->files->file_lock);
  2419. }
  2420. task_unlock(p);
  2421. } while_each_thread(g, p);
  2422. read_unlock(&tasklist_lock);
  2423. #endif
  2424. }
  2425. /*
  2426. * The tq handling here is a little racy - tty->SAK_work may already be queued.
  2427. * Fortunately we don't need to worry, because if ->SAK_work is already queued,
  2428. * the values which we write to it will be identical to the values which it
  2429. * already has. --akpm
  2430. */
  2431. void do_SAK(struct tty_struct *tty)
  2432. {
  2433. if (!tty)
  2434. return;
  2435. PREPARE_WORK(&tty->SAK_work, __do_SAK, tty);
  2436. schedule_work(&tty->SAK_work);
  2437. }
  2438. EXPORT_SYMBOL(do_SAK);
  2439. /*
  2440. * This routine is called out of the software interrupt to flush data
  2441. * from the buffer chain to the line discipline.
  2442. */
  2443. static void flush_to_ldisc(void *private_)
  2444. {
  2445. struct tty_struct *tty = (struct tty_struct *) private_;
  2446. unsigned long flags;
  2447. struct tty_ldisc *disc;
  2448. struct tty_buffer *tbuf, *head;
  2449. char *char_buf;
  2450. unsigned char *flag_buf;
  2451. disc = tty_ldisc_ref(tty);
  2452. if (disc == NULL) /* !TTY_LDISC */
  2453. return;
  2454. spin_lock_irqsave(&tty->buf.lock, flags);
  2455. head = tty->buf.head;
  2456. if (head != NULL) {
  2457. tty->buf.head = NULL;
  2458. for (;;) {
  2459. int count = head->commit - head->read;
  2460. if (!count) {
  2461. if (head->next == NULL)
  2462. break;
  2463. tbuf = head;
  2464. head = head->next;
  2465. tty_buffer_free(tty, tbuf);
  2466. continue;
  2467. }
  2468. if (!tty->receive_room) {
  2469. schedule_delayed_work(&tty->buf.work, 1);
  2470. break;
  2471. }
  2472. if (count > tty->receive_room)
  2473. count = tty->receive_room;
  2474. char_buf = head->char_buf_ptr + head->read;
  2475. flag_buf = head->flag_buf_ptr + head->read;
  2476. head->read += count;
  2477. spin_unlock_irqrestore(&tty->buf.lock, flags);
  2478. disc->receive_buf(tty, char_buf, flag_buf, count);
  2479. spin_lock_irqsave(&tty->buf.lock, flags);
  2480. }
  2481. tty->buf.head = head;
  2482. }
  2483. spin_unlock_irqrestore(&tty->buf.lock, flags);
  2484. tty_ldisc_deref(disc);
  2485. }
  2486. /*
  2487. * Routine which returns the baud rate of the tty
  2488. *
  2489. * Note that the baud_table needs to be kept in sync with the
  2490. * include/asm/termbits.h file.
  2491. */
  2492. static int baud_table[] = {
  2493. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  2494. 9600, 19200, 38400, 57600, 115200, 230400, 460800,
  2495. #ifdef __sparc__
  2496. 76800, 153600, 307200, 614400, 921600
  2497. #else
  2498. 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
  2499. 2500000, 3000000, 3500000, 4000000
  2500. #endif
  2501. };
  2502. static int n_baud_table = ARRAY_SIZE(baud_table);
  2503. /**
  2504. * tty_termios_baud_rate
  2505. * @termios: termios structure
  2506. *
  2507. * Convert termios baud rate data into a speed. This should be called
  2508. * with the termios lock held if this termios is a terminal termios
  2509. * structure. May change the termios data.
  2510. */
  2511. int tty_termios_baud_rate(struct termios *termios)
  2512. {
  2513. unsigned int cbaud;
  2514. cbaud = termios->c_cflag & CBAUD;
  2515. if (cbaud & CBAUDEX) {
  2516. cbaud &= ~CBAUDEX;
  2517. if (cbaud < 1 || cbaud + 15 > n_baud_table)
  2518. termios->c_cflag &= ~CBAUDEX;
  2519. else
  2520. cbaud += 15;
  2521. }
  2522. return baud_table[cbaud];
  2523. }
  2524. EXPORT_SYMBOL(tty_termios_baud_rate);
  2525. /**
  2526. * tty_get_baud_rate - get tty bit rates
  2527. * @tty: tty to query
  2528. *
  2529. * Returns the baud rate as an integer for this terminal. The
  2530. * termios lock must be held by the caller and the terminal bit
  2531. * flags may be updated.
  2532. */
  2533. int tty_get_baud_rate(struct tty_struct *tty)
  2534. {
  2535. int baud = tty_termios_baud_rate(tty->termios);
  2536. if (baud == 38400 && tty->alt_speed) {
  2537. if (!tty->warned) {
  2538. printk(KERN_WARNING "Use of setserial/setrocket to "
  2539. "set SPD_* flags is deprecated\n");
  2540. tty->warned = 1;
  2541. }
  2542. baud = tty->alt_speed;
  2543. }
  2544. return baud;
  2545. }
  2546. EXPORT_SYMBOL(tty_get_baud_rate);
  2547. /**
  2548. * tty_flip_buffer_push - terminal
  2549. * @tty: tty to push
  2550. *
  2551. * Queue a push of the terminal flip buffers to the line discipline. This
  2552. * function must not be called from IRQ context if tty->low_latency is set.
  2553. *
  2554. * In the event of the queue being busy for flipping the work will be
  2555. * held off and retried later.
  2556. */
  2557. void tty_flip_buffer_push(struct tty_struct *tty)
  2558. {
  2559. unsigned long flags;
  2560. spin_lock_irqsave(&tty->buf.lock, flags);
  2561. if (tty->buf.tail != NULL)
  2562. tty->buf.tail->commit = tty->buf.tail->used;
  2563. spin_unlock_irqrestore(&tty->buf.lock, flags);
  2564. if (tty->low_latency)
  2565. flush_to_ldisc((void *) tty);
  2566. else
  2567. schedule_delayed_work(&tty->buf.work, 1);
  2568. }
  2569. EXPORT_SYMBOL(tty_flip_buffer_push);
  2570. /*
  2571. * This subroutine initializes a tty structure.
  2572. */
  2573. static void initialize_tty_struct(struct tty_struct *tty)
  2574. {
  2575. memset(tty, 0, sizeof(struct tty_struct));
  2576. tty->magic = TTY_MAGIC;
  2577. tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
  2578. tty->pgrp = -1;
  2579. tty->overrun_time = jiffies;
  2580. tty->buf.head = tty->buf.tail = NULL;
  2581. tty_buffer_init(tty);
  2582. INIT_WORK(&tty->buf.work, flush_to_ldisc, tty);
  2583. init_MUTEX(&tty->buf.pty_sem);
  2584. init_MUTEX(&tty->termios_sem);
  2585. init_waitqueue_head(&tty->write_wait);
  2586. init_waitqueue_head(&tty->read_wait);
  2587. INIT_WORK(&tty->hangup_work, do_tty_hangup, tty);
  2588. mutex_init(&tty->atomic_read_lock);
  2589. mutex_init(&tty->atomic_write_lock);
  2590. spin_lock_init(&tty->read_lock);
  2591. INIT_LIST_HEAD(&tty->tty_files);
  2592. INIT_WORK(&tty->SAK_work, NULL, NULL);
  2593. }
  2594. /*
  2595. * The default put_char routine if the driver did not define one.
  2596. */
  2597. static void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
  2598. {
  2599. tty->driver->write(tty, &ch, 1);
  2600. }
  2601. static struct class *tty_class;
  2602. /**
  2603. * tty_register_device - register a tty device
  2604. * @driver: the tty driver that describes the tty device
  2605. * @index: the index in the tty driver for this tty device
  2606. * @device: a struct device that is associated with this tty device.
  2607. * This field is optional, if there is no known struct device for this
  2608. * tty device it can be set to NULL safely.
  2609. *
  2610. * Returns a pointer to the class device (or ERR_PTR(-EFOO) on error).
  2611. *
  2612. * This call is required to be made to register an individual tty device if
  2613. * the tty driver's flags have the TTY_DRIVER_NO_DEVFS bit set. If that
  2614. * bit is not set, this function should not be called.
  2615. */
  2616. struct class_device *tty_register_device(struct tty_driver *driver,
  2617. unsigned index, struct device *device)
  2618. {
  2619. char name[64];
  2620. dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
  2621. if (index >= driver->num) {
  2622. printk(KERN_ERR "Attempt to register invalid tty line number "
  2623. " (%d).\n", index);
  2624. return ERR_PTR(-EINVAL);
  2625. }
  2626. devfs_mk_cdev(dev, S_IFCHR | S_IRUSR | S_IWUSR,
  2627. "%s%d", driver->devfs_name, index + driver->name_base);
  2628. if (driver->type == TTY_DRIVER_TYPE_PTY)
  2629. pty_line_name(driver, index, name);
  2630. else
  2631. tty_line_name(driver, index, name);
  2632. return class_device_create(tty_class, NULL, dev, device, "%s", name);
  2633. }
  2634. /**
  2635. * tty_unregister_device - unregister a tty device
  2636. * @driver: the tty driver that describes the tty device
  2637. * @index: the index in the tty driver for this tty device
  2638. *
  2639. * If a tty device is registered with a call to tty_register_device() then
  2640. * this function must be made when the tty device is gone.
  2641. */
  2642. void tty_unregister_device(struct tty_driver *driver, unsigned index)
  2643. {
  2644. devfs_remove("%s%d", driver->devfs_name, index + driver->name_base);
  2645. class_device_destroy(tty_class, MKDEV(driver->major, driver->minor_start) + index);
  2646. }
  2647. EXPORT_SYMBOL(tty_register_device);
  2648. EXPORT_SYMBOL(tty_unregister_device);
  2649. struct tty_driver *alloc_tty_driver(int lines)
  2650. {
  2651. struct tty_driver *driver;
  2652. driver = kmalloc(sizeof(struct tty_driver), GFP_KERNEL);
  2653. if (driver) {
  2654. memset(driver, 0, sizeof(struct tty_driver));
  2655. driver->magic = TTY_DRIVER_MAGIC;
  2656. driver->num = lines;
  2657. /* later we'll move allocation of tables here */
  2658. }
  2659. return driver;
  2660. }
  2661. void put_tty_driver(struct tty_driver *driver)
  2662. {
  2663. kfree(driver);
  2664. }
  2665. void tty_set_operations(struct tty_driver *driver, struct tty_operations *op)
  2666. {
  2667. driver->open = op->open;
  2668. driver->close = op->close;
  2669. driver->write = op->write;
  2670. driver->put_char = op->put_char;
  2671. driver->flush_chars = op->flush_chars;
  2672. driver->write_room = op->write_room;
  2673. driver->chars_in_buffer = op->chars_in_buffer;
  2674. driver->ioctl = op->ioctl;
  2675. driver->set_termios = op->set_termios;
  2676. driver->throttle = op->throttle;
  2677. driver->unthrottle = op->unthrottle;
  2678. driver->stop = op->stop;
  2679. driver->start = op->start;
  2680. driver->hangup = op->hangup;
  2681. driver->break_ctl = op->break_ctl;
  2682. driver->flush_buffer = op->flush_buffer;
  2683. driver->set_ldisc = op->set_ldisc;
  2684. driver->wait_until_sent = op->wait_until_sent;
  2685. driver->send_xchar = op->send_xchar;
  2686. driver->read_proc = op->read_proc;
  2687. driver->write_proc = op->write_proc;
  2688. driver->tiocmget = op->tiocmget;
  2689. driver->tiocmset = op->tiocmset;
  2690. }
  2691. EXPORT_SYMBOL(alloc_tty_driver);
  2692. EXPORT_SYMBOL(put_tty_driver);
  2693. EXPORT_SYMBOL(tty_set_operations);
  2694. /*
  2695. * Called by a tty driver to register itself.
  2696. */
  2697. int tty_register_driver(struct tty_driver *driver)
  2698. {
  2699. int error;
  2700. int i;
  2701. dev_t dev;
  2702. void **p = NULL;
  2703. if (driver->flags & TTY_DRIVER_INSTALLED)
  2704. return 0;
  2705. if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  2706. p = kmalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
  2707. if (!p)
  2708. return -ENOMEM;
  2709. memset(p, 0, driver->num * 3 * sizeof(void *));
  2710. }
  2711. if (!driver->major) {
  2712. error = alloc_chrdev_region(&dev, driver->minor_start, driver->num,
  2713. (char*)driver->name);
  2714. if (!error) {
  2715. driver->major = MAJOR(dev);
  2716. driver->minor_start = MINOR(dev);
  2717. }
  2718. } else {
  2719. dev = MKDEV(driver->major, driver->minor_start);
  2720. error = register_chrdev_region(dev, driver->num,
  2721. (char*)driver->name);
  2722. }
  2723. if (error < 0) {
  2724. kfree(p);
  2725. return error;
  2726. }
  2727. if (p) {
  2728. driver->ttys = (struct tty_struct **)p;
  2729. driver->termios = (struct termios **)(p + driver->num);
  2730. driver->termios_locked = (struct termios **)(p + driver->num * 2);
  2731. } else {
  2732. driver->ttys = NULL;
  2733. driver->termios = NULL;
  2734. driver->termios_locked = NULL;
  2735. }
  2736. cdev_init(&driver->cdev, &tty_fops);
  2737. driver->cdev.owner = driver->owner;
  2738. error = cdev_add(&driver->cdev, dev, driver->num);
  2739. if (error) {
  2740. cdev_del(&driver->cdev);
  2741. unregister_chrdev_region(dev, driver->num);
  2742. driver->ttys = NULL;
  2743. driver->termios = driver->termios_locked = NULL;
  2744. kfree(p);
  2745. return error;
  2746. }
  2747. if (!driver->put_char)
  2748. driver->put_char = tty_default_put_char;
  2749. list_add(&driver->tty_drivers, &tty_drivers);
  2750. if ( !(driver->flags & TTY_DRIVER_NO_DEVFS) ) {
  2751. for(i = 0; i < driver->num; i++)
  2752. tty_register_device(driver, i, NULL);
  2753. }
  2754. proc_tty_register_driver(driver);
  2755. return 0;
  2756. }
  2757. EXPORT_SYMBOL(tty_register_driver);
  2758. /*
  2759. * Called by a tty driver to unregister itself.
  2760. */
  2761. int tty_unregister_driver(struct tty_driver *driver)
  2762. {
  2763. int i;
  2764. struct termios *tp;
  2765. void *p;
  2766. if (driver->refcount)
  2767. return -EBUSY;
  2768. unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
  2769. driver->num);
  2770. list_del(&driver->tty_drivers);
  2771. /*
  2772. * Free the termios and termios_locked structures because
  2773. * we don't want to get memory leaks when modular tty
  2774. * drivers are removed from the kernel.
  2775. */
  2776. for (i = 0; i < driver->num; i++) {
  2777. tp = driver->termios[i];
  2778. if (tp) {
  2779. driver->termios[i] = NULL;
  2780. kfree(tp);
  2781. }
  2782. tp = driver->termios_locked[i];
  2783. if (tp) {
  2784. driver->termios_locked[i] = NULL;
  2785. kfree(tp);
  2786. }
  2787. if (!(driver->flags & TTY_DRIVER_NO_DEVFS))
  2788. tty_unregister_device(driver, i);
  2789. }
  2790. p = driver->ttys;
  2791. proc_tty_unregister_driver(driver);
  2792. driver->ttys = NULL;
  2793. driver->termios = driver->termios_locked = NULL;
  2794. kfree(p);
  2795. cdev_del(&driver->cdev);
  2796. return 0;
  2797. }
  2798. EXPORT_SYMBOL(tty_unregister_driver);
  2799. /*
  2800. * Initialize the console device. This is called *early*, so
  2801. * we can't necessarily depend on lots of kernel help here.
  2802. * Just do some early initializations, and do the complex setup
  2803. * later.
  2804. */
  2805. void __init console_init(void)
  2806. {
  2807. initcall_t *call;
  2808. /* Setup the default TTY line discipline. */
  2809. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  2810. /*
  2811. * set up the console device so that later boot sequences can
  2812. * inform about problems etc..
  2813. */
  2814. #ifdef CONFIG_EARLY_PRINTK
  2815. disable_early_printk();
  2816. #endif
  2817. call = __con_initcall_start;
  2818. while (call < __con_initcall_end) {
  2819. (*call)();
  2820. call++;
  2821. }
  2822. }
  2823. #ifdef CONFIG_VT
  2824. extern int vty_init(void);
  2825. #endif
  2826. static int __init tty_class_init(void)
  2827. {
  2828. tty_class = class_create(THIS_MODULE, "tty");
  2829. if (IS_ERR(tty_class))
  2830. return PTR_ERR(tty_class);
  2831. return 0;
  2832. }
  2833. postcore_initcall(tty_class_init);
  2834. /* 3/2004 jmc: why do these devices exist? */
  2835. static struct cdev tty_cdev, console_cdev;
  2836. #ifdef CONFIG_UNIX98_PTYS
  2837. static struct cdev ptmx_cdev;
  2838. #endif
  2839. #ifdef CONFIG_VT
  2840. static struct cdev vc0_cdev;
  2841. #endif
  2842. /*
  2843. * Ok, now we can initialize the rest of the tty devices and can count
  2844. * on memory allocations, interrupts etc..
  2845. */
  2846. static int __init tty_init(void)
  2847. {
  2848. cdev_init(&tty_cdev, &tty_fops);
  2849. if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
  2850. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
  2851. panic("Couldn't register /dev/tty driver\n");
  2852. devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 0), S_IFCHR|S_IRUGO|S_IWUGO, "tty");
  2853. class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
  2854. cdev_init(&console_cdev, &console_fops);
  2855. if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
  2856. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
  2857. panic("Couldn't register /dev/console driver\n");
  2858. devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 1), S_IFCHR|S_IRUSR|S_IWUSR, "console");
  2859. class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, "console");
  2860. #ifdef CONFIG_UNIX98_PTYS
  2861. cdev_init(&ptmx_cdev, &ptmx_fops);
  2862. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  2863. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  2864. panic("Couldn't register /dev/ptmx driver\n");
  2865. devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 2), S_IFCHR|S_IRUGO|S_IWUGO, "ptmx");
  2866. class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
  2867. #endif
  2868. #ifdef CONFIG_VT
  2869. cdev_init(&vc0_cdev, &console_fops);
  2870. if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
  2871. register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
  2872. panic("Couldn't register /dev/tty0 driver\n");
  2873. devfs_mk_cdev(MKDEV(TTY_MAJOR, 0), S_IFCHR|S_IRUSR|S_IWUSR, "vc/0");
  2874. class_device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
  2875. vty_init();
  2876. #endif
  2877. return 0;
  2878. }
  2879. module_init(tty_init);