line.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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.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. spin_lock_irqsave(&line->lock, flags);
  150. flush_buffer(line);
  151. spin_unlock_irqrestore(&line->lock, flags);
  152. }
  153. /*
  154. * We map both ->flush_chars and ->put_char (which go in pair) onto
  155. * ->flush_buffer and ->write. Hope it's not that bad.
  156. */
  157. void line_flush_chars(struct tty_struct *tty)
  158. {
  159. line_flush_buffer(tty);
  160. }
  161. int line_put_char(struct tty_struct *tty, unsigned char ch)
  162. {
  163. return line_write(tty, &ch, sizeof(ch));
  164. }
  165. int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
  166. {
  167. struct line *line = tty->driver_data;
  168. unsigned long flags;
  169. int n, ret = 0;
  170. spin_lock_irqsave(&line->lock, flags);
  171. if (line->head != line->tail)
  172. ret = buffer_data(line, buf, len);
  173. else {
  174. n = write_chan(&line->chan_list, buf, len,
  175. line->driver->write_irq);
  176. if (n < 0) {
  177. ret = n;
  178. goto out_up;
  179. }
  180. len -= n;
  181. ret += n;
  182. if (len > 0)
  183. ret += buffer_data(line, buf + n, len);
  184. }
  185. out_up:
  186. spin_unlock_irqrestore(&line->lock, flags);
  187. return ret;
  188. }
  189. void line_set_termios(struct tty_struct *tty, struct ktermios * old)
  190. {
  191. /* nothing */
  192. }
  193. static const struct {
  194. int cmd;
  195. char *level;
  196. char *name;
  197. } tty_ioctls[] = {
  198. /* don't print these, they flood the log ... */
  199. { TCGETS, NULL, "TCGETS" },
  200. { TCSETS, NULL, "TCSETS" },
  201. { TCSETSW, NULL, "TCSETSW" },
  202. { TCFLSH, NULL, "TCFLSH" },
  203. { TCSBRK, NULL, "TCSBRK" },
  204. /* general tty stuff */
  205. { TCSETSF, KERN_DEBUG, "TCSETSF" },
  206. { TCGETA, KERN_DEBUG, "TCGETA" },
  207. { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
  208. { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
  209. { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
  210. /* linux-specific ones */
  211. { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
  212. { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
  213. { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
  214. { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
  215. };
  216. int line_ioctl(struct tty_struct *tty, unsigned int cmd,
  217. unsigned long arg)
  218. {
  219. int ret;
  220. int i;
  221. ret = 0;
  222. switch(cmd) {
  223. #ifdef TIOCGETP
  224. case TIOCGETP:
  225. case TIOCSETP:
  226. case TIOCSETN:
  227. #endif
  228. #ifdef TIOCGETC
  229. case TIOCGETC:
  230. case TIOCSETC:
  231. #endif
  232. #ifdef TIOCGLTC
  233. case TIOCGLTC:
  234. case TIOCSLTC:
  235. #endif
  236. /* Note: these are out of date as we now have TCGETS2 etc but this
  237. whole lot should probably go away */
  238. case TCGETS:
  239. case TCSETSF:
  240. case TCSETSW:
  241. case TCSETS:
  242. case TCGETA:
  243. case TCSETAF:
  244. case TCSETAW:
  245. case TCSETA:
  246. case TCXONC:
  247. case TCFLSH:
  248. case TIOCOUTQ:
  249. case TIOCINQ:
  250. case TIOCGLCKTRMIOS:
  251. case TIOCSLCKTRMIOS:
  252. case TIOCPKT:
  253. case TIOCGSOFTCAR:
  254. case TIOCSSOFTCAR:
  255. return -ENOIOCTLCMD;
  256. #if 0
  257. case TCwhatever:
  258. /* do something */
  259. break;
  260. #endif
  261. default:
  262. for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
  263. if (cmd == tty_ioctls[i].cmd)
  264. break;
  265. if (i == ARRAY_SIZE(tty_ioctls)) {
  266. printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
  267. __func__, tty->name, cmd);
  268. }
  269. ret = -ENOIOCTLCMD;
  270. break;
  271. }
  272. return ret;
  273. }
  274. void line_throttle(struct tty_struct *tty)
  275. {
  276. struct line *line = tty->driver_data;
  277. deactivate_chan(&line->chan_list, line->driver->read_irq);
  278. line->throttled = 1;
  279. }
  280. void line_unthrottle(struct tty_struct *tty)
  281. {
  282. struct line *line = tty->driver_data;
  283. line->throttled = 0;
  284. chan_interrupt(&line->chan_list, &line->task, tty,
  285. line->driver->read_irq);
  286. /*
  287. * Maybe there is enough stuff pending that calling the interrupt
  288. * throttles us again. In this case, line->throttled will be 1
  289. * again and we shouldn't turn the interrupt back on.
  290. */
  291. if (!line->throttled)
  292. reactivate_chan(&line->chan_list, line->driver->read_irq);
  293. }
  294. static irqreturn_t line_write_interrupt(int irq, void *data)
  295. {
  296. struct chan *chan = data;
  297. struct line *line = chan->line;
  298. struct tty_struct *tty = line->tty;
  299. int err;
  300. /*
  301. * Interrupts are disabled here because genirq keep irqs disabled when
  302. * calling the action handler.
  303. */
  304. spin_lock(&line->lock);
  305. err = flush_buffer(line);
  306. if (err == 0) {
  307. return IRQ_NONE;
  308. } else if (err < 0) {
  309. line->head = line->buffer;
  310. line->tail = line->buffer;
  311. }
  312. spin_unlock(&line->lock);
  313. if (tty == NULL)
  314. return IRQ_NONE;
  315. tty_wakeup(tty);
  316. return IRQ_HANDLED;
  317. }
  318. int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
  319. {
  320. const struct line_driver *driver = line->driver;
  321. int err = 0, flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
  322. if (input)
  323. err = um_request_irq(driver->read_irq, fd, IRQ_READ,
  324. line_interrupt, flags,
  325. driver->read_irq_name, data);
  326. if (err)
  327. return err;
  328. if (output)
  329. err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
  330. line_write_interrupt, flags,
  331. driver->write_irq_name, data);
  332. line->have_irq = 1;
  333. return err;
  334. }
  335. /*
  336. * Normally, a driver like this can rely mostly on the tty layer
  337. * locking, particularly when it comes to the driver structure.
  338. * However, in this case, mconsole requests can come in "from the
  339. * side", and race with opens and closes.
  340. *
  341. * mconsole config requests will want to be sure the device isn't in
  342. * use, and get_config, open, and close will want a stable
  343. * configuration. The checking and modification of the configuration
  344. * is done under a spinlock. Checking whether the device is in use is
  345. * line->tty->count > 1, also under the spinlock.
  346. *
  347. * line->count serves to decide whether the device should be enabled or
  348. * disabled on the host. If it's equal to 0, then we are doing the
  349. * first open or last close. Otherwise, open and close just return.
  350. */
  351. int line_open(struct line *lines, struct tty_struct *tty)
  352. {
  353. struct line *line = &lines[tty->index];
  354. int err = -ENODEV;
  355. mutex_lock(&line->count_lock);
  356. if (!line->valid)
  357. goto out_unlock;
  358. err = 0;
  359. if (line->count++)
  360. goto out_unlock;
  361. BUG_ON(tty->driver_data);
  362. tty->driver_data = line;
  363. line->tty = tty;
  364. err = enable_chan(line);
  365. if (err) /* line_close() will be called by our caller */
  366. goto out_unlock;
  367. INIT_DELAYED_WORK(&line->task, line_timer_cb);
  368. if (!line->sigio) {
  369. chan_enable_winch(&line->chan_list, tty);
  370. line->sigio = 1;
  371. }
  372. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  373. &tty->winsize.ws_col);
  374. out_unlock:
  375. mutex_unlock(&line->count_lock);
  376. return err;
  377. }
  378. static void unregister_winch(struct tty_struct *tty);
  379. void line_close(struct tty_struct *tty, struct file * filp)
  380. {
  381. struct line *line = tty->driver_data;
  382. /*
  383. * If line_open fails (and tty->driver_data is never set),
  384. * tty_open will call line_close. So just return in this case.
  385. */
  386. if (line == NULL)
  387. return;
  388. /* We ignore the error anyway! */
  389. flush_buffer(line);
  390. mutex_lock(&line->count_lock);
  391. BUG_ON(!line->valid);
  392. if (--line->count)
  393. goto out_unlock;
  394. line->tty = NULL;
  395. tty->driver_data = NULL;
  396. if (line->sigio) {
  397. unregister_winch(tty);
  398. line->sigio = 0;
  399. }
  400. out_unlock:
  401. mutex_unlock(&line->count_lock);
  402. }
  403. void close_lines(struct line *lines, int nlines)
  404. {
  405. int i;
  406. for(i = 0; i < nlines; i++)
  407. close_chan(&lines[i].chan_list, 0);
  408. }
  409. int setup_one_line(struct line *lines, int n, char *init,
  410. const struct chan_opts *opts, char **error_out)
  411. {
  412. struct line *line = &lines[n];
  413. struct tty_driver *driver = line->driver->driver;
  414. int err = -EINVAL;
  415. mutex_lock(&line->count_lock);
  416. if (line->count) {
  417. *error_out = "Device is already open";
  418. goto out;
  419. }
  420. if (!strcmp(init, "none")) {
  421. if (line->valid) {
  422. line->valid = 0;
  423. kfree(line->init_str);
  424. tty_unregister_device(driver, n);
  425. parse_chan_pair(NULL, line, n, opts, error_out);
  426. err = 0;
  427. }
  428. } else {
  429. char *new = kstrdup(init, GFP_KERNEL);
  430. if (!new) {
  431. *error_out = "Failed to allocate memory";
  432. return -ENOMEM;
  433. }
  434. if (line->valid)
  435. tty_unregister_device(driver, n);
  436. line->init_str = new;
  437. line->valid = 1;
  438. err = parse_chan_pair(new, line, n, opts, error_out);
  439. if (!err) {
  440. struct device *d = tty_register_device(driver, n, NULL);
  441. if (IS_ERR(d)) {
  442. *error_out = "Failed to register device";
  443. err = PTR_ERR(d);
  444. parse_chan_pair(NULL, line, n, opts, error_out);
  445. }
  446. }
  447. if (err) {
  448. line->init_str = NULL;
  449. line->valid = 0;
  450. kfree(new);
  451. }
  452. }
  453. out:
  454. mutex_unlock(&line->count_lock);
  455. return err;
  456. }
  457. /*
  458. * Common setup code for both startup command line and mconsole initialization.
  459. * @lines contains the array (of size @num) to modify;
  460. * @init is the setup string;
  461. * @error_out is an error string in the case of failure;
  462. */
  463. int line_setup(char **conf, unsigned int num, char **def,
  464. char *init, char *name)
  465. {
  466. char *error;
  467. if (*init == '=') {
  468. /*
  469. * We said con=/ssl= instead of con#=, so we are configuring all
  470. * consoles at once.
  471. */
  472. *def = init + 1;
  473. } else {
  474. char *end;
  475. unsigned n = simple_strtoul(init, &end, 0);
  476. if (*end != '=') {
  477. error = "Couldn't parse device number";
  478. goto out;
  479. }
  480. if (n >= num) {
  481. error = "Device number out of range";
  482. goto out;
  483. }
  484. conf[n] = end + 1;
  485. }
  486. return 0;
  487. out:
  488. printk(KERN_ERR "Failed to set up %s with "
  489. "configuration string \"%s\" : %s\n", name, init, error);
  490. return -EINVAL;
  491. }
  492. int line_config(struct line *lines, unsigned int num, char *str,
  493. const struct chan_opts *opts, char **error_out)
  494. {
  495. char *end;
  496. int n;
  497. if (*str == '=') {
  498. *error_out = "Can't configure all devices from mconsole";
  499. return -EINVAL;
  500. }
  501. n = simple_strtoul(str, &end, 0);
  502. if (*end++ != '=') {
  503. *error_out = "Couldn't parse device number";
  504. return -EINVAL;
  505. }
  506. if (n >= num) {
  507. *error_out = "Device number out of range";
  508. return -EINVAL;
  509. }
  510. return setup_one_line(lines, n, end, opts, error_out);
  511. }
  512. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  513. int size, char **error_out)
  514. {
  515. struct line *line;
  516. char *end;
  517. int dev, n = 0;
  518. dev = simple_strtoul(name, &end, 0);
  519. if ((*end != '\0') || (end == name)) {
  520. *error_out = "line_get_config failed to parse device number";
  521. return 0;
  522. }
  523. if ((dev < 0) || (dev >= num)) {
  524. *error_out = "device number out of range";
  525. return 0;
  526. }
  527. line = &lines[dev];
  528. mutex_lock(&line->count_lock);
  529. if (!line->valid)
  530. CONFIG_CHUNK(str, size, n, "none", 1);
  531. else if (line->tty == NULL)
  532. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  533. else n = chan_config_string(&line->chan_list, str, size, error_out);
  534. mutex_unlock(&line->count_lock);
  535. return n;
  536. }
  537. int line_id(char **str, int *start_out, int *end_out)
  538. {
  539. char *end;
  540. int n;
  541. n = simple_strtoul(*str, &end, 0);
  542. if ((*end != '\0') || (end == *str))
  543. return -1;
  544. *str = end;
  545. *start_out = n;
  546. *end_out = n;
  547. return n;
  548. }
  549. int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
  550. {
  551. if (n >= num) {
  552. *error_out = "Device number out of range";
  553. return -EINVAL;
  554. }
  555. return setup_one_line(lines, n, "none", NULL, error_out);
  556. }
  557. int register_lines(struct line_driver *line_driver,
  558. const struct tty_operations *ops,
  559. struct line *lines, int nlines)
  560. {
  561. struct tty_driver *driver = alloc_tty_driver(nlines);
  562. int err;
  563. int i;
  564. if (!driver)
  565. return -ENOMEM;
  566. driver->driver_name = line_driver->name;
  567. driver->name = line_driver->device_name;
  568. driver->major = line_driver->major;
  569. driver->minor_start = line_driver->minor_start;
  570. driver->type = line_driver->type;
  571. driver->subtype = line_driver->subtype;
  572. driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  573. driver->init_termios = tty_std_termios;
  574. for (i = 0; i < nlines; i++) {
  575. spin_lock_init(&lines[i].lock);
  576. mutex_init(&lines[i].count_lock);
  577. lines[i].driver = line_driver;
  578. INIT_LIST_HEAD(&lines[i].chan_list);
  579. }
  580. tty_set_operations(driver, ops);
  581. err = tty_register_driver(driver);
  582. if (err) {
  583. printk(KERN_ERR "register_lines : can't register %s driver\n",
  584. line_driver->name);
  585. put_tty_driver(driver);
  586. return err;
  587. }
  588. line_driver->driver = driver;
  589. mconsole_register_dev(&line_driver->mc);
  590. return 0;
  591. }
  592. static DEFINE_SPINLOCK(winch_handler_lock);
  593. static LIST_HEAD(winch_handlers);
  594. struct winch {
  595. struct list_head list;
  596. int fd;
  597. int tty_fd;
  598. int pid;
  599. struct tty_struct *tty;
  600. unsigned long stack;
  601. struct work_struct work;
  602. };
  603. static void __free_winch(struct work_struct *work)
  604. {
  605. struct winch *winch = container_of(work, struct winch, work);
  606. free_irq(WINCH_IRQ, winch);
  607. if (winch->pid != -1)
  608. os_kill_process(winch->pid, 1);
  609. if (winch->stack != 0)
  610. free_stack(winch->stack, 0);
  611. kfree(winch);
  612. }
  613. static void free_winch(struct winch *winch)
  614. {
  615. int fd = winch->fd;
  616. winch->fd = -1;
  617. if (fd != -1)
  618. os_close_file(fd);
  619. list_del(&winch->list);
  620. __free_winch(&winch->work);
  621. }
  622. static irqreturn_t winch_interrupt(int irq, void *data)
  623. {
  624. struct winch *winch = data;
  625. struct tty_struct *tty;
  626. struct line *line;
  627. int fd = winch->fd;
  628. int err;
  629. char c;
  630. if (fd != -1) {
  631. err = generic_read(fd, &c, NULL);
  632. if (err < 0) {
  633. if (err != -EAGAIN) {
  634. winch->fd = -1;
  635. list_del(&winch->list);
  636. os_close_file(fd);
  637. printk(KERN_ERR "winch_interrupt : "
  638. "read failed, errno = %d\n", -err);
  639. printk(KERN_ERR "fd %d is losing SIGWINCH "
  640. "support\n", winch->tty_fd);
  641. INIT_WORK(&winch->work, __free_winch);
  642. schedule_work(&winch->work);
  643. return IRQ_HANDLED;
  644. }
  645. goto out;
  646. }
  647. }
  648. tty = winch->tty;
  649. if (tty != NULL) {
  650. line = tty->driver_data;
  651. if (line != NULL) {
  652. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  653. &tty->winsize.ws_col);
  654. kill_pgrp(tty->pgrp, SIGWINCH, 1);
  655. }
  656. }
  657. out:
  658. if (winch->fd != -1)
  659. reactivate_fd(winch->fd, WINCH_IRQ);
  660. return IRQ_HANDLED;
  661. }
  662. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
  663. unsigned long stack)
  664. {
  665. struct winch *winch;
  666. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  667. if (winch == NULL) {
  668. printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
  669. goto cleanup;
  670. }
  671. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  672. .fd = fd,
  673. .tty_fd = tty_fd,
  674. .pid = pid,
  675. .tty = tty,
  676. .stack = stack });
  677. if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  678. IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  679. "winch", winch) < 0) {
  680. printk(KERN_ERR "register_winch_irq - failed to register "
  681. "IRQ\n");
  682. goto out_free;
  683. }
  684. spin_lock(&winch_handler_lock);
  685. list_add(&winch->list, &winch_handlers);
  686. spin_unlock(&winch_handler_lock);
  687. return;
  688. out_free:
  689. kfree(winch);
  690. cleanup:
  691. os_kill_process(pid, 1);
  692. os_close_file(fd);
  693. if (stack != 0)
  694. free_stack(stack, 0);
  695. }
  696. static void unregister_winch(struct tty_struct *tty)
  697. {
  698. struct list_head *ele, *next;
  699. struct winch *winch;
  700. spin_lock(&winch_handler_lock);
  701. list_for_each_safe(ele, next, &winch_handlers) {
  702. winch = list_entry(ele, struct winch, list);
  703. if (winch->tty == tty) {
  704. free_winch(winch);
  705. break;
  706. }
  707. }
  708. spin_unlock(&winch_handler_lock);
  709. }
  710. static void winch_cleanup(void)
  711. {
  712. struct list_head *ele, *next;
  713. struct winch *winch;
  714. spin_lock(&winch_handler_lock);
  715. list_for_each_safe(ele, next, &winch_handlers) {
  716. winch = list_entry(ele, struct winch, list);
  717. free_winch(winch);
  718. }
  719. spin_unlock(&winch_handler_lock);
  720. }
  721. __uml_exitcall(winch_cleanup);
  722. char *add_xterm_umid(char *base)
  723. {
  724. char *umid, *title;
  725. int len;
  726. umid = get_umid();
  727. if (*umid == '\0')
  728. return base;
  729. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  730. title = kmalloc(len, GFP_KERNEL);
  731. if (title == NULL) {
  732. printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
  733. return base;
  734. }
  735. snprintf(title, len, "%s (%s)", base, umid);
  736. return title;
  737. }