tty_io.c 78 KB

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