line.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/irqreturn.h"
  6. #include "linux/kd.h"
  7. #include "linux/sched.h"
  8. #include "linux/slab.h"
  9. #include "chan_kern.h"
  10. #include "irq_kern.h"
  11. #include "irq_user.h"
  12. #include "kern_util.h"
  13. #include "os.h"
  14. #define LINE_BUFSIZE 4096
  15. static irqreturn_t line_interrupt(int irq, void *data)
  16. {
  17. struct chan *chan = data;
  18. struct line *line = chan->line;
  19. if (line)
  20. chan_interrupt(&line->chan_list, &line->task, line->tty, irq);
  21. return IRQ_HANDLED;
  22. }
  23. static void line_timer_cb(struct work_struct *work)
  24. {
  25. struct line *line = container_of(work, struct line, task.work);
  26. if (!line->throttled)
  27. chan_interrupt(&line->chan_list, &line->task, line->tty,
  28. line->driver->read_irq);
  29. }
  30. /*
  31. * Returns the free space inside the ring buffer of this line.
  32. *
  33. * Should be called while holding line->lock (this does not modify data).
  34. */
  35. static int write_room(struct line *line)
  36. {
  37. int n;
  38. if (line->buffer == NULL)
  39. return LINE_BUFSIZE - 1;
  40. /* This is for the case where the buffer is wrapped! */
  41. n = line->head - line->tail;
  42. if (n <= 0)
  43. n += LINE_BUFSIZE; /* The other case */
  44. return n - 1;
  45. }
  46. int line_write_room(struct tty_struct *tty)
  47. {
  48. struct line *line = tty->driver_data;
  49. unsigned long flags;
  50. int room;
  51. spin_lock_irqsave(&line->lock, flags);
  52. room = write_room(line);
  53. spin_unlock_irqrestore(&line->lock, flags);
  54. return room;
  55. }
  56. int line_chars_in_buffer(struct tty_struct *tty)
  57. {
  58. struct line *line = tty->driver_data;
  59. unsigned long flags;
  60. int ret;
  61. spin_lock_irqsave(&line->lock, flags);
  62. /* write_room subtracts 1 for the needed NULL, so we readd it.*/
  63. ret = LINE_BUFSIZE - (write_room(line) + 1);
  64. spin_unlock_irqrestore(&line->lock, flags);
  65. return ret;
  66. }
  67. /*
  68. * This copies the content of buf into the circular buffer associated with
  69. * this line.
  70. * The return value is the number of characters actually copied, i.e. the ones
  71. * for which there was space: this function is not supposed to ever flush out
  72. * the circular buffer.
  73. *
  74. * Must be called while holding line->lock!
  75. */
  76. static int buffer_data(struct line *line, const char *buf, int len)
  77. {
  78. int end, room;
  79. if (line->buffer == NULL) {
  80. line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
  81. if (line->buffer == NULL) {
  82. printk(KERN_ERR "buffer_data - atomic allocation "
  83. "failed\n");
  84. return 0;
  85. }
  86. line->head = line->buffer;
  87. line->tail = line->buffer;
  88. }
  89. room = write_room(line);
  90. len = (len > room) ? room : len;
  91. end = line->buffer + LINE_BUFSIZE - line->tail;
  92. if (len < end) {
  93. memcpy(line->tail, buf, len);
  94. line->tail += len;
  95. }
  96. else {
  97. /* The circular buffer is wrapping */
  98. memcpy(line->tail, buf, end);
  99. buf += end;
  100. memcpy(line->buffer, buf, len - end);
  101. line->tail = line->buffer + len - end;
  102. }
  103. return len;
  104. }
  105. /*
  106. * Flushes the ring buffer to the output channels. That is, write_chan is
  107. * called, passing it line->head as buffer, and an appropriate count.
  108. *
  109. * On exit, returns 1 when the buffer is empty,
  110. * 0 when the buffer is not empty on exit,
  111. * and -errno when an error occurred.
  112. *
  113. * Must be called while holding line->lock!*/
  114. static int flush_buffer(struct line *line)
  115. {
  116. int n, count;
  117. if ((line->buffer == NULL) || (line->head == line->tail))
  118. return 1;
  119. if (line->tail < line->head) {
  120. /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
  121. count = line->buffer + LINE_BUFSIZE - line->head;
  122. n = write_chan(&line->chan_list, line->head, count,
  123. line->driver->write_irq);
  124. if (n < 0)
  125. return n;
  126. if (n == count) {
  127. /*
  128. * We have flushed from ->head to buffer end, now we
  129. * must flush only from the beginning to ->tail.
  130. */
  131. line->head = line->buffer;
  132. } else {
  133. line->head += n;
  134. return 0;
  135. }
  136. }
  137. count = line->tail - line->head;
  138. n = write_chan(&line->chan_list, line->head, count,
  139. line->driver->write_irq);
  140. if (n < 0)
  141. return n;
  142. line->head += n;
  143. return line->head == line->tail;
  144. }
  145. void line_flush_buffer(struct tty_struct *tty)
  146. {
  147. struct line *line = tty->driver_data;
  148. unsigned long flags;
  149. int err;
  150. spin_lock_irqsave(&line->lock, flags);
  151. err = flush_buffer(line);
  152. spin_unlock_irqrestore(&line->lock, flags);
  153. }
  154. /*
  155. * We map both ->flush_chars and ->put_char (which go in pair) onto
  156. * ->flush_buffer and ->write. Hope it's not that bad.
  157. */
  158. void line_flush_chars(struct tty_struct *tty)
  159. {
  160. line_flush_buffer(tty);
  161. }
  162. int line_put_char(struct tty_struct *tty, unsigned char ch)
  163. {
  164. return line_write(tty, &ch, sizeof(ch));
  165. }
  166. int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
  167. {
  168. struct line *line = tty->driver_data;
  169. unsigned long flags;
  170. int n, ret = 0;
  171. spin_lock_irqsave(&line->lock, flags);
  172. if (line->head != line->tail)
  173. ret = buffer_data(line, buf, len);
  174. else {
  175. n = write_chan(&line->chan_list, buf, len,
  176. line->driver->write_irq);
  177. if (n < 0) {
  178. ret = n;
  179. goto out_up;
  180. }
  181. len -= n;
  182. ret += n;
  183. if (len > 0)
  184. ret += buffer_data(line, buf + n, len);
  185. }
  186. out_up:
  187. spin_unlock_irqrestore(&line->lock, flags);
  188. return ret;
  189. }
  190. void line_set_termios(struct tty_struct *tty, struct ktermios * old)
  191. {
  192. /* nothing */
  193. }
  194. static const struct {
  195. int cmd;
  196. char *level;
  197. char *name;
  198. } tty_ioctls[] = {
  199. /* don't print these, they flood the log ... */
  200. { TCGETS, NULL, "TCGETS" },
  201. { TCSETS, NULL, "TCSETS" },
  202. { TCSETSW, NULL, "TCSETSW" },
  203. { TCFLSH, NULL, "TCFLSH" },
  204. { TCSBRK, NULL, "TCSBRK" },
  205. /* general tty stuff */
  206. { TCSETSF, KERN_DEBUG, "TCSETSF" },
  207. { TCGETA, KERN_DEBUG, "TCGETA" },
  208. { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
  209. { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
  210. { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
  211. /* linux-specific ones */
  212. { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
  213. { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
  214. { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
  215. { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
  216. };
  217. int line_ioctl(struct tty_struct *tty, unsigned int cmd,
  218. unsigned long arg)
  219. {
  220. int ret;
  221. int i;
  222. ret = 0;
  223. switch(cmd) {
  224. #ifdef TIOCGETP
  225. case TIOCGETP:
  226. case TIOCSETP:
  227. case TIOCSETN:
  228. #endif
  229. #ifdef TIOCGETC
  230. case TIOCGETC:
  231. case TIOCSETC:
  232. #endif
  233. #ifdef TIOCGLTC
  234. case TIOCGLTC:
  235. case TIOCSLTC:
  236. #endif
  237. /* Note: these are out of date as we now have TCGETS2 etc but this
  238. whole lot should probably go away */
  239. case TCGETS:
  240. case TCSETSF:
  241. case TCSETSW:
  242. case TCSETS:
  243. case TCGETA:
  244. case TCSETAF:
  245. case TCSETAW:
  246. case TCSETA:
  247. case TCXONC:
  248. case TCFLSH:
  249. case TIOCOUTQ:
  250. case TIOCINQ:
  251. case TIOCGLCKTRMIOS:
  252. case TIOCSLCKTRMIOS:
  253. case TIOCPKT:
  254. case TIOCGSOFTCAR:
  255. case TIOCSSOFTCAR:
  256. return -ENOIOCTLCMD;
  257. #if 0
  258. case TCwhatever:
  259. /* do something */
  260. break;
  261. #endif
  262. default:
  263. for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
  264. if (cmd == tty_ioctls[i].cmd)
  265. break;
  266. if (i == ARRAY_SIZE(tty_ioctls)) {
  267. printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
  268. __func__, tty->name, cmd);
  269. }
  270. ret = -ENOIOCTLCMD;
  271. break;
  272. }
  273. return ret;
  274. }
  275. void line_throttle(struct tty_struct *tty)
  276. {
  277. struct line *line = tty->driver_data;
  278. deactivate_chan(&line->chan_list, line->driver->read_irq);
  279. line->throttled = 1;
  280. }
  281. void line_unthrottle(struct tty_struct *tty)
  282. {
  283. struct line *line = tty->driver_data;
  284. line->throttled = 0;
  285. chan_interrupt(&line->chan_list, &line->task, tty,
  286. line->driver->read_irq);
  287. /*
  288. * Maybe there is enough stuff pending that calling the interrupt
  289. * throttles us again. In this case, line->throttled will be 1
  290. * again and we shouldn't turn the interrupt back on.
  291. */
  292. if (!line->throttled)
  293. reactivate_chan(&line->chan_list, line->driver->read_irq);
  294. }
  295. static irqreturn_t line_write_interrupt(int irq, void *data)
  296. {
  297. struct chan *chan = data;
  298. struct line *line = chan->line;
  299. struct tty_struct *tty = line->tty;
  300. int err;
  301. /*
  302. * Interrupts are disabled here because we registered the interrupt with
  303. * IRQF_DISABLED (see line_setup_irq).
  304. */
  305. spin_lock(&line->lock);
  306. err = flush_buffer(line);
  307. if (err == 0) {
  308. return IRQ_NONE;
  309. } else if (err < 0) {
  310. line->head = line->buffer;
  311. line->tail = line->buffer;
  312. }
  313. spin_unlock(&line->lock);
  314. if (tty == NULL)
  315. return IRQ_NONE;
  316. tty_wakeup(tty);
  317. return IRQ_HANDLED;
  318. }
  319. int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
  320. {
  321. const struct line_driver *driver = line->driver;
  322. int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
  323. if (input)
  324. err = um_request_irq(driver->read_irq, fd, IRQ_READ,
  325. line_interrupt, flags,
  326. driver->read_irq_name, data);
  327. if (err)
  328. return err;
  329. if (output)
  330. err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
  331. line_write_interrupt, flags,
  332. driver->write_irq_name, data);
  333. line->have_irq = 1;
  334. return err;
  335. }
  336. /*
  337. * Normally, a driver like this can rely mostly on the tty layer
  338. * locking, particularly when it comes to the driver structure.
  339. * However, in this case, mconsole requests can come in "from the
  340. * side", and race with opens and closes.
  341. *
  342. * mconsole config requests will want to be sure the device isn't in
  343. * use, and get_config, open, and close will want a stable
  344. * configuration. The checking and modification of the configuration
  345. * is done under a spinlock. Checking whether the device is in use is
  346. * line->tty->count > 1, also under the spinlock.
  347. *
  348. * tty->count serves to decide whether the device should be enabled or
  349. * disabled on the host. If it's equal to 1, then we are doing the
  350. * first open or last close. Otherwise, open and close just return.
  351. */
  352. int line_open(struct line *lines, struct tty_struct *tty)
  353. {
  354. struct line *line = &lines[tty->index];
  355. int err = -ENODEV;
  356. spin_lock(&line->count_lock);
  357. if (!line->valid)
  358. goto out_unlock;
  359. err = 0;
  360. if (tty->count > 1)
  361. goto out_unlock;
  362. spin_unlock(&line->count_lock);
  363. tty->driver_data = line;
  364. line->tty = tty;
  365. err = enable_chan(line);
  366. if (err)
  367. return err;
  368. INIT_DELAYED_WORK(&line->task, line_timer_cb);
  369. if (!line->sigio) {
  370. chan_enable_winch(&line->chan_list, tty);
  371. line->sigio = 1;
  372. }
  373. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  374. &tty->winsize.ws_col);
  375. return err;
  376. out_unlock:
  377. spin_unlock(&line->count_lock);
  378. return err;
  379. }
  380. static void unregister_winch(struct tty_struct *tty);
  381. void line_close(struct tty_struct *tty, struct file * filp)
  382. {
  383. struct line *line = tty->driver_data;
  384. /*
  385. * If line_open fails (and tty->driver_data is never set),
  386. * tty_open will call line_close. So just return in this case.
  387. */
  388. if (line == NULL)
  389. return;
  390. /* We ignore the error anyway! */
  391. flush_buffer(line);
  392. spin_lock(&line->count_lock);
  393. if (!line->valid)
  394. goto out_unlock;
  395. if (tty->count > 1)
  396. goto out_unlock;
  397. spin_unlock(&line->count_lock);
  398. line->tty = NULL;
  399. tty->driver_data = NULL;
  400. if (line->sigio) {
  401. unregister_winch(tty);
  402. line->sigio = 0;
  403. }
  404. return;
  405. out_unlock:
  406. spin_unlock(&line->count_lock);
  407. }
  408. void close_lines(struct line *lines, int nlines)
  409. {
  410. int i;
  411. for(i = 0; i < nlines; i++)
  412. close_chan(&lines[i].chan_list, 0);
  413. }
  414. static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
  415. char **error_out)
  416. {
  417. struct line *line = &lines[n];
  418. int err = -EINVAL;
  419. spin_lock(&line->count_lock);
  420. if (line->tty != NULL) {
  421. *error_out = "Device is already open";
  422. goto out;
  423. }
  424. if (line->init_pri <= init_prio) {
  425. line->init_pri = init_prio;
  426. if (!strcmp(init, "none"))
  427. line->valid = 0;
  428. else {
  429. line->init_str = init;
  430. line->valid = 1;
  431. }
  432. }
  433. err = 0;
  434. out:
  435. spin_unlock(&line->count_lock);
  436. return err;
  437. }
  438. /*
  439. * Common setup code for both startup command line and mconsole initialization.
  440. * @lines contains the array (of size @num) to modify;
  441. * @init is the setup string;
  442. * @error_out is an error string in the case of failure;
  443. */
  444. int line_setup(struct line *lines, unsigned int num, char *init,
  445. char **error_out)
  446. {
  447. int i, n, err;
  448. char *end;
  449. if (*init == '=') {
  450. /*
  451. * We said con=/ssl= instead of con#=, so we are configuring all
  452. * consoles at once.
  453. */
  454. n = -1;
  455. }
  456. else {
  457. n = simple_strtoul(init, &end, 0);
  458. if (*end != '=') {
  459. *error_out = "Couldn't parse device number";
  460. return -EINVAL;
  461. }
  462. init = end;
  463. }
  464. init++;
  465. if (n >= (signed int) num) {
  466. *error_out = "Device number out of range";
  467. return -EINVAL;
  468. }
  469. else if (n >= 0) {
  470. err = setup_one_line(lines, n, init, INIT_ONE, error_out);
  471. if (err)
  472. return err;
  473. }
  474. else {
  475. for(i = 0; i < num; i++) {
  476. err = setup_one_line(lines, i, init, INIT_ALL,
  477. error_out);
  478. if (err)
  479. return err;
  480. }
  481. }
  482. return n == -1 ? num : n;
  483. }
  484. int line_config(struct line *lines, unsigned int num, char *str,
  485. const struct chan_opts *opts, char **error_out)
  486. {
  487. struct line *line;
  488. char *new;
  489. int n;
  490. if (*str == '=') {
  491. *error_out = "Can't configure all devices from mconsole";
  492. return -EINVAL;
  493. }
  494. new = kstrdup(str, GFP_KERNEL);
  495. if (new == NULL) {
  496. *error_out = "Failed to allocate memory";
  497. return -ENOMEM;
  498. }
  499. n = line_setup(lines, num, new, error_out);
  500. if (n < 0)
  501. return n;
  502. line = &lines[n];
  503. return parse_chan_pair(line->init_str, line, n, opts, error_out);
  504. }
  505. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  506. int size, char **error_out)
  507. {
  508. struct line *line;
  509. char *end;
  510. int dev, n = 0;
  511. dev = simple_strtoul(name, &end, 0);
  512. if ((*end != '\0') || (end == name)) {
  513. *error_out = "line_get_config failed to parse device number";
  514. return 0;
  515. }
  516. if ((dev < 0) || (dev >= num)) {
  517. *error_out = "device number out of range";
  518. return 0;
  519. }
  520. line = &lines[dev];
  521. spin_lock(&line->count_lock);
  522. if (!line->valid)
  523. CONFIG_CHUNK(str, size, n, "none", 1);
  524. else if (line->tty == NULL)
  525. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  526. else n = chan_config_string(&line->chan_list, str, size, error_out);
  527. spin_unlock(&line->count_lock);
  528. return n;
  529. }
  530. int line_id(char **str, int *start_out, int *end_out)
  531. {
  532. char *end;
  533. int n;
  534. n = simple_strtoul(*str, &end, 0);
  535. if ((*end != '\0') || (end == *str))
  536. return -1;
  537. *str = end;
  538. *start_out = n;
  539. *end_out = n;
  540. return n;
  541. }
  542. int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
  543. {
  544. int err;
  545. char config[sizeof("conxxxx=none\0")];
  546. sprintf(config, "%d=none", n);
  547. err = line_setup(lines, num, config, error_out);
  548. if (err >= 0)
  549. err = 0;
  550. return err;
  551. }
  552. struct tty_driver *register_lines(struct line_driver *line_driver,
  553. const struct tty_operations *ops,
  554. struct line *lines, int nlines)
  555. {
  556. int i;
  557. struct tty_driver *driver = alloc_tty_driver(nlines);
  558. if (!driver)
  559. return NULL;
  560. driver->driver_name = line_driver->name;
  561. driver->name = line_driver->device_name;
  562. driver->major = line_driver->major;
  563. driver->minor_start = line_driver->minor_start;
  564. driver->type = line_driver->type;
  565. driver->subtype = line_driver->subtype;
  566. driver->flags = TTY_DRIVER_REAL_RAW;
  567. driver->init_termios = tty_std_termios;
  568. tty_set_operations(driver, ops);
  569. if (tty_register_driver(driver)) {
  570. printk(KERN_ERR "register_lines : can't register %s driver\n",
  571. line_driver->name);
  572. put_tty_driver(driver);
  573. return NULL;
  574. }
  575. for(i = 0; i < nlines; i++) {
  576. if (!lines[i].valid)
  577. tty_unregister_device(driver, i);
  578. }
  579. mconsole_register_dev(&line_driver->mc);
  580. return driver;
  581. }
  582. static DEFINE_SPINLOCK(winch_handler_lock);
  583. static LIST_HEAD(winch_handlers);
  584. void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
  585. {
  586. struct line *line;
  587. char *error;
  588. int i;
  589. for(i = 0; i < nlines; i++) {
  590. line = &lines[i];
  591. INIT_LIST_HEAD(&line->chan_list);
  592. if (line->init_str == NULL)
  593. continue;
  594. line->init_str = kstrdup(line->init_str, GFP_KERNEL);
  595. if (line->init_str == NULL)
  596. printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
  597. if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
  598. printk(KERN_ERR "parse_chan_pair failed for "
  599. "device %d : %s\n", i, error);
  600. line->valid = 0;
  601. }
  602. }
  603. }
  604. struct winch {
  605. struct list_head list;
  606. int fd;
  607. int tty_fd;
  608. int pid;
  609. struct tty_struct *tty;
  610. unsigned long stack;
  611. };
  612. static void free_winch(struct winch *winch, int free_irq_ok)
  613. {
  614. if (free_irq_ok)
  615. free_irq(WINCH_IRQ, winch);
  616. list_del(&winch->list);
  617. if (winch->pid != -1)
  618. os_kill_process(winch->pid, 1);
  619. if (winch->fd != -1)
  620. os_close_file(winch->fd);
  621. if (winch->stack != 0)
  622. free_stack(winch->stack, 0);
  623. kfree(winch);
  624. }
  625. static irqreturn_t winch_interrupt(int irq, void *data)
  626. {
  627. struct winch *winch = data;
  628. struct tty_struct *tty;
  629. struct line *line;
  630. int err;
  631. char c;
  632. if (winch->fd != -1) {
  633. err = generic_read(winch->fd, &c, NULL);
  634. if (err < 0) {
  635. if (err != -EAGAIN) {
  636. printk(KERN_ERR "winch_interrupt : "
  637. "read failed, errno = %d\n", -err);
  638. printk(KERN_ERR "fd %d is losing SIGWINCH "
  639. "support\n", winch->tty_fd);
  640. free_winch(winch, 0);
  641. return IRQ_HANDLED;
  642. }
  643. goto out;
  644. }
  645. }
  646. tty = winch->tty;
  647. if (tty != NULL) {
  648. line = tty->driver_data;
  649. if (line != NULL) {
  650. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  651. &tty->winsize.ws_col);
  652. kill_pgrp(tty->pgrp, SIGWINCH, 1);
  653. }
  654. }
  655. out:
  656. if (winch->fd != -1)
  657. reactivate_fd(winch->fd, WINCH_IRQ);
  658. return IRQ_HANDLED;
  659. }
  660. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
  661. unsigned long stack)
  662. {
  663. struct winch *winch;
  664. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  665. if (winch == NULL) {
  666. printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
  667. goto cleanup;
  668. }
  669. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  670. .fd = fd,
  671. .tty_fd = tty_fd,
  672. .pid = pid,
  673. .tty = tty,
  674. .stack = stack });
  675. if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  676. IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  677. "winch", winch) < 0) {
  678. printk(KERN_ERR "register_winch_irq - failed to register "
  679. "IRQ\n");
  680. goto out_free;
  681. }
  682. spin_lock(&winch_handler_lock);
  683. list_add(&winch->list, &winch_handlers);
  684. spin_unlock(&winch_handler_lock);
  685. return;
  686. out_free:
  687. kfree(winch);
  688. cleanup:
  689. os_kill_process(pid, 1);
  690. os_close_file(fd);
  691. if (stack != 0)
  692. free_stack(stack, 0);
  693. }
  694. static void unregister_winch(struct tty_struct *tty)
  695. {
  696. struct list_head *ele, *next;
  697. struct winch *winch;
  698. spin_lock(&winch_handler_lock);
  699. list_for_each_safe(ele, next, &winch_handlers) {
  700. winch = list_entry(ele, struct winch, list);
  701. if (winch->tty == tty) {
  702. free_winch(winch, 1);
  703. break;
  704. }
  705. }
  706. spin_unlock(&winch_handler_lock);
  707. }
  708. static void winch_cleanup(void)
  709. {
  710. struct list_head *ele, *next;
  711. struct winch *winch;
  712. spin_lock(&winch_handler_lock);
  713. list_for_each_safe(ele, next, &winch_handlers) {
  714. winch = list_entry(ele, struct winch, list);
  715. free_winch(winch, 1);
  716. }
  717. spin_unlock(&winch_handler_lock);
  718. }
  719. __uml_exitcall(winch_cleanup);
  720. char *add_xterm_umid(char *base)
  721. {
  722. char *umid, *title;
  723. int len;
  724. umid = get_umid();
  725. if (*umid == '\0')
  726. return base;
  727. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  728. title = kmalloc(len, GFP_KERNEL);
  729. if (title == NULL) {
  730. printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
  731. return base;
  732. }
  733. snprintf(title, len, "%s (%s)", base, umid);
  734. return title;
  735. }