tty_io.c 85 KB

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