tty_io.c 79 KB

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