tty_io.c 90 KB

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