tty_io.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244
  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 tty_driver_kref_get(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 = tty_driver_kref_get(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. * tty_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_driver_install_tty() - install a tty entry in the driver
  1104. * @driver: the driver for the tty
  1105. * @tty: the tty
  1106. *
  1107. * Install a tty object into the driver tables. The tty->index field
  1108. * will be set by the time this is called.
  1109. *
  1110. * Locking: tty_mutex for now
  1111. */
  1112. static int tty_driver_install_tty(struct tty_driver *driver,
  1113. struct tty_struct *tty)
  1114. {
  1115. if (driver->ops->install)
  1116. return driver->ops->install(driver, tty);
  1117. driver->ttys[tty->index] = tty;
  1118. return 0;
  1119. }
  1120. /**
  1121. * tty_driver_remove_tty() - remove a tty from the driver tables
  1122. * @driver: the driver for the tty
  1123. * @idx: the minor number
  1124. *
  1125. * Remvoe a tty object from the driver tables. The tty->index field
  1126. * will be set by the time this is called.
  1127. *
  1128. * Locking: tty_mutex for now
  1129. */
  1130. static void tty_driver_remove_tty(struct tty_driver *driver,
  1131. struct tty_struct *tty)
  1132. {
  1133. if (driver->ops->remove)
  1134. driver->ops->remove(driver, tty);
  1135. else
  1136. driver->ttys[tty->index] = NULL;
  1137. }
  1138. /*
  1139. * tty_reopen() - fast re-open of an open tty
  1140. * @tty - the tty to open
  1141. *
  1142. * Return 0 on success, -errno on error.
  1143. *
  1144. * Locking: tty_mutex must be held from the time the tty was found
  1145. * till this open completes.
  1146. */
  1147. static int tty_reopen(struct tty_struct *tty)
  1148. {
  1149. struct tty_driver *driver = tty->driver;
  1150. if (test_bit(TTY_CLOSING, &tty->flags))
  1151. return -EIO;
  1152. if (driver->type == TTY_DRIVER_TYPE_PTY &&
  1153. driver->subtype == PTY_TYPE_MASTER) {
  1154. /*
  1155. * special case for PTY masters: only one open permitted,
  1156. * and the slave side open count is incremented as well.
  1157. */
  1158. if (tty->count)
  1159. return -EIO;
  1160. tty->link->count++;
  1161. }
  1162. tty->count++;
  1163. tty->driver = driver; /* N.B. why do this every time?? */
  1164. WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
  1165. return 0;
  1166. }
  1167. /**
  1168. * tty_init_dev - initialise a tty device
  1169. * @driver: tty driver we are opening a device on
  1170. * @idx: device index
  1171. * @ret_tty: returned tty structure
  1172. * @first_ok: ok to open a new device (used by ptmx)
  1173. *
  1174. * Prepare a tty device. This may not be a "new" clean device but
  1175. * could also be an active device. The pty drivers require special
  1176. * handling because of this.
  1177. *
  1178. * Locking:
  1179. * The function is called under the tty_mutex, which
  1180. * protects us from the tty struct or driver itself going away.
  1181. *
  1182. * On exit the tty device has the line discipline attached and
  1183. * a reference count of 1. If a pair was created for pty/tty use
  1184. * and the other was a pty master then it too has a reference count of 1.
  1185. *
  1186. * WSH 06/09/97: Rewritten to remove races and properly clean up after a
  1187. * failed open. The new code protects the open with a mutex, so it's
  1188. * really quite straightforward. The mutex locking can probably be
  1189. * relaxed for the (most common) case of reopening a tty.
  1190. */
  1191. struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
  1192. int first_ok)
  1193. {
  1194. struct tty_struct *tty, *o_tty;
  1195. struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc;
  1196. struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
  1197. int retval;
  1198. /* check whether we're reopening an existing tty */
  1199. tty = tty_driver_lookup_tty(driver, idx);
  1200. if (IS_ERR(tty))
  1201. return tty;
  1202. if (tty) {
  1203. retval = tty_reopen(tty);
  1204. if (retval)
  1205. return ERR_PTR(retval);
  1206. return tty;
  1207. }
  1208. /* Check if pty master is being opened multiple times */
  1209. if (driver->subtype == PTY_TYPE_MASTER &&
  1210. (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok)
  1211. return ERR_PTR(-EIO);
  1212. /*
  1213. * First time open is complex, especially for PTY devices.
  1214. * This code guarantees that either everything succeeds and the
  1215. * TTY is ready for operation, or else the table slots are vacated
  1216. * and the allocated memory released. (Except that the termios
  1217. * and locked termios may be retained.)
  1218. */
  1219. if (!try_module_get(driver->owner))
  1220. return ERR_PTR(-ENODEV);
  1221. o_tty = NULL;
  1222. tp = o_tp = NULL;
  1223. ltp = o_ltp = NULL;
  1224. tty = alloc_tty_struct();
  1225. if (!tty)
  1226. goto fail_no_mem;
  1227. initialize_tty_struct(tty);
  1228. tty->driver = driver;
  1229. tty->ops = driver->ops;
  1230. tty->index = idx;
  1231. tty_line_name(driver, idx, tty->name);
  1232. if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
  1233. tp_loc = &tty->termios;
  1234. ltp_loc = &tty->termios_locked;
  1235. } else {
  1236. tp_loc = &driver->termios[idx];
  1237. ltp_loc = &driver->termios_locked[idx];
  1238. }
  1239. if (!*tp_loc) {
  1240. tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
  1241. if (!tp)
  1242. goto free_mem_out;
  1243. *tp = driver->init_termios;
  1244. }
  1245. if (!*ltp_loc) {
  1246. ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
  1247. if (!ltp)
  1248. goto free_mem_out;
  1249. }
  1250. if (driver->type == TTY_DRIVER_TYPE_PTY) {
  1251. o_tty = alloc_tty_struct();
  1252. if (!o_tty)
  1253. goto free_mem_out;
  1254. if (!try_module_get(driver->other->owner)) {
  1255. /* This cannot in fact currently happen */
  1256. free_tty_struct(o_tty);
  1257. o_tty = NULL;
  1258. goto free_mem_out;
  1259. }
  1260. initialize_tty_struct(o_tty);
  1261. o_tty->driver = driver->other;
  1262. o_tty->ops = driver->ops;
  1263. o_tty->index = idx;
  1264. tty_line_name(driver->other, idx, o_tty->name);
  1265. if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
  1266. o_tp_loc = &o_tty->termios;
  1267. o_ltp_loc = &o_tty->termios_locked;
  1268. } else {
  1269. o_tp_loc = &driver->other->termios[idx];
  1270. o_ltp_loc = &driver->other->termios_locked[idx];
  1271. }
  1272. if (!*o_tp_loc) {
  1273. o_tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
  1274. if (!o_tp)
  1275. goto free_mem_out;
  1276. *o_tp = driver->other->init_termios;
  1277. }
  1278. if (!*o_ltp_loc) {
  1279. o_ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
  1280. if (!o_ltp)
  1281. goto free_mem_out;
  1282. }
  1283. /*
  1284. * Everything allocated ... set up the o_tty structure.
  1285. */
  1286. if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM))
  1287. driver->other->ttys[idx] = o_tty;
  1288. if (!*o_tp_loc)
  1289. *o_tp_loc = o_tp;
  1290. if (!*o_ltp_loc)
  1291. *o_ltp_loc = o_ltp;
  1292. o_tty->termios = *o_tp_loc;
  1293. o_tty->termios_locked = *o_ltp_loc;
  1294. tty_driver_kref_get(driver->other);
  1295. if (driver->subtype == PTY_TYPE_MASTER)
  1296. o_tty->count++;
  1297. /* Establish the links in both directions */
  1298. tty->link = o_tty;
  1299. o_tty->link = tty;
  1300. }
  1301. /*
  1302. * All structures have been allocated, so now we install them.
  1303. * Failures after this point use release_tty to clean up, so
  1304. * there's no need to null out the local pointers.
  1305. */
  1306. if (!*tp_loc)
  1307. *tp_loc = tp;
  1308. if (!*ltp_loc)
  1309. *ltp_loc = ltp;
  1310. tty->termios = *tp_loc;
  1311. tty->termios_locked = *ltp_loc;
  1312. /* Compatibility until drivers always set this */
  1313. tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
  1314. tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
  1315. tty_driver_kref_get(driver);
  1316. tty->count++;
  1317. retval = tty_driver_install_tty(driver, tty);
  1318. if (retval < 0)
  1319. goto release_mem_out;
  1320. /*
  1321. * Structures all installed ... call the ldisc open routines.
  1322. * If we fail here just call release_tty to clean up. No need
  1323. * to decrement the use counts, as release_tty doesn't care.
  1324. */
  1325. retval = tty_ldisc_setup(tty, o_tty);
  1326. if (retval)
  1327. goto release_mem_out;
  1328. return tty;
  1329. /* Release locally allocated memory ... nothing placed in slots */
  1330. free_mem_out:
  1331. kfree(o_tp);
  1332. if (o_tty) {
  1333. module_put(o_tty->driver->owner);
  1334. free_tty_struct(o_tty);
  1335. }
  1336. kfree(ltp);
  1337. kfree(tp);
  1338. free_tty_struct(tty);
  1339. fail_no_mem:
  1340. module_put(driver->owner);
  1341. return ERR_PTR(-ENOMEM);
  1342. /* call the tty release_tty routine to clean out this slot */
  1343. release_mem_out:
  1344. if (printk_ratelimit())
  1345. printk(KERN_INFO "tty_init_dev: ldisc open failed, "
  1346. "clearing slot %d\n", idx);
  1347. release_tty(tty, idx);
  1348. return ERR_PTR(retval);
  1349. }
  1350. void tty_free_termios(struct tty_struct *tty)
  1351. {
  1352. struct ktermios *tp;
  1353. int idx = tty->index;
  1354. /* Kill this flag and push into drivers for locking etc */
  1355. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
  1356. /* FIXME: Locking on ->termios array */
  1357. tp = tty->termios;
  1358. tty->driver->termios[idx] = NULL;
  1359. kfree(tp);
  1360. tp = tty->termios_locked;
  1361. tty->driver->termios_locked[idx] = NULL;
  1362. kfree(tp);
  1363. }
  1364. }
  1365. EXPORT_SYMBOL(tty_free_termios);
  1366. void tty_shutdown(struct tty_struct *tty)
  1367. {
  1368. tty_driver_remove_tty(tty->driver, tty);
  1369. tty_free_termios(tty);
  1370. }
  1371. EXPORT_SYMBOL(tty_shutdown);
  1372. /**
  1373. * release_one_tty - release tty structure memory
  1374. * @kref: kref of tty we are obliterating
  1375. *
  1376. * Releases memory associated with a tty structure, and clears out the
  1377. * driver table slots. This function is called when a device is no longer
  1378. * in use. It also gets called when setup of a device fails.
  1379. *
  1380. * Locking:
  1381. * tty_mutex - sometimes only
  1382. * takes the file list lock internally when working on the list
  1383. * of ttys that the driver keeps.
  1384. */
  1385. static void release_one_tty(struct kref *kref)
  1386. {
  1387. struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
  1388. struct tty_driver *driver = tty->driver;
  1389. if (tty->ops->shutdown)
  1390. tty->ops->shutdown(tty);
  1391. else
  1392. tty_shutdown(tty);
  1393. tty->magic = 0;
  1394. tty_driver_kref_put(driver);
  1395. module_put(driver->owner);
  1396. file_list_lock();
  1397. list_del_init(&tty->tty_files);
  1398. file_list_unlock();
  1399. free_tty_struct(tty);
  1400. }
  1401. /**
  1402. * tty_kref_put - release a tty kref
  1403. * @tty: tty device
  1404. *
  1405. * Release a reference to a tty device and if need be let the kref
  1406. * layer destruct the object for us
  1407. */
  1408. void tty_kref_put(struct tty_struct *tty)
  1409. {
  1410. if (tty)
  1411. kref_put(&tty->kref, release_one_tty);
  1412. }
  1413. EXPORT_SYMBOL(tty_kref_put);
  1414. /**
  1415. * release_tty - release tty structure memory
  1416. *
  1417. * Release both @tty and a possible linked partner (think pty pair),
  1418. * and decrement the refcount of the backing module.
  1419. *
  1420. * Locking:
  1421. * tty_mutex - sometimes only
  1422. * takes the file list lock internally when working on the list
  1423. * of ttys that the driver keeps.
  1424. * FIXME: should we require tty_mutex is held here ??
  1425. *
  1426. */
  1427. static void release_tty(struct tty_struct *tty, int idx)
  1428. {
  1429. /* This should always be true but check for the moment */
  1430. WARN_ON(tty->index != idx);
  1431. if (tty->link)
  1432. tty_kref_put(tty->link);
  1433. tty_kref_put(tty);
  1434. }
  1435. /*
  1436. * Even releasing the tty structures is a tricky business.. We have
  1437. * to be very careful that the structures are all released at the
  1438. * same time, as interrupts might otherwise get the wrong pointers.
  1439. *
  1440. * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
  1441. * lead to double frees or releasing memory still in use.
  1442. */
  1443. void tty_release_dev(struct file *filp)
  1444. {
  1445. struct tty_struct *tty, *o_tty;
  1446. int pty_master, tty_closing, o_tty_closing, do_sleep;
  1447. int devpts;
  1448. int idx;
  1449. char buf[64];
  1450. tty = (struct tty_struct *)filp->private_data;
  1451. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode,
  1452. "tty_release_dev"))
  1453. return;
  1454. check_tty_count(tty, "tty_release_dev");
  1455. tty_fasync(-1, filp, 0);
  1456. idx = tty->index;
  1457. pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  1458. tty->driver->subtype == PTY_TYPE_MASTER);
  1459. devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
  1460. o_tty = tty->link;
  1461. #ifdef TTY_PARANOIA_CHECK
  1462. if (idx < 0 || idx >= tty->driver->num) {
  1463. printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
  1464. "free (%s)\n", tty->name);
  1465. return;
  1466. }
  1467. if (!devpts) {
  1468. if (tty != tty->driver->ttys[idx]) {
  1469. printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
  1470. "for (%s)\n", idx, tty->name);
  1471. return;
  1472. }
  1473. if (tty->termios != tty->driver->termios[idx]) {
  1474. printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
  1475. "for (%s)\n",
  1476. idx, tty->name);
  1477. return;
  1478. }
  1479. if (tty->termios_locked != tty->driver->termios_locked[idx]) {
  1480. printk(KERN_DEBUG "tty_release_dev: driver.termios_locked[%d] not "
  1481. "termios_locked for (%s)\n",
  1482. idx, tty->name);
  1483. return;
  1484. }
  1485. }
  1486. #endif
  1487. #ifdef TTY_DEBUG_HANGUP
  1488. printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...",
  1489. tty_name(tty, buf), tty->count);
  1490. #endif
  1491. #ifdef TTY_PARANOIA_CHECK
  1492. if (tty->driver->other &&
  1493. !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
  1494. if (o_tty != tty->driver->other->ttys[idx]) {
  1495. printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
  1496. "not o_tty for (%s)\n",
  1497. idx, tty->name);
  1498. return;
  1499. }
  1500. if (o_tty->termios != tty->driver->other->termios[idx]) {
  1501. printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
  1502. "not o_termios for (%s)\n",
  1503. idx, tty->name);
  1504. return;
  1505. }
  1506. if (o_tty->termios_locked !=
  1507. tty->driver->other->termios_locked[idx]) {
  1508. printk(KERN_DEBUG "tty_release_dev: other->termios_locked["
  1509. "%d] not o_termios_locked for (%s)\n",
  1510. idx, tty->name);
  1511. return;
  1512. }
  1513. if (o_tty->link != tty) {
  1514. printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
  1515. return;
  1516. }
  1517. }
  1518. #endif
  1519. if (tty->ops->close)
  1520. tty->ops->close(tty, filp);
  1521. /*
  1522. * Sanity check: if tty->count is going to zero, there shouldn't be
  1523. * any waiters on tty->read_wait or tty->write_wait. We test the
  1524. * wait queues and kick everyone out _before_ actually starting to
  1525. * close. This ensures that we won't block while releasing the tty
  1526. * structure.
  1527. *
  1528. * The test for the o_tty closing is necessary, since the master and
  1529. * slave sides may close in any order. If the slave side closes out
  1530. * first, its count will be one, since the master side holds an open.
  1531. * Thus this test wouldn't be triggered at the time the slave closes,
  1532. * so we do it now.
  1533. *
  1534. * Note that it's possible for the tty to be opened again while we're
  1535. * flushing out waiters. By recalculating the closing flags before
  1536. * each iteration we avoid any problems.
  1537. */
  1538. while (1) {
  1539. /* Guard against races with tty->count changes elsewhere and
  1540. opens on /dev/tty */
  1541. mutex_lock(&tty_mutex);
  1542. tty_closing = tty->count <= 1;
  1543. o_tty_closing = o_tty &&
  1544. (o_tty->count <= (pty_master ? 1 : 0));
  1545. do_sleep = 0;
  1546. if (tty_closing) {
  1547. if (waitqueue_active(&tty->read_wait)) {
  1548. wake_up(&tty->read_wait);
  1549. do_sleep++;
  1550. }
  1551. if (waitqueue_active(&tty->write_wait)) {
  1552. wake_up(&tty->write_wait);
  1553. do_sleep++;
  1554. }
  1555. }
  1556. if (o_tty_closing) {
  1557. if (waitqueue_active(&o_tty->read_wait)) {
  1558. wake_up(&o_tty->read_wait);
  1559. do_sleep++;
  1560. }
  1561. if (waitqueue_active(&o_tty->write_wait)) {
  1562. wake_up(&o_tty->write_wait);
  1563. do_sleep++;
  1564. }
  1565. }
  1566. if (!do_sleep)
  1567. break;
  1568. printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
  1569. "active!\n", tty_name(tty, buf));
  1570. mutex_unlock(&tty_mutex);
  1571. schedule();
  1572. }
  1573. /*
  1574. * The closing flags are now consistent with the open counts on
  1575. * both sides, and we've completed the last operation that could
  1576. * block, so it's safe to proceed with closing.
  1577. */
  1578. if (pty_master) {
  1579. if (--o_tty->count < 0) {
  1580. printk(KERN_WARNING "tty_release_dev: bad pty slave count "
  1581. "(%d) for %s\n",
  1582. o_tty->count, tty_name(o_tty, buf));
  1583. o_tty->count = 0;
  1584. }
  1585. }
  1586. if (--tty->count < 0) {
  1587. printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n",
  1588. tty->count, tty_name(tty, buf));
  1589. tty->count = 0;
  1590. }
  1591. /*
  1592. * We've decremented tty->count, so we need to remove this file
  1593. * descriptor off the tty->tty_files list; this serves two
  1594. * purposes:
  1595. * - check_tty_count sees the correct number of file descriptors
  1596. * associated with this tty.
  1597. * - do_tty_hangup no longer sees this file descriptor as
  1598. * something that needs to be handled for hangups.
  1599. */
  1600. file_kill(filp);
  1601. filp->private_data = NULL;
  1602. /*
  1603. * Perform some housekeeping before deciding whether to return.
  1604. *
  1605. * Set the TTY_CLOSING flag if this was the last open. In the
  1606. * case of a pty we may have to wait around for the other side
  1607. * to close, and TTY_CLOSING makes sure we can't be reopened.
  1608. */
  1609. if (tty_closing)
  1610. set_bit(TTY_CLOSING, &tty->flags);
  1611. if (o_tty_closing)
  1612. set_bit(TTY_CLOSING, &o_tty->flags);
  1613. /*
  1614. * If _either_ side is closing, make sure there aren't any
  1615. * processes that still think tty or o_tty is their controlling
  1616. * tty.
  1617. */
  1618. if (tty_closing || o_tty_closing) {
  1619. read_lock(&tasklist_lock);
  1620. session_clear_tty(tty->session);
  1621. if (o_tty)
  1622. session_clear_tty(o_tty->session);
  1623. read_unlock(&tasklist_lock);
  1624. }
  1625. mutex_unlock(&tty_mutex);
  1626. /* check whether both sides are closing ... */
  1627. if (!tty_closing || (o_tty && !o_tty_closing))
  1628. return;
  1629. #ifdef TTY_DEBUG_HANGUP
  1630. printk(KERN_DEBUG "freeing tty structure...");
  1631. #endif
  1632. /*
  1633. * Ask the line discipline code to release its structures
  1634. */
  1635. tty_ldisc_release(tty, o_tty);
  1636. /*
  1637. * The release_tty function takes care of the details of clearing
  1638. * the slots and preserving the termios structure.
  1639. */
  1640. release_tty(tty, idx);
  1641. /* Make this pty number available for reallocation */
  1642. if (devpts)
  1643. devpts_kill_index(idx);
  1644. }
  1645. /**
  1646. * __tty_open - open a tty device
  1647. * @inode: inode of device file
  1648. * @filp: file pointer to tty
  1649. *
  1650. * tty_open and tty_release keep up the tty count that contains the
  1651. * number of opens done on a tty. We cannot use the inode-count, as
  1652. * different inodes might point to the same tty.
  1653. *
  1654. * Open-counting is needed for pty masters, as well as for keeping
  1655. * track of serial lines: DTR is dropped when the last close happens.
  1656. * (This is not done solely through tty->count, now. - Ted 1/27/92)
  1657. *
  1658. * The termios state of a pty is reset on first open so that
  1659. * settings don't persist across reuse.
  1660. *
  1661. * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work.
  1662. * tty->count should protect the rest.
  1663. * ->siglock protects ->signal/->sighand
  1664. */
  1665. static int __tty_open(struct inode *inode, struct file *filp)
  1666. {
  1667. struct tty_struct *tty;
  1668. int noctty, retval;
  1669. struct tty_driver *driver;
  1670. int index;
  1671. dev_t device = inode->i_rdev;
  1672. unsigned short saved_flags = filp->f_flags;
  1673. nonseekable_open(inode, filp);
  1674. retry_open:
  1675. noctty = filp->f_flags & O_NOCTTY;
  1676. index = -1;
  1677. retval = 0;
  1678. mutex_lock(&tty_mutex);
  1679. if (device == MKDEV(TTYAUX_MAJOR, 0)) {
  1680. tty = get_current_tty();
  1681. if (!tty) {
  1682. mutex_unlock(&tty_mutex);
  1683. return -ENXIO;
  1684. }
  1685. driver = tty_driver_kref_get(tty->driver);
  1686. index = tty->index;
  1687. filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
  1688. /* noctty = 1; */
  1689. /* FIXME: Should we take a driver reference ? */
  1690. tty_kref_put(tty);
  1691. goto got_driver;
  1692. }
  1693. #ifdef CONFIG_VT
  1694. if (device == MKDEV(TTY_MAJOR, 0)) {
  1695. extern struct tty_driver *console_driver;
  1696. driver = tty_driver_kref_get(console_driver);
  1697. index = fg_console;
  1698. noctty = 1;
  1699. goto got_driver;
  1700. }
  1701. #endif
  1702. if (device == MKDEV(TTYAUX_MAJOR, 1)) {
  1703. driver = tty_driver_kref_get(console_device(&index));
  1704. if (driver) {
  1705. /* Don't let /dev/console block */
  1706. filp->f_flags |= O_NONBLOCK;
  1707. noctty = 1;
  1708. goto got_driver;
  1709. }
  1710. mutex_unlock(&tty_mutex);
  1711. return -ENODEV;
  1712. }
  1713. driver = get_tty_driver(device, &index);
  1714. if (!driver) {
  1715. mutex_unlock(&tty_mutex);
  1716. return -ENODEV;
  1717. }
  1718. got_driver:
  1719. tty = tty_init_dev(driver, index, 0);
  1720. mutex_unlock(&tty_mutex);
  1721. tty_driver_kref_put(driver);
  1722. if (IS_ERR(tty))
  1723. return PTR_ERR(tty);
  1724. filp->private_data = tty;
  1725. file_move(filp, &tty->tty_files);
  1726. check_tty_count(tty, "tty_open");
  1727. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  1728. tty->driver->subtype == PTY_TYPE_MASTER)
  1729. noctty = 1;
  1730. #ifdef TTY_DEBUG_HANGUP
  1731. printk(KERN_DEBUG "opening %s...", tty->name);
  1732. #endif
  1733. if (!retval) {
  1734. if (tty->ops->open)
  1735. retval = tty->ops->open(tty, filp);
  1736. else
  1737. retval = -ENODEV;
  1738. }
  1739. filp->f_flags = saved_flags;
  1740. if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
  1741. !capable(CAP_SYS_ADMIN))
  1742. retval = -EBUSY;
  1743. if (retval) {
  1744. #ifdef TTY_DEBUG_HANGUP
  1745. printk(KERN_DEBUG "error %d in opening %s...", retval,
  1746. tty->name);
  1747. #endif
  1748. tty_release_dev(filp);
  1749. if (retval != -ERESTARTSYS)
  1750. return retval;
  1751. if (signal_pending(current))
  1752. return retval;
  1753. schedule();
  1754. /*
  1755. * Need to reset f_op in case a hangup happened.
  1756. */
  1757. if (filp->f_op == &hung_up_tty_fops)
  1758. filp->f_op = &tty_fops;
  1759. goto retry_open;
  1760. }
  1761. mutex_lock(&tty_mutex);
  1762. spin_lock_irq(&current->sighand->siglock);
  1763. if (!noctty &&
  1764. current->signal->leader &&
  1765. !current->signal->tty &&
  1766. tty->session == NULL)
  1767. __proc_set_tty(current, tty);
  1768. spin_unlock_irq(&current->sighand->siglock);
  1769. mutex_unlock(&tty_mutex);
  1770. return 0;
  1771. }
  1772. /* BKL pushdown: scary code avoidance wrapper */
  1773. static int tty_open(struct inode *inode, struct file *filp)
  1774. {
  1775. int ret;
  1776. lock_kernel();
  1777. ret = __tty_open(inode, filp);
  1778. unlock_kernel();
  1779. return ret;
  1780. }
  1781. /**
  1782. * tty_release - vfs callback for close
  1783. * @inode: inode of tty
  1784. * @filp: file pointer for handle to tty
  1785. *
  1786. * Called the last time each file handle is closed that references
  1787. * this tty. There may however be several such references.
  1788. *
  1789. * Locking:
  1790. * Takes bkl. See tty_release_dev
  1791. */
  1792. static int tty_release(struct inode *inode, struct file *filp)
  1793. {
  1794. lock_kernel();
  1795. tty_release_dev(filp);
  1796. unlock_kernel();
  1797. return 0;
  1798. }
  1799. /**
  1800. * tty_poll - check tty status
  1801. * @filp: file being polled
  1802. * @wait: poll wait structures to update
  1803. *
  1804. * Call the line discipline polling method to obtain the poll
  1805. * status of the device.
  1806. *
  1807. * Locking: locks called line discipline but ldisc poll method
  1808. * may be re-entered freely by other callers.
  1809. */
  1810. static unsigned int tty_poll(struct file *filp, poll_table *wait)
  1811. {
  1812. struct tty_struct *tty;
  1813. struct tty_ldisc *ld;
  1814. int ret = 0;
  1815. tty = (struct tty_struct *)filp->private_data;
  1816. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
  1817. return 0;
  1818. ld = tty_ldisc_ref_wait(tty);
  1819. if (ld->ops->poll)
  1820. ret = (ld->ops->poll)(tty, filp, wait);
  1821. tty_ldisc_deref(ld);
  1822. return ret;
  1823. }
  1824. static int tty_fasync(int fd, struct file *filp, int on)
  1825. {
  1826. struct tty_struct *tty;
  1827. unsigned long flags;
  1828. int retval = 0;
  1829. lock_kernel();
  1830. tty = (struct tty_struct *)filp->private_data;
  1831. if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
  1832. goto out;
  1833. retval = fasync_helper(fd, filp, on, &tty->fasync);
  1834. if (retval <= 0)
  1835. goto out;
  1836. if (on) {
  1837. enum pid_type type;
  1838. struct pid *pid;
  1839. if (!waitqueue_active(&tty->read_wait))
  1840. tty->minimum_to_wake = 1;
  1841. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1842. if (tty->pgrp) {
  1843. pid = tty->pgrp;
  1844. type = PIDTYPE_PGID;
  1845. } else {
  1846. pid = task_pid(current);
  1847. type = PIDTYPE_PID;
  1848. }
  1849. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1850. retval = __f_setown(filp, pid, type, 0);
  1851. if (retval)
  1852. goto out;
  1853. } else {
  1854. if (!tty->fasync && !waitqueue_active(&tty->read_wait))
  1855. tty->minimum_to_wake = N_TTY_BUF_SIZE;
  1856. }
  1857. retval = 0;
  1858. out:
  1859. unlock_kernel();
  1860. return retval;
  1861. }
  1862. /**
  1863. * tiocsti - fake input character
  1864. * @tty: tty to fake input into
  1865. * @p: pointer to character
  1866. *
  1867. * Fake input to a tty device. Does the necessary locking and
  1868. * input management.
  1869. *
  1870. * FIXME: does not honour flow control ??
  1871. *
  1872. * Locking:
  1873. * Called functions take tty_ldisc_lock
  1874. * current->signal->tty check is safe without locks
  1875. *
  1876. * FIXME: may race normal receive processing
  1877. */
  1878. static int tiocsti(struct tty_struct *tty, char __user *p)
  1879. {
  1880. char ch, mbz = 0;
  1881. struct tty_ldisc *ld;
  1882. if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
  1883. return -EPERM;
  1884. if (get_user(ch, p))
  1885. return -EFAULT;
  1886. ld = tty_ldisc_ref_wait(tty);
  1887. ld->ops->receive_buf(tty, &ch, &mbz, 1);
  1888. tty_ldisc_deref(ld);
  1889. return 0;
  1890. }
  1891. /**
  1892. * tiocgwinsz - implement window query ioctl
  1893. * @tty; tty
  1894. * @arg: user buffer for result
  1895. *
  1896. * Copies the kernel idea of the window size into the user buffer.
  1897. *
  1898. * Locking: tty->termios_mutex is taken to ensure the winsize data
  1899. * is consistent.
  1900. */
  1901. static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
  1902. {
  1903. int err;
  1904. mutex_lock(&tty->termios_mutex);
  1905. err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
  1906. mutex_unlock(&tty->termios_mutex);
  1907. return err ? -EFAULT: 0;
  1908. }
  1909. /**
  1910. * tty_do_resize - resize event
  1911. * @tty: tty being resized
  1912. * @real_tty: real tty (not the same as tty if using a pty/tty pair)
  1913. * @rows: rows (character)
  1914. * @cols: cols (character)
  1915. *
  1916. * Update the termios variables and send the neccessary signals to
  1917. * peform a terminal resize correctly
  1918. */
  1919. int tty_do_resize(struct tty_struct *tty, struct tty_struct *real_tty,
  1920. struct winsize *ws)
  1921. {
  1922. struct pid *pgrp, *rpgrp;
  1923. unsigned long flags;
  1924. /* For a PTY we need to lock the tty side */
  1925. mutex_lock(&real_tty->termios_mutex);
  1926. if (!memcmp(ws, &real_tty->winsize, sizeof(*ws)))
  1927. goto done;
  1928. /* Get the PID values and reference them so we can
  1929. avoid holding the tty ctrl lock while sending signals */
  1930. spin_lock_irqsave(&tty->ctrl_lock, flags);
  1931. pgrp = get_pid(tty->pgrp);
  1932. rpgrp = get_pid(real_tty->pgrp);
  1933. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  1934. if (pgrp)
  1935. kill_pgrp(pgrp, SIGWINCH, 1);
  1936. if (rpgrp != pgrp && rpgrp)
  1937. kill_pgrp(rpgrp, SIGWINCH, 1);
  1938. put_pid(pgrp);
  1939. put_pid(rpgrp);
  1940. tty->winsize = *ws;
  1941. real_tty->winsize = *ws;
  1942. done:
  1943. mutex_unlock(&real_tty->termios_mutex);
  1944. return 0;
  1945. }
  1946. /**
  1947. * tiocswinsz - implement window size set ioctl
  1948. * @tty; tty
  1949. * @arg: user buffer for result
  1950. *
  1951. * Copies the user idea of the window size to the kernel. Traditionally
  1952. * this is just advisory information but for the Linux console it
  1953. * actually has driver level meaning and triggers a VC resize.
  1954. *
  1955. * Locking:
  1956. * Driver dependant. The default do_resize method takes the
  1957. * tty termios mutex and ctrl_lock. The console takes its own lock
  1958. * then calls into the default method.
  1959. */
  1960. static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
  1961. struct winsize __user *arg)
  1962. {
  1963. struct winsize tmp_ws;
  1964. if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
  1965. return -EFAULT;
  1966. if (tty->ops->resize)
  1967. return tty->ops->resize(tty, real_tty, &tmp_ws);
  1968. else
  1969. return tty_do_resize(tty, real_tty, &tmp_ws);
  1970. }
  1971. /**
  1972. * tioccons - allow admin to move logical console
  1973. * @file: the file to become console
  1974. *
  1975. * Allow the adminstrator to move the redirected console device
  1976. *
  1977. * Locking: uses redirect_lock to guard the redirect information
  1978. */
  1979. static int tioccons(struct file *file)
  1980. {
  1981. if (!capable(CAP_SYS_ADMIN))
  1982. return -EPERM;
  1983. if (file->f_op->write == redirected_tty_write) {
  1984. struct file *f;
  1985. spin_lock(&redirect_lock);
  1986. f = redirect;
  1987. redirect = NULL;
  1988. spin_unlock(&redirect_lock);
  1989. if (f)
  1990. fput(f);
  1991. return 0;
  1992. }
  1993. spin_lock(&redirect_lock);
  1994. if (redirect) {
  1995. spin_unlock(&redirect_lock);
  1996. return -EBUSY;
  1997. }
  1998. get_file(file);
  1999. redirect = file;
  2000. spin_unlock(&redirect_lock);
  2001. return 0;
  2002. }
  2003. /**
  2004. * fionbio - non blocking ioctl
  2005. * @file: file to set blocking value
  2006. * @p: user parameter
  2007. *
  2008. * Historical tty interfaces had a blocking control ioctl before
  2009. * the generic functionality existed. This piece of history is preserved
  2010. * in the expected tty API of posix OS's.
  2011. *
  2012. * Locking: none, the open fle handle ensures it won't go away.
  2013. */
  2014. static int fionbio(struct file *file, int __user *p)
  2015. {
  2016. int nonblock;
  2017. if (get_user(nonblock, p))
  2018. return -EFAULT;
  2019. /* file->f_flags is still BKL protected in the fs layer - vomit */
  2020. lock_kernel();
  2021. if (nonblock)
  2022. file->f_flags |= O_NONBLOCK;
  2023. else
  2024. file->f_flags &= ~O_NONBLOCK;
  2025. unlock_kernel();
  2026. return 0;
  2027. }
  2028. /**
  2029. * tiocsctty - set controlling tty
  2030. * @tty: tty structure
  2031. * @arg: user argument
  2032. *
  2033. * This ioctl is used to manage job control. It permits a session
  2034. * leader to set this tty as the controlling tty for the session.
  2035. *
  2036. * Locking:
  2037. * Takes tty_mutex() to protect tty instance
  2038. * Takes tasklist_lock internally to walk sessions
  2039. * Takes ->siglock() when updating signal->tty
  2040. */
  2041. static int tiocsctty(struct tty_struct *tty, int arg)
  2042. {
  2043. int ret = 0;
  2044. if (current->signal->leader && (task_session(current) == tty->session))
  2045. return ret;
  2046. mutex_lock(&tty_mutex);
  2047. /*
  2048. * The process must be a session leader and
  2049. * not have a controlling tty already.
  2050. */
  2051. if (!current->signal->leader || current->signal->tty) {
  2052. ret = -EPERM;
  2053. goto unlock;
  2054. }
  2055. if (tty->session) {
  2056. /*
  2057. * This tty is already the controlling
  2058. * tty for another session group!
  2059. */
  2060. if (arg == 1 && capable(CAP_SYS_ADMIN)) {
  2061. /*
  2062. * Steal it away
  2063. */
  2064. read_lock(&tasklist_lock);
  2065. session_clear_tty(tty->session);
  2066. read_unlock(&tasklist_lock);
  2067. } else {
  2068. ret = -EPERM;
  2069. goto unlock;
  2070. }
  2071. }
  2072. proc_set_tty(current, tty);
  2073. unlock:
  2074. mutex_unlock(&tty_mutex);
  2075. return ret;
  2076. }
  2077. /**
  2078. * tty_get_pgrp - return a ref counted pgrp pid
  2079. * @tty: tty to read
  2080. *
  2081. * Returns a refcounted instance of the pid struct for the process
  2082. * group controlling the tty.
  2083. */
  2084. struct pid *tty_get_pgrp(struct tty_struct *tty)
  2085. {
  2086. unsigned long flags;
  2087. struct pid *pgrp;
  2088. spin_lock_irqsave(&tty->ctrl_lock, flags);
  2089. pgrp = get_pid(tty->pgrp);
  2090. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  2091. return pgrp;
  2092. }
  2093. EXPORT_SYMBOL_GPL(tty_get_pgrp);
  2094. /**
  2095. * tiocgpgrp - get process group
  2096. * @tty: tty passed by user
  2097. * @real_tty: tty side of the tty pased by the user if a pty else the tty
  2098. * @p: returned pid
  2099. *
  2100. * Obtain the process group of the tty. If there is no process group
  2101. * return an error.
  2102. *
  2103. * Locking: none. Reference to current->signal->tty is safe.
  2104. */
  2105. static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2106. {
  2107. struct pid *pid;
  2108. int ret;
  2109. /*
  2110. * (tty == real_tty) is a cheap way of
  2111. * testing if the tty is NOT a master pty.
  2112. */
  2113. if (tty == real_tty && current->signal->tty != real_tty)
  2114. return -ENOTTY;
  2115. pid = tty_get_pgrp(real_tty);
  2116. ret = put_user(pid_vnr(pid), p);
  2117. put_pid(pid);
  2118. return ret;
  2119. }
  2120. /**
  2121. * tiocspgrp - attempt to set process group
  2122. * @tty: tty passed by user
  2123. * @real_tty: tty side device matching tty passed by user
  2124. * @p: pid pointer
  2125. *
  2126. * Set the process group of the tty to the session passed. Only
  2127. * permitted where the tty session is our session.
  2128. *
  2129. * Locking: RCU, ctrl lock
  2130. */
  2131. static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2132. {
  2133. struct pid *pgrp;
  2134. pid_t pgrp_nr;
  2135. int retval = tty_check_change(real_tty);
  2136. unsigned long flags;
  2137. if (retval == -EIO)
  2138. return -ENOTTY;
  2139. if (retval)
  2140. return retval;
  2141. if (!current->signal->tty ||
  2142. (current->signal->tty != real_tty) ||
  2143. (real_tty->session != task_session(current)))
  2144. return -ENOTTY;
  2145. if (get_user(pgrp_nr, p))
  2146. return -EFAULT;
  2147. if (pgrp_nr < 0)
  2148. return -EINVAL;
  2149. rcu_read_lock();
  2150. pgrp = find_vpid(pgrp_nr);
  2151. retval = -ESRCH;
  2152. if (!pgrp)
  2153. goto out_unlock;
  2154. retval = -EPERM;
  2155. if (session_of_pgrp(pgrp) != task_session(current))
  2156. goto out_unlock;
  2157. retval = 0;
  2158. spin_lock_irqsave(&tty->ctrl_lock, flags);
  2159. put_pid(real_tty->pgrp);
  2160. real_tty->pgrp = get_pid(pgrp);
  2161. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  2162. out_unlock:
  2163. rcu_read_unlock();
  2164. return retval;
  2165. }
  2166. /**
  2167. * tiocgsid - get session id
  2168. * @tty: tty passed by user
  2169. * @real_tty: tty side of the tty pased by the user if a pty else the tty
  2170. * @p: pointer to returned session id
  2171. *
  2172. * Obtain the session id of the tty. If there is no session
  2173. * return an error.
  2174. *
  2175. * Locking: none. Reference to current->signal->tty is safe.
  2176. */
  2177. static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  2178. {
  2179. /*
  2180. * (tty == real_tty) is a cheap way of
  2181. * testing if the tty is NOT a master pty.
  2182. */
  2183. if (tty == real_tty && current->signal->tty != real_tty)
  2184. return -ENOTTY;
  2185. if (!real_tty->session)
  2186. return -ENOTTY;
  2187. return put_user(pid_vnr(real_tty->session), p);
  2188. }
  2189. /**
  2190. * tiocsetd - set line discipline
  2191. * @tty: tty device
  2192. * @p: pointer to user data
  2193. *
  2194. * Set the line discipline according to user request.
  2195. *
  2196. * Locking: see tty_set_ldisc, this function is just a helper
  2197. */
  2198. static int tiocsetd(struct tty_struct *tty, int __user *p)
  2199. {
  2200. int ldisc;
  2201. int ret;
  2202. if (get_user(ldisc, p))
  2203. return -EFAULT;
  2204. lock_kernel();
  2205. ret = tty_set_ldisc(tty, ldisc);
  2206. unlock_kernel();
  2207. return ret;
  2208. }
  2209. /**
  2210. * send_break - performed time break
  2211. * @tty: device to break on
  2212. * @duration: timeout in mS
  2213. *
  2214. * Perform a timed break on hardware that lacks its own driver level
  2215. * timed break functionality.
  2216. *
  2217. * Locking:
  2218. * atomic_write_lock serializes
  2219. *
  2220. */
  2221. static int send_break(struct tty_struct *tty, unsigned int duration)
  2222. {
  2223. int retval;
  2224. if (tty->ops->break_ctl == NULL)
  2225. return 0;
  2226. if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
  2227. retval = tty->ops->break_ctl(tty, duration);
  2228. else {
  2229. /* Do the work ourselves */
  2230. if (tty_write_lock(tty, 0) < 0)
  2231. return -EINTR;
  2232. retval = tty->ops->break_ctl(tty, -1);
  2233. if (retval)
  2234. goto out;
  2235. if (!signal_pending(current))
  2236. msleep_interruptible(duration);
  2237. retval = tty->ops->break_ctl(tty, 0);
  2238. out:
  2239. tty_write_unlock(tty);
  2240. if (signal_pending(current))
  2241. retval = -EINTR;
  2242. }
  2243. return retval;
  2244. }
  2245. /**
  2246. * tty_tiocmget - get modem status
  2247. * @tty: tty device
  2248. * @file: user file pointer
  2249. * @p: pointer to result
  2250. *
  2251. * Obtain the modem status bits from the tty driver if the feature
  2252. * is supported. Return -EINVAL if it is not available.
  2253. *
  2254. * Locking: none (up to the driver)
  2255. */
  2256. static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
  2257. {
  2258. int retval = -EINVAL;
  2259. if (tty->ops->tiocmget) {
  2260. retval = tty->ops->tiocmget(tty, file);
  2261. if (retval >= 0)
  2262. retval = put_user(retval, p);
  2263. }
  2264. return retval;
  2265. }
  2266. /**
  2267. * tty_tiocmset - set modem status
  2268. * @tty: tty device
  2269. * @file: user file pointer
  2270. * @cmd: command - clear bits, set bits or set all
  2271. * @p: pointer to desired bits
  2272. *
  2273. * Set the modem status bits from the tty driver if the feature
  2274. * is supported. Return -EINVAL if it is not available.
  2275. *
  2276. * Locking: none (up to the driver)
  2277. */
  2278. static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
  2279. unsigned __user *p)
  2280. {
  2281. int retval;
  2282. unsigned int set, clear, val;
  2283. if (tty->ops->tiocmset == NULL)
  2284. return -EINVAL;
  2285. retval = get_user(val, p);
  2286. if (retval)
  2287. return retval;
  2288. set = clear = 0;
  2289. switch (cmd) {
  2290. case TIOCMBIS:
  2291. set = val;
  2292. break;
  2293. case TIOCMBIC:
  2294. clear = val;
  2295. break;
  2296. case TIOCMSET:
  2297. set = val;
  2298. clear = ~val;
  2299. break;
  2300. }
  2301. set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
  2302. clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
  2303. return tty->ops->tiocmset(tty, file, set, clear);
  2304. }
  2305. /*
  2306. * Split this up, as gcc can choke on it otherwise..
  2307. */
  2308. long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2309. {
  2310. struct tty_struct *tty, *real_tty;
  2311. void __user *p = (void __user *)arg;
  2312. int retval;
  2313. struct tty_ldisc *ld;
  2314. struct inode *inode = file->f_dentry->d_inode;
  2315. tty = (struct tty_struct *)file->private_data;
  2316. if (tty_paranoia_check(tty, inode, "tty_ioctl"))
  2317. return -EINVAL;
  2318. real_tty = tty;
  2319. if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
  2320. tty->driver->subtype == PTY_TYPE_MASTER)
  2321. real_tty = tty->link;
  2322. /*
  2323. * Factor out some common prep work
  2324. */
  2325. switch (cmd) {
  2326. case TIOCSETD:
  2327. case TIOCSBRK:
  2328. case TIOCCBRK:
  2329. case TCSBRK:
  2330. case TCSBRKP:
  2331. retval = tty_check_change(tty);
  2332. if (retval)
  2333. return retval;
  2334. if (cmd != TIOCCBRK) {
  2335. tty_wait_until_sent(tty, 0);
  2336. if (signal_pending(current))
  2337. return -EINTR;
  2338. }
  2339. break;
  2340. }
  2341. /*
  2342. * Now do the stuff.
  2343. */
  2344. switch (cmd) {
  2345. case TIOCSTI:
  2346. return tiocsti(tty, p);
  2347. case TIOCGWINSZ:
  2348. return tiocgwinsz(real_tty, p);
  2349. case TIOCSWINSZ:
  2350. return tiocswinsz(tty, real_tty, p);
  2351. case TIOCCONS:
  2352. return real_tty != tty ? -EINVAL : tioccons(file);
  2353. case FIONBIO:
  2354. return fionbio(file, p);
  2355. case TIOCEXCL:
  2356. set_bit(TTY_EXCLUSIVE, &tty->flags);
  2357. return 0;
  2358. case TIOCNXCL:
  2359. clear_bit(TTY_EXCLUSIVE, &tty->flags);
  2360. return 0;
  2361. case TIOCNOTTY:
  2362. if (current->signal->tty != tty)
  2363. return -ENOTTY;
  2364. no_tty();
  2365. return 0;
  2366. case TIOCSCTTY:
  2367. return tiocsctty(tty, arg);
  2368. case TIOCGPGRP:
  2369. return tiocgpgrp(tty, real_tty, p);
  2370. case TIOCSPGRP:
  2371. return tiocspgrp(tty, real_tty, p);
  2372. case TIOCGSID:
  2373. return tiocgsid(tty, real_tty, p);
  2374. case TIOCGETD:
  2375. return put_user(tty->ldisc.ops->num, (int __user *)p);
  2376. case TIOCSETD:
  2377. return tiocsetd(tty, p);
  2378. /*
  2379. * Break handling
  2380. */
  2381. case TIOCSBRK: /* Turn break on, unconditionally */
  2382. if (tty->ops->break_ctl)
  2383. return tty->ops->break_ctl(tty, -1);
  2384. return 0;
  2385. case TIOCCBRK: /* Turn break off, unconditionally */
  2386. if (tty->ops->break_ctl)
  2387. return tty->ops->break_ctl(tty, 0);
  2388. return 0;
  2389. case TCSBRK: /* SVID version: non-zero arg --> no break */
  2390. /* non-zero arg means wait for all output data
  2391. * to be sent (performed above) but don't send break.
  2392. * This is used by the tcdrain() termios function.
  2393. */
  2394. if (!arg)
  2395. return send_break(tty, 250);
  2396. return 0;
  2397. case TCSBRKP: /* support for POSIX tcsendbreak() */
  2398. return send_break(tty, arg ? arg*100 : 250);
  2399. case TIOCMGET:
  2400. return tty_tiocmget(tty, file, p);
  2401. case TIOCMSET:
  2402. case TIOCMBIC:
  2403. case TIOCMBIS:
  2404. return tty_tiocmset(tty, file, cmd, p);
  2405. case TCFLSH:
  2406. switch (arg) {
  2407. case TCIFLUSH:
  2408. case TCIOFLUSH:
  2409. /* flush tty buffer and allow ldisc to process ioctl */
  2410. tty_buffer_flush(tty);
  2411. break;
  2412. }
  2413. break;
  2414. }
  2415. if (tty->ops->ioctl) {
  2416. retval = (tty->ops->ioctl)(tty, file, cmd, arg);
  2417. if (retval != -ENOIOCTLCMD)
  2418. return retval;
  2419. }
  2420. ld = tty_ldisc_ref_wait(tty);
  2421. retval = -EINVAL;
  2422. if (ld->ops->ioctl) {
  2423. retval = ld->ops->ioctl(tty, file, cmd, arg);
  2424. if (retval == -ENOIOCTLCMD)
  2425. retval = -EINVAL;
  2426. }
  2427. tty_ldisc_deref(ld);
  2428. return retval;
  2429. }
  2430. #ifdef CONFIG_COMPAT
  2431. static long tty_compat_ioctl(struct file *file, unsigned int cmd,
  2432. unsigned long arg)
  2433. {
  2434. struct inode *inode = file->f_dentry->d_inode;
  2435. struct tty_struct *tty = file->private_data;
  2436. struct tty_ldisc *ld;
  2437. int retval = -ENOIOCTLCMD;
  2438. if (tty_paranoia_check(tty, inode, "tty_ioctl"))
  2439. return -EINVAL;
  2440. if (tty->ops->compat_ioctl) {
  2441. retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
  2442. if (retval != -ENOIOCTLCMD)
  2443. return retval;
  2444. }
  2445. ld = tty_ldisc_ref_wait(tty);
  2446. if (ld->ops->compat_ioctl)
  2447. retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
  2448. tty_ldisc_deref(ld);
  2449. return retval;
  2450. }
  2451. #endif
  2452. /*
  2453. * This implements the "Secure Attention Key" --- the idea is to
  2454. * prevent trojan horses by killing all processes associated with this
  2455. * tty when the user hits the "Secure Attention Key". Required for
  2456. * super-paranoid applications --- see the Orange Book for more details.
  2457. *
  2458. * This code could be nicer; ideally it should send a HUP, wait a few
  2459. * seconds, then send a INT, and then a KILL signal. But you then
  2460. * have to coordinate with the init process, since all processes associated
  2461. * with the current tty must be dead before the new getty is allowed
  2462. * to spawn.
  2463. *
  2464. * Now, if it would be correct ;-/ The current code has a nasty hole -
  2465. * it doesn't catch files in flight. We may send the descriptor to ourselves
  2466. * via AF_UNIX socket, close it and later fetch from socket. FIXME.
  2467. *
  2468. * Nasty bug: do_SAK is being called in interrupt context. This can
  2469. * deadlock. We punt it up to process context. AKPM - 16Mar2001
  2470. */
  2471. void __do_SAK(struct tty_struct *tty)
  2472. {
  2473. #ifdef TTY_SOFT_SAK
  2474. tty_hangup(tty);
  2475. #else
  2476. struct task_struct *g, *p;
  2477. struct pid *session;
  2478. int i;
  2479. struct file *filp;
  2480. struct fdtable *fdt;
  2481. if (!tty)
  2482. return;
  2483. session = tty->session;
  2484. tty_ldisc_flush(tty);
  2485. tty_driver_flush_buffer(tty);
  2486. read_lock(&tasklist_lock);
  2487. /* Kill the entire session */
  2488. do_each_pid_task(session, PIDTYPE_SID, p) {
  2489. printk(KERN_NOTICE "SAK: killed process %d"
  2490. " (%s): task_session_nr(p)==tty->session\n",
  2491. task_pid_nr(p), p->comm);
  2492. send_sig(SIGKILL, p, 1);
  2493. } while_each_pid_task(session, PIDTYPE_SID, p);
  2494. /* Now kill any processes that happen to have the
  2495. * tty open.
  2496. */
  2497. do_each_thread(g, p) {
  2498. if (p->signal->tty == tty) {
  2499. printk(KERN_NOTICE "SAK: killed process %d"
  2500. " (%s): task_session_nr(p)==tty->session\n",
  2501. task_pid_nr(p), p->comm);
  2502. send_sig(SIGKILL, p, 1);
  2503. continue;
  2504. }
  2505. task_lock(p);
  2506. if (p->files) {
  2507. /*
  2508. * We don't take a ref to the file, so we must
  2509. * hold ->file_lock instead.
  2510. */
  2511. spin_lock(&p->files->file_lock);
  2512. fdt = files_fdtable(p->files);
  2513. for (i = 0; i < fdt->max_fds; i++) {
  2514. filp = fcheck_files(p->files, i);
  2515. if (!filp)
  2516. continue;
  2517. if (filp->f_op->read == tty_read &&
  2518. filp->private_data == tty) {
  2519. printk(KERN_NOTICE "SAK: killed process %d"
  2520. " (%s): fd#%d opened to the tty\n",
  2521. task_pid_nr(p), p->comm, i);
  2522. force_sig(SIGKILL, p);
  2523. break;
  2524. }
  2525. }
  2526. spin_unlock(&p->files->file_lock);
  2527. }
  2528. task_unlock(p);
  2529. } while_each_thread(g, p);
  2530. read_unlock(&tasklist_lock);
  2531. #endif
  2532. }
  2533. static void do_SAK_work(struct work_struct *work)
  2534. {
  2535. struct tty_struct *tty =
  2536. container_of(work, struct tty_struct, SAK_work);
  2537. __do_SAK(tty);
  2538. }
  2539. /*
  2540. * The tq handling here is a little racy - tty->SAK_work may already be queued.
  2541. * Fortunately we don't need to worry, because if ->SAK_work is already queued,
  2542. * the values which we write to it will be identical to the values which it
  2543. * already has. --akpm
  2544. */
  2545. void do_SAK(struct tty_struct *tty)
  2546. {
  2547. if (!tty)
  2548. return;
  2549. schedule_work(&tty->SAK_work);
  2550. }
  2551. EXPORT_SYMBOL(do_SAK);
  2552. /**
  2553. * initialize_tty_struct
  2554. * @tty: tty to initialize
  2555. *
  2556. * This subroutine initializes a tty structure that has been newly
  2557. * allocated.
  2558. *
  2559. * Locking: none - tty in question must not be exposed at this point
  2560. */
  2561. static void initialize_tty_struct(struct tty_struct *tty)
  2562. {
  2563. memset(tty, 0, sizeof(struct tty_struct));
  2564. kref_init(&tty->kref);
  2565. tty->magic = TTY_MAGIC;
  2566. tty_ldisc_init(tty);
  2567. tty->session = NULL;
  2568. tty->pgrp = NULL;
  2569. tty->overrun_time = jiffies;
  2570. tty->buf.head = tty->buf.tail = NULL;
  2571. tty_buffer_init(tty);
  2572. mutex_init(&tty->termios_mutex);
  2573. init_waitqueue_head(&tty->write_wait);
  2574. init_waitqueue_head(&tty->read_wait);
  2575. INIT_WORK(&tty->hangup_work, do_tty_hangup);
  2576. mutex_init(&tty->atomic_read_lock);
  2577. mutex_init(&tty->atomic_write_lock);
  2578. spin_lock_init(&tty->read_lock);
  2579. spin_lock_init(&tty->ctrl_lock);
  2580. INIT_LIST_HEAD(&tty->tty_files);
  2581. INIT_WORK(&tty->SAK_work, do_SAK_work);
  2582. }
  2583. /**
  2584. * tty_put_char - write one character to a tty
  2585. * @tty: tty
  2586. * @ch: character
  2587. *
  2588. * Write one byte to the tty using the provided put_char method
  2589. * if present. Returns the number of characters successfully output.
  2590. *
  2591. * Note: the specific put_char operation in the driver layer may go
  2592. * away soon. Don't call it directly, use this method
  2593. */
  2594. int tty_put_char(struct tty_struct *tty, unsigned char ch)
  2595. {
  2596. if (tty->ops->put_char)
  2597. return tty->ops->put_char(tty, ch);
  2598. return tty->ops->write(tty, &ch, 1);
  2599. }
  2600. EXPORT_SYMBOL_GPL(tty_put_char);
  2601. struct class *tty_class;
  2602. /**
  2603. * tty_register_device - register a tty device
  2604. * @driver: the tty driver that describes the tty device
  2605. * @index: the index in the tty driver for this tty device
  2606. * @device: a struct device that is associated with this tty device.
  2607. * This field is optional, if there is no known struct device
  2608. * for this tty device it can be set to NULL safely.
  2609. *
  2610. * Returns a pointer to the struct device for this tty device
  2611. * (or ERR_PTR(-EFOO) on error).
  2612. *
  2613. * This call is required to be made to register an individual tty device
  2614. * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
  2615. * that bit is not set, this function should not be called by a tty
  2616. * driver.
  2617. *
  2618. * Locking: ??
  2619. */
  2620. struct device *tty_register_device(struct tty_driver *driver, unsigned index,
  2621. struct device *device)
  2622. {
  2623. char name[64];
  2624. dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
  2625. if (index >= driver->num) {
  2626. printk(KERN_ERR "Attempt to register invalid tty line number "
  2627. " (%d).\n", index);
  2628. return ERR_PTR(-EINVAL);
  2629. }
  2630. if (driver->type == TTY_DRIVER_TYPE_PTY)
  2631. pty_line_name(driver, index, name);
  2632. else
  2633. tty_line_name(driver, index, name);
  2634. return device_create_drvdata(tty_class, device, dev, NULL, name);
  2635. }
  2636. EXPORT_SYMBOL(tty_register_device);
  2637. /**
  2638. * tty_unregister_device - unregister a tty device
  2639. * @driver: the tty driver that describes the tty device
  2640. * @index: the index in the tty driver for this tty device
  2641. *
  2642. * If a tty device is registered with a call to tty_register_device() then
  2643. * this function must be called when the tty device is gone.
  2644. *
  2645. * Locking: ??
  2646. */
  2647. void tty_unregister_device(struct tty_driver *driver, unsigned index)
  2648. {
  2649. device_destroy(tty_class,
  2650. MKDEV(driver->major, driver->minor_start) + index);
  2651. }
  2652. EXPORT_SYMBOL(tty_unregister_device);
  2653. struct tty_driver *alloc_tty_driver(int lines)
  2654. {
  2655. struct tty_driver *driver;
  2656. driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
  2657. if (driver) {
  2658. kref_init(&driver->kref);
  2659. driver->magic = TTY_DRIVER_MAGIC;
  2660. driver->num = lines;
  2661. /* later we'll move allocation of tables here */
  2662. }
  2663. return driver;
  2664. }
  2665. EXPORT_SYMBOL(alloc_tty_driver);
  2666. static void destruct_tty_driver(struct kref *kref)
  2667. {
  2668. struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
  2669. int i;
  2670. struct ktermios *tp;
  2671. void *p;
  2672. if (driver->flags & TTY_DRIVER_INSTALLED) {
  2673. /*
  2674. * Free the termios and termios_locked structures because
  2675. * we don't want to get memory leaks when modular tty
  2676. * drivers are removed from the kernel.
  2677. */
  2678. for (i = 0; i < driver->num; i++) {
  2679. tp = driver->termios[i];
  2680. if (tp) {
  2681. driver->termios[i] = NULL;
  2682. kfree(tp);
  2683. }
  2684. tp = driver->termios_locked[i];
  2685. if (tp) {
  2686. driver->termios_locked[i] = NULL;
  2687. kfree(tp);
  2688. }
  2689. if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
  2690. tty_unregister_device(driver, i);
  2691. }
  2692. p = driver->ttys;
  2693. proc_tty_unregister_driver(driver);
  2694. driver->ttys = NULL;
  2695. driver->termios = driver->termios_locked = NULL;
  2696. kfree(p);
  2697. cdev_del(&driver->cdev);
  2698. }
  2699. kfree(driver);
  2700. }
  2701. void tty_driver_kref_put(struct tty_driver *driver)
  2702. {
  2703. kref_put(&driver->kref, destruct_tty_driver);
  2704. }
  2705. EXPORT_SYMBOL(tty_driver_kref_put);
  2706. void tty_set_operations(struct tty_driver *driver,
  2707. const struct tty_operations *op)
  2708. {
  2709. driver->ops = op;
  2710. };
  2711. EXPORT_SYMBOL(tty_set_operations);
  2712. void put_tty_driver(struct tty_driver *d)
  2713. {
  2714. tty_driver_kref_put(d);
  2715. }
  2716. EXPORT_SYMBOL(put_tty_driver);
  2717. /*
  2718. * Called by a tty driver to register itself.
  2719. */
  2720. int tty_register_driver(struct tty_driver *driver)
  2721. {
  2722. int error;
  2723. int i;
  2724. dev_t dev;
  2725. void **p = NULL;
  2726. if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
  2727. p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
  2728. if (!p)
  2729. return -ENOMEM;
  2730. }
  2731. if (!driver->major) {
  2732. error = alloc_chrdev_region(&dev, driver->minor_start,
  2733. driver->num, driver->name);
  2734. if (!error) {
  2735. driver->major = MAJOR(dev);
  2736. driver->minor_start = MINOR(dev);
  2737. }
  2738. } else {
  2739. dev = MKDEV(driver->major, driver->minor_start);
  2740. error = register_chrdev_region(dev, driver->num, driver->name);
  2741. }
  2742. if (error < 0) {
  2743. kfree(p);
  2744. return error;
  2745. }
  2746. if (p) {
  2747. driver->ttys = (struct tty_struct **)p;
  2748. driver->termios = (struct ktermios **)(p + driver->num);
  2749. driver->termios_locked = (struct ktermios **)
  2750. (p + driver->num * 2);
  2751. } else {
  2752. driver->ttys = NULL;
  2753. driver->termios = NULL;
  2754. driver->termios_locked = NULL;
  2755. }
  2756. cdev_init(&driver->cdev, &tty_fops);
  2757. driver->cdev.owner = driver->owner;
  2758. error = cdev_add(&driver->cdev, dev, driver->num);
  2759. if (error) {
  2760. unregister_chrdev_region(dev, driver->num);
  2761. driver->ttys = NULL;
  2762. driver->termios = driver->termios_locked = NULL;
  2763. kfree(p);
  2764. return error;
  2765. }
  2766. mutex_lock(&tty_mutex);
  2767. list_add(&driver->tty_drivers, &tty_drivers);
  2768. mutex_unlock(&tty_mutex);
  2769. if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
  2770. for (i = 0; i < driver->num; i++)
  2771. tty_register_device(driver, i, NULL);
  2772. }
  2773. proc_tty_register_driver(driver);
  2774. driver->flags |= TTY_DRIVER_INSTALLED;
  2775. return 0;
  2776. }
  2777. EXPORT_SYMBOL(tty_register_driver);
  2778. /*
  2779. * Called by a tty driver to unregister itself.
  2780. */
  2781. int tty_unregister_driver(struct tty_driver *driver)
  2782. {
  2783. #if 0
  2784. /* FIXME */
  2785. if (driver->refcount)
  2786. return -EBUSY;
  2787. #endif
  2788. unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
  2789. driver->num);
  2790. mutex_lock(&tty_mutex);
  2791. list_del(&driver->tty_drivers);
  2792. mutex_unlock(&tty_mutex);
  2793. return 0;
  2794. }
  2795. EXPORT_SYMBOL(tty_unregister_driver);
  2796. dev_t tty_devnum(struct tty_struct *tty)
  2797. {
  2798. return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
  2799. }
  2800. EXPORT_SYMBOL(tty_devnum);
  2801. void proc_clear_tty(struct task_struct *p)
  2802. {
  2803. struct tty_struct *tty;
  2804. spin_lock_irq(&p->sighand->siglock);
  2805. tty = p->signal->tty;
  2806. p->signal->tty = NULL;
  2807. spin_unlock_irq(&p->sighand->siglock);
  2808. tty_kref_put(tty);
  2809. }
  2810. /* Called under the sighand lock */
  2811. static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
  2812. {
  2813. if (tty) {
  2814. unsigned long flags;
  2815. /* We should not have a session or pgrp to put here but.... */
  2816. spin_lock_irqsave(&tty->ctrl_lock, flags);
  2817. put_pid(tty->session);
  2818. put_pid(tty->pgrp);
  2819. tty->pgrp = get_pid(task_pgrp(tsk));
  2820. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  2821. tty->session = get_pid(task_session(tsk));
  2822. if (tsk->signal->tty) {
  2823. printk(KERN_DEBUG "tty not NULL!!\n");
  2824. tty_kref_put(tsk->signal->tty);
  2825. }
  2826. }
  2827. put_pid(tsk->signal->tty_old_pgrp);
  2828. tsk->signal->tty = tty_kref_get(tty);
  2829. tsk->signal->tty_old_pgrp = NULL;
  2830. }
  2831. static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
  2832. {
  2833. spin_lock_irq(&tsk->sighand->siglock);
  2834. __proc_set_tty(tsk, tty);
  2835. spin_unlock_irq(&tsk->sighand->siglock);
  2836. }
  2837. struct tty_struct *get_current_tty(void)
  2838. {
  2839. struct tty_struct *tty;
  2840. unsigned long flags;
  2841. spin_lock_irqsave(&current->sighand->siglock, flags);
  2842. tty = tty_kref_get(current->signal->tty);
  2843. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  2844. return tty;
  2845. }
  2846. EXPORT_SYMBOL_GPL(get_current_tty);
  2847. void tty_default_fops(struct file_operations *fops)
  2848. {
  2849. *fops = tty_fops;
  2850. }
  2851. /*
  2852. * Initialize the console device. This is called *early*, so
  2853. * we can't necessarily depend on lots of kernel help here.
  2854. * Just do some early initializations, and do the complex setup
  2855. * later.
  2856. */
  2857. void __init console_init(void)
  2858. {
  2859. initcall_t *call;
  2860. /* Setup the default TTY line discipline. */
  2861. tty_ldisc_begin();
  2862. /*
  2863. * set up the console device so that later boot sequences can
  2864. * inform about problems etc..
  2865. */
  2866. call = __con_initcall_start;
  2867. while (call < __con_initcall_end) {
  2868. (*call)();
  2869. call++;
  2870. }
  2871. }
  2872. static int __init tty_class_init(void)
  2873. {
  2874. tty_class = class_create(THIS_MODULE, "tty");
  2875. if (IS_ERR(tty_class))
  2876. return PTR_ERR(tty_class);
  2877. return 0;
  2878. }
  2879. postcore_initcall(tty_class_init);
  2880. /* 3/2004 jmc: why do these devices exist? */
  2881. static struct cdev tty_cdev, console_cdev;
  2882. /*
  2883. * Ok, now we can initialize the rest of the tty devices and can count
  2884. * on memory allocations, interrupts etc..
  2885. */
  2886. static int __init tty_init(void)
  2887. {
  2888. cdev_init(&tty_cdev, &tty_fops);
  2889. if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
  2890. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
  2891. panic("Couldn't register /dev/tty driver\n");
  2892. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
  2893. "tty");
  2894. cdev_init(&console_cdev, &console_fops);
  2895. if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
  2896. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
  2897. panic("Couldn't register /dev/console driver\n");
  2898. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
  2899. "console");
  2900. #ifdef CONFIG_VT
  2901. vty_init(&console_fops);
  2902. #endif
  2903. return 0;
  2904. }
  2905. module_init(tty_init);