line.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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 "chan_kern.h"
  8. #include "irq_kern.h"
  9. #include "irq_user.h"
  10. #include "kern_util.h"
  11. #include "os.h"
  12. #define LINE_BUFSIZE 4096
  13. static irqreturn_t line_interrupt(int irq, void *data)
  14. {
  15. struct chan *chan = data;
  16. struct line *line = chan->line;
  17. struct tty_struct *tty = line->tty;
  18. if (line)
  19. chan_interrupt(&line->chan_list, &line->task, tty, irq);
  20. return IRQ_HANDLED;
  21. }
  22. static void line_timer_cb(struct work_struct *work)
  23. {
  24. struct line *line = container_of(work, struct line, task.work);
  25. if (!line->throttled)
  26. chan_interrupt(&line->chan_list, &line->task, line->tty,
  27. line->driver->read_irq);
  28. }
  29. /*
  30. * Returns the free space inside the ring buffer of this line.
  31. *
  32. * Should be called while holding line->lock (this does not modify data).
  33. */
  34. static int write_room(struct line *line)
  35. {
  36. int n;
  37. if (line->buffer == NULL)
  38. return LINE_BUFSIZE - 1;
  39. /* This is for the case where the buffer is wrapped! */
  40. n = line->head - line->tail;
  41. if (n <= 0)
  42. n += LINE_BUFSIZE; /* The other case */
  43. return n - 1;
  44. }
  45. int line_write_room(struct tty_struct *tty)
  46. {
  47. struct line *line = tty->driver_data;
  48. unsigned long flags;
  49. int room;
  50. spin_lock_irqsave(&line->lock, flags);
  51. room = write_room(line);
  52. spin_unlock_irqrestore(&line->lock, flags);
  53. return room;
  54. }
  55. int line_chars_in_buffer(struct tty_struct *tty)
  56. {
  57. struct line *line = tty->driver_data;
  58. unsigned long flags;
  59. int ret;
  60. spin_lock_irqsave(&line->lock, flags);
  61. /* write_room subtracts 1 for the needed NULL, so we readd it.*/
  62. ret = LINE_BUFSIZE - (write_room(line) + 1);
  63. spin_unlock_irqrestore(&line->lock, flags);
  64. return ret;
  65. }
  66. /*
  67. * This copies the content of buf into the circular buffer associated with
  68. * this line.
  69. * The return value is the number of characters actually copied, i.e. the ones
  70. * for which there was space: this function is not supposed to ever flush out
  71. * the circular buffer.
  72. *
  73. * Must be called while holding line->lock!
  74. */
  75. static int buffer_data(struct line *line, const char *buf, int len)
  76. {
  77. int end, room;
  78. if (line->buffer == NULL) {
  79. line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
  80. if (line->buffer == NULL) {
  81. printk(KERN_ERR "buffer_data - atomic allocation "
  82. "failed\n");
  83. return 0;
  84. }
  85. line->head = line->buffer;
  86. line->tail = line->buffer;
  87. }
  88. room = write_room(line);
  89. len = (len > room) ? room : len;
  90. end = line->buffer + LINE_BUFSIZE - line->tail;
  91. if (len < end) {
  92. memcpy(line->tail, buf, len);
  93. line->tail += len;
  94. }
  95. else {
  96. /* The circular buffer is wrapping */
  97. memcpy(line->tail, buf, end);
  98. buf += end;
  99. memcpy(line->buffer, buf, len - end);
  100. line->tail = line->buffer + len - end;
  101. }
  102. return len;
  103. }
  104. /*
  105. * Flushes the ring buffer to the output channels. That is, write_chan is
  106. * called, passing it line->head as buffer, and an appropriate count.
  107. *
  108. * On exit, returns 1 when the buffer is empty,
  109. * 0 when the buffer is not empty on exit,
  110. * and -errno when an error occurred.
  111. *
  112. * Must be called while holding line->lock!*/
  113. static int flush_buffer(struct line *line)
  114. {
  115. int n, count;
  116. if ((line->buffer == NULL) || (line->head == line->tail))
  117. return 1;
  118. if (line->tail < line->head) {
  119. /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
  120. count = line->buffer + LINE_BUFSIZE - line->head;
  121. n = write_chan(&line->chan_list, line->head, count,
  122. line->driver->write_irq);
  123. if (n < 0)
  124. return n;
  125. if (n == count) {
  126. /*
  127. * We have flushed from ->head to buffer end, now we
  128. * must flush only from the beginning to ->tail.
  129. */
  130. line->head = line->buffer;
  131. } else {
  132. line->head += n;
  133. return 0;
  134. }
  135. }
  136. count = line->tail - line->head;
  137. n = write_chan(&line->chan_list, line->head, count,
  138. line->driver->write_irq);
  139. if (n < 0)
  140. return n;
  141. line->head += n;
  142. return line->head == line->tail;
  143. }
  144. void line_flush_buffer(struct tty_struct *tty)
  145. {
  146. struct line *line = tty->driver_data;
  147. unsigned long flags;
  148. int err;
  149. spin_lock_irqsave(&line->lock, flags);
  150. err = 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, struct file * file,
  217. unsigned int cmd, 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 we registered the interrupt with
  302. * IRQF_DISABLED (see line_setup_irq).
  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_DISABLED | 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. * tty->count serves to decide whether the device should be enabled or
  348. * disabled on the host. If it's equal to 1, 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. spin_lock(&line->count_lock);
  356. if (!line->valid)
  357. goto out_unlock;
  358. err = 0;
  359. if (tty->count > 1)
  360. goto out_unlock;
  361. spin_unlock(&line->count_lock);
  362. tty->driver_data = line;
  363. line->tty = tty;
  364. err = enable_chan(line);
  365. if (err)
  366. return err;
  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. return err;
  375. out_unlock:
  376. spin_unlock(&line->count_lock);
  377. return err;
  378. }
  379. static void unregister_winch(struct tty_struct *tty);
  380. void line_close(struct tty_struct *tty, struct file * filp)
  381. {
  382. struct line *line = tty->driver_data;
  383. /*
  384. * If line_open fails (and tty->driver_data is never set),
  385. * tty_open will call line_close. So just return in this case.
  386. */
  387. if (line == NULL)
  388. return;
  389. /* We ignore the error anyway! */
  390. flush_buffer(line);
  391. spin_lock(&line->count_lock);
  392. if (!line->valid)
  393. goto out_unlock;
  394. if (tty->count > 1)
  395. goto out_unlock;
  396. spin_unlock(&line->count_lock);
  397. line->tty = NULL;
  398. tty->driver_data = NULL;
  399. if (line->sigio) {
  400. unregister_winch(tty);
  401. line->sigio = 0;
  402. }
  403. return;
  404. out_unlock:
  405. spin_unlock(&line->count_lock);
  406. }
  407. void close_lines(struct line *lines, int nlines)
  408. {
  409. int i;
  410. for(i = 0; i < nlines; i++)
  411. close_chan(&lines[i].chan_list, 0);
  412. }
  413. static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
  414. char **error_out)
  415. {
  416. struct line *line = &lines[n];
  417. int err = -EINVAL;
  418. spin_lock(&line->count_lock);
  419. if (line->tty != NULL) {
  420. *error_out = "Device is already open";
  421. goto out;
  422. }
  423. if (line->init_pri <= init_prio) {
  424. line->init_pri = init_prio;
  425. if (!strcmp(init, "none"))
  426. line->valid = 0;
  427. else {
  428. line->init_str = init;
  429. line->valid = 1;
  430. }
  431. }
  432. err = 0;
  433. out:
  434. spin_unlock(&line->count_lock);
  435. return err;
  436. }
  437. /*
  438. * Common setup code for both startup command line and mconsole initialization.
  439. * @lines contains the array (of size @num) to modify;
  440. * @init is the setup string;
  441. * @error_out is an error string in the case of failure;
  442. */
  443. int line_setup(struct line *lines, unsigned int num, char *init,
  444. char **error_out)
  445. {
  446. int i, n, err;
  447. char *end;
  448. if (*init == '=') {
  449. /*
  450. * We said con=/ssl= instead of con#=, so we are configuring all
  451. * consoles at once.
  452. */
  453. n = -1;
  454. }
  455. else {
  456. n = simple_strtoul(init, &end, 0);
  457. if (*end != '=') {
  458. *error_out = "Couldn't parse device number";
  459. return -EINVAL;
  460. }
  461. init = end;
  462. }
  463. init++;
  464. if (n >= (signed int) num) {
  465. *error_out = "Device number out of range";
  466. return -EINVAL;
  467. }
  468. else if (n >= 0) {
  469. err = setup_one_line(lines, n, init, INIT_ONE, error_out);
  470. if (err)
  471. return err;
  472. }
  473. else {
  474. for(i = 0; i < num; i++) {
  475. err = setup_one_line(lines, i, init, INIT_ALL,
  476. error_out);
  477. if (err)
  478. return err;
  479. }
  480. }
  481. return n == -1 ? num : n;
  482. }
  483. int line_config(struct line *lines, unsigned int num, char *str,
  484. const struct chan_opts *opts, char **error_out)
  485. {
  486. struct line *line;
  487. char *new;
  488. int n;
  489. if (*str == '=') {
  490. *error_out = "Can't configure all devices from mconsole";
  491. return -EINVAL;
  492. }
  493. new = kstrdup(str, GFP_KERNEL);
  494. if (new == NULL) {
  495. *error_out = "Failed to allocate memory";
  496. return -ENOMEM;
  497. }
  498. n = line_setup(lines, num, new, error_out);
  499. if (n < 0)
  500. return n;
  501. line = &lines[n];
  502. return parse_chan_pair(line->init_str, line, n, opts, error_out);
  503. }
  504. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  505. int size, char **error_out)
  506. {
  507. struct line *line;
  508. char *end;
  509. int dev, n = 0;
  510. dev = simple_strtoul(name, &end, 0);
  511. if ((*end != '\0') || (end == name)) {
  512. *error_out = "line_get_config failed to parse device number";
  513. return 0;
  514. }
  515. if ((dev < 0) || (dev >= num)) {
  516. *error_out = "device number out of range";
  517. return 0;
  518. }
  519. line = &lines[dev];
  520. spin_lock(&line->count_lock);
  521. if (!line->valid)
  522. CONFIG_CHUNK(str, size, n, "none", 1);
  523. else if (line->tty == NULL)
  524. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  525. else n = chan_config_string(&line->chan_list, str, size, error_out);
  526. spin_unlock(&line->count_lock);
  527. return n;
  528. }
  529. int line_id(char **str, int *start_out, int *end_out)
  530. {
  531. char *end;
  532. int n;
  533. n = simple_strtoul(*str, &end, 0);
  534. if ((*end != '\0') || (end == *str))
  535. return -1;
  536. *str = end;
  537. *start_out = n;
  538. *end_out = n;
  539. return n;
  540. }
  541. int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
  542. {
  543. int err;
  544. char config[sizeof("conxxxx=none\0")];
  545. sprintf(config, "%d=none", n);
  546. err = line_setup(lines, num, config, error_out);
  547. if (err >= 0)
  548. err = 0;
  549. return err;
  550. }
  551. struct tty_driver *register_lines(struct line_driver *line_driver,
  552. const struct tty_operations *ops,
  553. struct line *lines, int nlines)
  554. {
  555. int i;
  556. struct tty_driver *driver = alloc_tty_driver(nlines);
  557. if (!driver)
  558. return NULL;
  559. driver->driver_name = line_driver->name;
  560. driver->name = line_driver->device_name;
  561. driver->major = line_driver->major;
  562. driver->minor_start = line_driver->minor_start;
  563. driver->type = line_driver->type;
  564. driver->subtype = line_driver->subtype;
  565. driver->flags = TTY_DRIVER_REAL_RAW;
  566. driver->init_termios = tty_std_termios;
  567. tty_set_operations(driver, ops);
  568. if (tty_register_driver(driver)) {
  569. printk(KERN_ERR "register_lines : can't register %s driver\n",
  570. line_driver->name);
  571. put_tty_driver(driver);
  572. return NULL;
  573. }
  574. for(i = 0; i < nlines; i++) {
  575. if (!lines[i].valid)
  576. tty_unregister_device(driver, i);
  577. }
  578. mconsole_register_dev(&line_driver->mc);
  579. return driver;
  580. }
  581. static DEFINE_SPINLOCK(winch_handler_lock);
  582. static LIST_HEAD(winch_handlers);
  583. void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
  584. {
  585. struct line *line;
  586. char *error;
  587. int i;
  588. for(i = 0; i < nlines; i++) {
  589. line = &lines[i];
  590. INIT_LIST_HEAD(&line->chan_list);
  591. if (line->init_str == NULL)
  592. continue;
  593. line->init_str = kstrdup(line->init_str, GFP_KERNEL);
  594. if (line->init_str == NULL)
  595. printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
  596. if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
  597. printk(KERN_ERR "parse_chan_pair failed for "
  598. "device %d : %s\n", i, error);
  599. line->valid = 0;
  600. }
  601. }
  602. }
  603. struct winch {
  604. struct list_head list;
  605. int fd;
  606. int tty_fd;
  607. int pid;
  608. struct tty_struct *tty;
  609. unsigned long stack;
  610. };
  611. static void free_winch(struct winch *winch, int free_irq_ok)
  612. {
  613. list_del(&winch->list);
  614. if (winch->pid != -1)
  615. os_kill_process(winch->pid, 1);
  616. if (winch->fd != -1)
  617. os_close_file(winch->fd);
  618. if (winch->stack != 0)
  619. free_stack(winch->stack, 0);
  620. if (free_irq_ok)
  621. free_irq(WINCH_IRQ, winch);
  622. kfree(winch);
  623. }
  624. static irqreturn_t winch_interrupt(int irq, void *data)
  625. {
  626. struct winch *winch = data;
  627. struct tty_struct *tty;
  628. struct line *line;
  629. int err;
  630. char c;
  631. if (winch->fd != -1) {
  632. err = generic_read(winch->fd, &c, NULL);
  633. if (err < 0) {
  634. if (err != -EAGAIN) {
  635. printk(KERN_ERR "winch_interrupt : "
  636. "read failed, errno = %d\n", -err);
  637. printk(KERN_ERR "fd %d is losing SIGWINCH "
  638. "support\n", winch->tty_fd);
  639. free_winch(winch, 0);
  640. return IRQ_HANDLED;
  641. }
  642. goto out;
  643. }
  644. }
  645. tty = winch->tty;
  646. if (tty != NULL) {
  647. line = tty->driver_data;
  648. if (line != NULL) {
  649. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  650. &tty->winsize.ws_col);
  651. kill_pgrp(tty->pgrp, SIGWINCH, 1);
  652. }
  653. }
  654. out:
  655. if (winch->fd != -1)
  656. reactivate_fd(winch->fd, WINCH_IRQ);
  657. return IRQ_HANDLED;
  658. }
  659. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
  660. unsigned long stack)
  661. {
  662. struct winch *winch;
  663. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  664. if (winch == NULL) {
  665. printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
  666. goto cleanup;
  667. }
  668. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  669. .fd = fd,
  670. .tty_fd = tty_fd,
  671. .pid = pid,
  672. .tty = tty,
  673. .stack = stack });
  674. if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  675. IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  676. "winch", winch) < 0) {
  677. printk(KERN_ERR "register_winch_irq - failed to register "
  678. "IRQ\n");
  679. goto out_free;
  680. }
  681. spin_lock(&winch_handler_lock);
  682. list_add(&winch->list, &winch_handlers);
  683. spin_unlock(&winch_handler_lock);
  684. return;
  685. out_free:
  686. kfree(winch);
  687. cleanup:
  688. os_kill_process(pid, 1);
  689. os_close_file(fd);
  690. if (stack != 0)
  691. free_stack(stack, 0);
  692. }
  693. static void unregister_winch(struct tty_struct *tty)
  694. {
  695. struct list_head *ele;
  696. struct winch *winch;
  697. spin_lock(&winch_handler_lock);
  698. list_for_each(ele, &winch_handlers) {
  699. winch = list_entry(ele, struct winch, list);
  700. if (winch->tty == tty) {
  701. free_winch(winch, 1);
  702. break;
  703. }
  704. }
  705. spin_unlock(&winch_handler_lock);
  706. }
  707. static void winch_cleanup(void)
  708. {
  709. struct list_head *ele, *next;
  710. struct winch *winch;
  711. spin_lock(&winch_handler_lock);
  712. list_for_each_safe(ele, next, &winch_handlers) {
  713. winch = list_entry(ele, struct winch, list);
  714. free_winch(winch, 1);
  715. }
  716. spin_unlock(&winch_handler_lock);
  717. }
  718. __uml_exitcall(winch_cleanup);
  719. char *add_xterm_umid(char *base)
  720. {
  721. char *umid, *title;
  722. int len;
  723. umid = get_umid();
  724. if (*umid == '\0')
  725. return base;
  726. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  727. title = kmalloc(len, GFP_KERNEL);
  728. if (title == NULL) {
  729. printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
  730. return base;
  731. }
  732. snprintf(title, len, "%s (%s)", base, umid);
  733. return title;
  734. }