tty_io.c 79 KB

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