tty_io.c 87 KB

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