line.c 19 KB

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