tty_io.c 78 KB

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