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