tty_io.c 81 KB

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