tty_io.c 80 KB

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