tty_io.c 77 KB

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