line.c 19 KB

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