tty_io.c 81 KB

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