tty_io.c 83 KB

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