tty_io.c 79 KB

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