tty_io.c 87 KB

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