tty_io.c 78 KB

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