printk.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. * linux/kernel/printk.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * Modified to make sys_syslog() more flexible: added commands to
  7. * return the last 4k of kernel messages, regardless of whether
  8. * they've been read or not. Added option to suppress kernel printk's
  9. * to the console. Added hook for sending the console messages
  10. * elsewhere, in preparation for a serial line console (someday).
  11. * Ted Ts'o, 2/11/93.
  12. * Modified for sysctl support, 1/8/97, Chris Horn.
  13. * Fixed SMP synchronization, 08/08/99, Manfred Spraul
  14. * manfreds@colorfullife.com
  15. * Rewrote bits to get rid of console_lock
  16. * 01Mar01 Andrew Morton <andrewm@uow.edu.au>
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_driver.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/console.h>
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/interrupt.h> /* For in_interrupt() */
  27. #include <linux/config.h>
  28. #include <linux/delay.h>
  29. #include <linux/smp.h>
  30. #include <linux/security.h>
  31. #include <linux/bootmem.h>
  32. #include <linux/syscalls.h>
  33. #include <asm/uaccess.h>
  34. #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
  35. /* printk's without a loglevel use this.. */
  36. #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
  37. /* We show everything that is MORE important than this.. */
  38. #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
  39. #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
  40. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  41. int console_printk[4] = {
  42. DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
  43. DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
  44. MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
  45. DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
  46. };
  47. EXPORT_SYMBOL(console_printk);
  48. /*
  49. * Low lever drivers may need that to know if they can schedule in
  50. * their unblank() callback or not. So let's export it.
  51. */
  52. int oops_in_progress;
  53. EXPORT_SYMBOL(oops_in_progress);
  54. /*
  55. * console_sem protects the console_drivers list, and also
  56. * provides serialisation for access to the entire console
  57. * driver system.
  58. */
  59. static DECLARE_MUTEX(console_sem);
  60. struct console *console_drivers;
  61. /*
  62. * This is used for debugging the mess that is the VT code by
  63. * keeping track if we have the console semaphore held. It's
  64. * definitely not the perfect debug tool (we don't know if _WE_
  65. * hold it are racing, but it helps tracking those weird code
  66. * path in the console code where we end up in places I want
  67. * locked without the console sempahore held
  68. */
  69. static int console_locked;
  70. /*
  71. * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
  72. * It is also used in interesting ways to provide interlocking in
  73. * release_console_sem().
  74. */
  75. static DEFINE_SPINLOCK(logbuf_lock);
  76. #define LOG_BUF_MASK (log_buf_len-1)
  77. #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
  78. /*
  79. * The indices into log_buf are not constrained to log_buf_len - they
  80. * must be masked before subscripting
  81. */
  82. static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
  83. static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
  84. static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
  85. /*
  86. * Array of consoles built from command line options (console=)
  87. */
  88. struct console_cmdline
  89. {
  90. char name[8]; /* Name of the driver */
  91. int index; /* Minor dev. to use */
  92. char *options; /* Options for the driver */
  93. };
  94. #define MAX_CMDLINECONSOLES 8
  95. static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
  96. static int selected_console = -1;
  97. static int preferred_console = -1;
  98. /* Flag: console code may call schedule() */
  99. static int console_may_schedule;
  100. #ifdef CONFIG_PRINTK
  101. static char __log_buf[__LOG_BUF_LEN];
  102. static char *log_buf = __log_buf;
  103. static int log_buf_len = __LOG_BUF_LEN;
  104. static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
  105. /*
  106. * Setup a list of consoles. Called from init/main.c
  107. */
  108. static int __init console_setup(char *str)
  109. {
  110. char name[sizeof(console_cmdline[0].name)];
  111. char *s, *options;
  112. int idx;
  113. /*
  114. * Decode str into name, index, options.
  115. */
  116. if (str[0] >= '0' && str[0] <= '9') {
  117. strcpy(name, "ttyS");
  118. strncpy(name + 4, str, sizeof(name) - 5);
  119. } else
  120. strncpy(name, str, sizeof(name) - 1);
  121. name[sizeof(name) - 1] = 0;
  122. if ((options = strchr(str, ',')) != NULL)
  123. *(options++) = 0;
  124. #ifdef __sparc__
  125. if (!strcmp(str, "ttya"))
  126. strcpy(name, "ttyS0");
  127. if (!strcmp(str, "ttyb"))
  128. strcpy(name, "ttyS1");
  129. #endif
  130. for(s = name; *s; s++)
  131. if ((*s >= '0' && *s <= '9') || *s == ',')
  132. break;
  133. idx = simple_strtoul(s, NULL, 10);
  134. *s = 0;
  135. add_preferred_console(name, idx, options);
  136. return 1;
  137. }
  138. __setup("console=", console_setup);
  139. static int __init log_buf_len_setup(char *str)
  140. {
  141. unsigned long size = memparse(str, &str);
  142. unsigned long flags;
  143. if (size)
  144. size = roundup_pow_of_two(size);
  145. if (size > log_buf_len) {
  146. unsigned long start, dest_idx, offset;
  147. char * new_log_buf;
  148. new_log_buf = alloc_bootmem(size);
  149. if (!new_log_buf) {
  150. printk("log_buf_len: allocation failed\n");
  151. goto out;
  152. }
  153. spin_lock_irqsave(&logbuf_lock, flags);
  154. log_buf_len = size;
  155. log_buf = new_log_buf;
  156. offset = start = min(con_start, log_start);
  157. dest_idx = 0;
  158. while (start != log_end) {
  159. log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
  160. start++;
  161. dest_idx++;
  162. }
  163. log_start -= offset;
  164. con_start -= offset;
  165. log_end -= offset;
  166. spin_unlock_irqrestore(&logbuf_lock, flags);
  167. printk("log_buf_len: %d\n", log_buf_len);
  168. }
  169. out:
  170. return 1;
  171. }
  172. __setup("log_buf_len=", log_buf_len_setup);
  173. /*
  174. * Commands to do_syslog:
  175. *
  176. * 0 -- Close the log. Currently a NOP.
  177. * 1 -- Open the log. Currently a NOP.
  178. * 2 -- Read from the log.
  179. * 3 -- Read all messages remaining in the ring buffer.
  180. * 4 -- Read and clear all messages remaining in the ring buffer
  181. * 5 -- Clear ring buffer.
  182. * 6 -- Disable printk's to console
  183. * 7 -- Enable printk's to console
  184. * 8 -- Set level of messages printed to console
  185. * 9 -- Return number of unread characters in the log buffer
  186. * 10 -- Return size of the log buffer
  187. */
  188. int do_syslog(int type, char __user * buf, int len)
  189. {
  190. unsigned long i, j, limit, count;
  191. int do_clear = 0;
  192. char c;
  193. int error = 0;
  194. error = security_syslog(type);
  195. if (error)
  196. return error;
  197. switch (type) {
  198. case 0: /* Close log */
  199. break;
  200. case 1: /* Open log */
  201. break;
  202. case 2: /* Read from log */
  203. error = -EINVAL;
  204. if (!buf || len < 0)
  205. goto out;
  206. error = 0;
  207. if (!len)
  208. goto out;
  209. if (!access_ok(VERIFY_WRITE, buf, len)) {
  210. error = -EFAULT;
  211. goto out;
  212. }
  213. error = wait_event_interruptible(log_wait, (log_start - log_end));
  214. if (error)
  215. goto out;
  216. i = 0;
  217. spin_lock_irq(&logbuf_lock);
  218. while (!error && (log_start != log_end) && i < len) {
  219. c = LOG_BUF(log_start);
  220. log_start++;
  221. spin_unlock_irq(&logbuf_lock);
  222. error = __put_user(c,buf);
  223. buf++;
  224. i++;
  225. cond_resched();
  226. spin_lock_irq(&logbuf_lock);
  227. }
  228. spin_unlock_irq(&logbuf_lock);
  229. if (!error)
  230. error = i;
  231. break;
  232. case 4: /* Read/clear last kernel messages */
  233. do_clear = 1;
  234. /* FALL THRU */
  235. case 3: /* Read last kernel messages */
  236. error = -EINVAL;
  237. if (!buf || len < 0)
  238. goto out;
  239. error = 0;
  240. if (!len)
  241. goto out;
  242. if (!access_ok(VERIFY_WRITE, buf, len)) {
  243. error = -EFAULT;
  244. goto out;
  245. }
  246. count = len;
  247. if (count > log_buf_len)
  248. count = log_buf_len;
  249. spin_lock_irq(&logbuf_lock);
  250. if (count > logged_chars)
  251. count = logged_chars;
  252. if (do_clear)
  253. logged_chars = 0;
  254. limit = log_end;
  255. /*
  256. * __put_user() could sleep, and while we sleep
  257. * printk() could overwrite the messages
  258. * we try to copy to user space. Therefore
  259. * the messages are copied in reverse. <manfreds>
  260. */
  261. for(i = 0; i < count && !error; i++) {
  262. j = limit-1-i;
  263. if (j + log_buf_len < log_end)
  264. break;
  265. c = LOG_BUF(j);
  266. spin_unlock_irq(&logbuf_lock);
  267. error = __put_user(c,&buf[count-1-i]);
  268. cond_resched();
  269. spin_lock_irq(&logbuf_lock);
  270. }
  271. spin_unlock_irq(&logbuf_lock);
  272. if (error)
  273. break;
  274. error = i;
  275. if(i != count) {
  276. int offset = count-error;
  277. /* buffer overflow during copy, correct user buffer. */
  278. for(i=0;i<error;i++) {
  279. if (__get_user(c,&buf[i+offset]) ||
  280. __put_user(c,&buf[i])) {
  281. error = -EFAULT;
  282. break;
  283. }
  284. cond_resched();
  285. }
  286. }
  287. break;
  288. case 5: /* Clear ring buffer */
  289. logged_chars = 0;
  290. break;
  291. case 6: /* Disable logging to console */
  292. console_loglevel = minimum_console_loglevel;
  293. break;
  294. case 7: /* Enable logging to console */
  295. console_loglevel = default_console_loglevel;
  296. break;
  297. case 8: /* Set level of messages printed to console */
  298. error = -EINVAL;
  299. if (len < 1 || len > 8)
  300. goto out;
  301. if (len < minimum_console_loglevel)
  302. len = minimum_console_loglevel;
  303. console_loglevel = len;
  304. error = 0;
  305. break;
  306. case 9: /* Number of chars in the log buffer */
  307. error = log_end - log_start;
  308. break;
  309. case 10: /* Size of the log buffer */
  310. error = log_buf_len;
  311. break;
  312. default:
  313. error = -EINVAL;
  314. break;
  315. }
  316. out:
  317. return error;
  318. }
  319. asmlinkage long sys_syslog(int type, char __user * buf, int len)
  320. {
  321. return do_syslog(type, buf, len);
  322. }
  323. /*
  324. * Call the console drivers on a range of log_buf
  325. */
  326. static void __call_console_drivers(unsigned long start, unsigned long end)
  327. {
  328. struct console *con;
  329. for (con = console_drivers; con; con = con->next) {
  330. if ((con->flags & CON_ENABLED) && con->write)
  331. con->write(con, &LOG_BUF(start), end - start);
  332. }
  333. }
  334. /*
  335. * Write out chars from start to end - 1 inclusive
  336. */
  337. static void _call_console_drivers(unsigned long start,
  338. unsigned long end, int msg_log_level)
  339. {
  340. if (msg_log_level < console_loglevel &&
  341. console_drivers && start != end) {
  342. if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
  343. /* wrapped write */
  344. __call_console_drivers(start & LOG_BUF_MASK,
  345. log_buf_len);
  346. __call_console_drivers(0, end & LOG_BUF_MASK);
  347. } else {
  348. __call_console_drivers(start, end);
  349. }
  350. }
  351. }
  352. /*
  353. * Call the console drivers, asking them to write out
  354. * log_buf[start] to log_buf[end - 1].
  355. * The console_sem must be held.
  356. */
  357. static void call_console_drivers(unsigned long start, unsigned long end)
  358. {
  359. unsigned long cur_index, start_print;
  360. static int msg_level = -1;
  361. if (((long)(start - end)) > 0)
  362. BUG();
  363. cur_index = start;
  364. start_print = start;
  365. while (cur_index != end) {
  366. if ( msg_level < 0 &&
  367. ((end - cur_index) > 2) &&
  368. LOG_BUF(cur_index + 0) == '<' &&
  369. LOG_BUF(cur_index + 1) >= '0' &&
  370. LOG_BUF(cur_index + 1) <= '7' &&
  371. LOG_BUF(cur_index + 2) == '>')
  372. {
  373. msg_level = LOG_BUF(cur_index + 1) - '0';
  374. cur_index += 3;
  375. start_print = cur_index;
  376. }
  377. while (cur_index != end) {
  378. char c = LOG_BUF(cur_index);
  379. cur_index++;
  380. if (c == '\n') {
  381. if (msg_level < 0) {
  382. /*
  383. * printk() has already given us loglevel tags in
  384. * the buffer. This code is here in case the
  385. * log buffer has wrapped right round and scribbled
  386. * on those tags
  387. */
  388. msg_level = default_message_loglevel;
  389. }
  390. _call_console_drivers(start_print, cur_index, msg_level);
  391. msg_level = -1;
  392. start_print = cur_index;
  393. break;
  394. }
  395. }
  396. }
  397. _call_console_drivers(start_print, end, msg_level);
  398. }
  399. static void emit_log_char(char c)
  400. {
  401. LOG_BUF(log_end) = c;
  402. log_end++;
  403. if (log_end - log_start > log_buf_len)
  404. log_start = log_end - log_buf_len;
  405. if (log_end - con_start > log_buf_len)
  406. con_start = log_end - log_buf_len;
  407. if (logged_chars < log_buf_len)
  408. logged_chars++;
  409. }
  410. /*
  411. * Zap console related locks when oopsing. Only zap at most once
  412. * every 10 seconds, to leave time for slow consoles to print a
  413. * full oops.
  414. */
  415. static void zap_locks(void)
  416. {
  417. static unsigned long oops_timestamp;
  418. if (time_after_eq(jiffies, oops_timestamp) &&
  419. !time_after(jiffies, oops_timestamp + 30*HZ))
  420. return;
  421. oops_timestamp = jiffies;
  422. /* If a crash is occurring, make sure we can't deadlock */
  423. spin_lock_init(&logbuf_lock);
  424. /* And make sure that we print immediately */
  425. init_MUTEX(&console_sem);
  426. }
  427. #if defined(CONFIG_PRINTK_TIME)
  428. static int printk_time = 1;
  429. #else
  430. static int printk_time = 0;
  431. #endif
  432. static int __init printk_time_setup(char *str)
  433. {
  434. if (*str)
  435. return 0;
  436. printk_time = 1;
  437. return 1;
  438. }
  439. __setup("time", printk_time_setup);
  440. /*
  441. * This is printk. It can be called from any context. We want it to work.
  442. *
  443. * We try to grab the console_sem. If we succeed, it's easy - we log the output and
  444. * call the console drivers. If we fail to get the semaphore we place the output
  445. * into the log buffer and return. The current holder of the console_sem will
  446. * notice the new output in release_console_sem() and will send it to the
  447. * consoles before releasing the semaphore.
  448. *
  449. * One effect of this deferred printing is that code which calls printk() and
  450. * then changes console_loglevel may break. This is because console_loglevel
  451. * is inspected when the actual printing occurs.
  452. */
  453. asmlinkage int printk(const char *fmt, ...)
  454. {
  455. va_list args;
  456. int r;
  457. va_start(args, fmt);
  458. r = vprintk(fmt, args);
  459. va_end(args);
  460. return r;
  461. }
  462. /* cpu currently holding logbuf_lock */
  463. static volatile unsigned int printk_cpu = UINT_MAX;
  464. asmlinkage int vprintk(const char *fmt, va_list args)
  465. {
  466. unsigned long flags;
  467. int printed_len;
  468. char *p;
  469. static char printk_buf[1024];
  470. static int log_level_unknown = 1;
  471. preempt_disable();
  472. if (unlikely(oops_in_progress) && printk_cpu == smp_processor_id())
  473. /* If a crash is occurring during printk() on this CPU,
  474. * make sure we can't deadlock */
  475. zap_locks();
  476. /* This stops the holder of console_sem just where we want him */
  477. spin_lock_irqsave(&logbuf_lock, flags);
  478. printk_cpu = smp_processor_id();
  479. /* Emit the output into the temporary buffer */
  480. printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args);
  481. /*
  482. * Copy the output into log_buf. If the caller didn't provide
  483. * appropriate log level tags, we insert them here
  484. */
  485. for (p = printk_buf; *p; p++) {
  486. if (log_level_unknown) {
  487. /* log_level_unknown signals the start of a new line */
  488. if (printk_time) {
  489. int loglev_char;
  490. char tbuf[50], *tp;
  491. unsigned tlen;
  492. unsigned long long t;
  493. unsigned long nanosec_rem;
  494. /*
  495. * force the log level token to be
  496. * before the time output.
  497. */
  498. if (p[0] == '<' && p[1] >='0' &&
  499. p[1] <= '7' && p[2] == '>') {
  500. loglev_char = p[1];
  501. p += 3;
  502. printed_len += 3;
  503. } else {
  504. loglev_char = default_message_loglevel
  505. + '0';
  506. }
  507. t = sched_clock();
  508. nanosec_rem = do_div(t, 1000000000);
  509. tlen = sprintf(tbuf,
  510. "<%c>[%5lu.%06lu] ",
  511. loglev_char,
  512. (unsigned long)t,
  513. nanosec_rem/1000);
  514. for (tp = tbuf; tp < tbuf + tlen; tp++)
  515. emit_log_char(*tp);
  516. printed_len += tlen - 3;
  517. } else {
  518. if (p[0] != '<' || p[1] < '0' ||
  519. p[1] > '7' || p[2] != '>') {
  520. emit_log_char('<');
  521. emit_log_char(default_message_loglevel
  522. + '0');
  523. emit_log_char('>');
  524. }
  525. printed_len += 3;
  526. }
  527. log_level_unknown = 0;
  528. if (!*p)
  529. break;
  530. }
  531. emit_log_char(*p);
  532. if (*p == '\n')
  533. log_level_unknown = 1;
  534. }
  535. if (!cpu_online(smp_processor_id())) {
  536. /*
  537. * Some console drivers may assume that per-cpu resources have
  538. * been allocated. So don't allow them to be called by this
  539. * CPU until it is officially up. We shouldn't be calling into
  540. * random console drivers on a CPU which doesn't exist yet..
  541. */
  542. printk_cpu = UINT_MAX;
  543. spin_unlock_irqrestore(&logbuf_lock, flags);
  544. goto out;
  545. }
  546. if (!down_trylock(&console_sem)) {
  547. console_locked = 1;
  548. /*
  549. * We own the drivers. We can drop the spinlock and let
  550. * release_console_sem() print the text
  551. */
  552. printk_cpu = UINT_MAX;
  553. spin_unlock_irqrestore(&logbuf_lock, flags);
  554. console_may_schedule = 0;
  555. release_console_sem();
  556. } else {
  557. /*
  558. * Someone else owns the drivers. We drop the spinlock, which
  559. * allows the semaphore holder to proceed and to call the
  560. * console drivers with the output which we just produced.
  561. */
  562. printk_cpu = UINT_MAX;
  563. spin_unlock_irqrestore(&logbuf_lock, flags);
  564. }
  565. out:
  566. preempt_enable();
  567. return printed_len;
  568. }
  569. EXPORT_SYMBOL(printk);
  570. EXPORT_SYMBOL(vprintk);
  571. #else
  572. asmlinkage long sys_syslog(int type, char __user * buf, int len)
  573. {
  574. return 0;
  575. }
  576. int do_syslog(int type, char __user * buf, int len) { return 0; }
  577. static void call_console_drivers(unsigned long start, unsigned long end) {}
  578. #endif
  579. /**
  580. * add_preferred_console - add a device to the list of preferred consoles.
  581. *
  582. * The last preferred console added will be used for kernel messages
  583. * and stdin/out/err for init. Normally this is used by console_setup
  584. * above to handle user-supplied console arguments; however it can also
  585. * be used by arch-specific code either to override the user or more
  586. * commonly to provide a default console (ie from PROM variables) when
  587. * the user has not supplied one.
  588. */
  589. int __init add_preferred_console(char *name, int idx, char *options)
  590. {
  591. struct console_cmdline *c;
  592. int i;
  593. /*
  594. * See if this tty is not yet registered, and
  595. * if we have a slot free.
  596. */
  597. for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
  598. if (strcmp(console_cmdline[i].name, name) == 0 &&
  599. console_cmdline[i].index == idx) {
  600. selected_console = i;
  601. return 0;
  602. }
  603. if (i == MAX_CMDLINECONSOLES)
  604. return -E2BIG;
  605. selected_console = i;
  606. c = &console_cmdline[i];
  607. memcpy(c->name, name, sizeof(c->name));
  608. c->name[sizeof(c->name) - 1] = 0;
  609. c->options = options;
  610. c->index = idx;
  611. return 0;
  612. }
  613. /**
  614. * acquire_console_sem - lock the console system for exclusive use.
  615. *
  616. * Acquires a semaphore which guarantees that the caller has
  617. * exclusive access to the console system and the console_drivers list.
  618. *
  619. * Can sleep, returns nothing.
  620. */
  621. void acquire_console_sem(void)
  622. {
  623. if (in_interrupt())
  624. BUG();
  625. down(&console_sem);
  626. console_locked = 1;
  627. console_may_schedule = 1;
  628. }
  629. EXPORT_SYMBOL(acquire_console_sem);
  630. int try_acquire_console_sem(void)
  631. {
  632. if (down_trylock(&console_sem))
  633. return -1;
  634. console_locked = 1;
  635. console_may_schedule = 0;
  636. return 0;
  637. }
  638. EXPORT_SYMBOL(try_acquire_console_sem);
  639. int is_console_locked(void)
  640. {
  641. return console_locked;
  642. }
  643. EXPORT_SYMBOL(is_console_locked);
  644. /**
  645. * release_console_sem - unlock the console system
  646. *
  647. * Releases the semaphore which the caller holds on the console system
  648. * and the console driver list.
  649. *
  650. * While the semaphore was held, console output may have been buffered
  651. * by printk(). If this is the case, release_console_sem() emits
  652. * the output prior to releasing the semaphore.
  653. *
  654. * If there is output waiting for klogd, we wake it up.
  655. *
  656. * release_console_sem() may be called from any context.
  657. */
  658. void release_console_sem(void)
  659. {
  660. unsigned long flags;
  661. unsigned long _con_start, _log_end;
  662. unsigned long wake_klogd = 0;
  663. for ( ; ; ) {
  664. spin_lock_irqsave(&logbuf_lock, flags);
  665. wake_klogd |= log_start - log_end;
  666. if (con_start == log_end)
  667. break; /* Nothing to print */
  668. _con_start = con_start;
  669. _log_end = log_end;
  670. con_start = log_end; /* Flush */
  671. spin_unlock(&logbuf_lock);
  672. call_console_drivers(_con_start, _log_end);
  673. local_irq_restore(flags);
  674. }
  675. console_locked = 0;
  676. console_may_schedule = 0;
  677. up(&console_sem);
  678. spin_unlock_irqrestore(&logbuf_lock, flags);
  679. if (wake_klogd && !oops_in_progress && waitqueue_active(&log_wait))
  680. wake_up_interruptible(&log_wait);
  681. }
  682. EXPORT_SYMBOL(release_console_sem);
  683. /** console_conditional_schedule - yield the CPU if required
  684. *
  685. * If the console code is currently allowed to sleep, and
  686. * if this CPU should yield the CPU to another task, do
  687. * so here.
  688. *
  689. * Must be called within acquire_console_sem().
  690. */
  691. void __sched console_conditional_schedule(void)
  692. {
  693. if (console_may_schedule)
  694. cond_resched();
  695. }
  696. EXPORT_SYMBOL(console_conditional_schedule);
  697. void console_print(const char *s)
  698. {
  699. printk(KERN_EMERG "%s", s);
  700. }
  701. EXPORT_SYMBOL(console_print);
  702. void console_unblank(void)
  703. {
  704. struct console *c;
  705. /*
  706. * console_unblank can no longer be called in interrupt context unless
  707. * oops_in_progress is set to 1..
  708. */
  709. if (oops_in_progress) {
  710. if (down_trylock(&console_sem) != 0)
  711. return;
  712. } else
  713. acquire_console_sem();
  714. console_locked = 1;
  715. console_may_schedule = 0;
  716. for (c = console_drivers; c != NULL; c = c->next)
  717. if ((c->flags & CON_ENABLED) && c->unblank)
  718. c->unblank();
  719. release_console_sem();
  720. }
  721. EXPORT_SYMBOL(console_unblank);
  722. /*
  723. * Return the console tty driver structure and its associated index
  724. */
  725. struct tty_driver *console_device(int *index)
  726. {
  727. struct console *c;
  728. struct tty_driver *driver = NULL;
  729. acquire_console_sem();
  730. for (c = console_drivers; c != NULL; c = c->next) {
  731. if (!c->device)
  732. continue;
  733. driver = c->device(c, index);
  734. if (driver)
  735. break;
  736. }
  737. release_console_sem();
  738. return driver;
  739. }
  740. /*
  741. * Prevent further output on the passed console device so that (for example)
  742. * serial drivers can disable console output before suspending a port, and can
  743. * re-enable output afterwards.
  744. */
  745. void console_stop(struct console *console)
  746. {
  747. acquire_console_sem();
  748. console->flags &= ~CON_ENABLED;
  749. release_console_sem();
  750. }
  751. EXPORT_SYMBOL(console_stop);
  752. void console_start(struct console *console)
  753. {
  754. acquire_console_sem();
  755. console->flags |= CON_ENABLED;
  756. release_console_sem();
  757. }
  758. EXPORT_SYMBOL(console_start);
  759. /*
  760. * The console driver calls this routine during kernel initialization
  761. * to register the console printing procedure with printk() and to
  762. * print any messages that were printed by the kernel before the
  763. * console driver was initialized.
  764. */
  765. void register_console(struct console * console)
  766. {
  767. int i;
  768. unsigned long flags;
  769. if (preferred_console < 0)
  770. preferred_console = selected_console;
  771. /*
  772. * See if we want to use this console driver. If we
  773. * didn't select a console we take the first one
  774. * that registers here.
  775. */
  776. if (preferred_console < 0) {
  777. if (console->index < 0)
  778. console->index = 0;
  779. if (console->setup == NULL ||
  780. console->setup(console, NULL) == 0) {
  781. console->flags |= CON_ENABLED | CON_CONSDEV;
  782. preferred_console = 0;
  783. }
  784. }
  785. /*
  786. * See if this console matches one we selected on
  787. * the command line.
  788. */
  789. for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) {
  790. if (strcmp(console_cmdline[i].name, console->name) != 0)
  791. continue;
  792. if (console->index >= 0 &&
  793. console->index != console_cmdline[i].index)
  794. continue;
  795. if (console->index < 0)
  796. console->index = console_cmdline[i].index;
  797. if (console->setup &&
  798. console->setup(console, console_cmdline[i].options) != 0)
  799. break;
  800. console->flags |= CON_ENABLED;
  801. console->index = console_cmdline[i].index;
  802. if (i == selected_console) {
  803. console->flags |= CON_CONSDEV;
  804. preferred_console = selected_console;
  805. }
  806. break;
  807. }
  808. if (!(console->flags & CON_ENABLED))
  809. return;
  810. if (console_drivers && (console_drivers->flags & CON_BOOT)) {
  811. unregister_console(console_drivers);
  812. console->flags &= ~CON_PRINTBUFFER;
  813. }
  814. /*
  815. * Put this console in the list - keep the
  816. * preferred driver at the head of the list.
  817. */
  818. acquire_console_sem();
  819. if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
  820. console->next = console_drivers;
  821. console_drivers = console;
  822. if (console->next)
  823. console->next->flags &= ~CON_CONSDEV;
  824. } else {
  825. console->next = console_drivers->next;
  826. console_drivers->next = console;
  827. }
  828. if (console->flags & CON_PRINTBUFFER) {
  829. /*
  830. * release_console_sem() will print out the buffered messages
  831. * for us.
  832. */
  833. spin_lock_irqsave(&logbuf_lock, flags);
  834. con_start = log_start;
  835. spin_unlock_irqrestore(&logbuf_lock, flags);
  836. }
  837. release_console_sem();
  838. }
  839. EXPORT_SYMBOL(register_console);
  840. int unregister_console(struct console * console)
  841. {
  842. struct console *a,*b;
  843. int res = 1;
  844. acquire_console_sem();
  845. if (console_drivers == console) {
  846. console_drivers=console->next;
  847. res = 0;
  848. } else {
  849. for (a=console_drivers->next, b=console_drivers ;
  850. a; b=a, a=b->next) {
  851. if (a == console) {
  852. b->next = a->next;
  853. res = 0;
  854. break;
  855. }
  856. }
  857. }
  858. /* If last console is removed, we re-enable picking the first
  859. * one that gets registered. Without that, pmac early boot console
  860. * would prevent fbcon from taking over.
  861. *
  862. * If this isn't the last console and it has CON_CONSDEV set, we
  863. * need to set it on the next preferred console.
  864. */
  865. if (console_drivers == NULL)
  866. preferred_console = selected_console;
  867. else if (console->flags & CON_CONSDEV)
  868. console_drivers->flags |= CON_CONSDEV;
  869. release_console_sem();
  870. return res;
  871. }
  872. EXPORT_SYMBOL(unregister_console);
  873. /**
  874. * tty_write_message - write a message to a certain tty, not just the console.
  875. *
  876. * This is used for messages that need to be redirected to a specific tty.
  877. * We don't put it into the syslog queue right now maybe in the future if
  878. * really needed.
  879. */
  880. void tty_write_message(struct tty_struct *tty, char *msg)
  881. {
  882. if (tty && tty->driver->write)
  883. tty->driver->write(tty, msg, strlen(msg));
  884. return;
  885. }
  886. /*
  887. * printk rate limiting, lifted from the networking subsystem.
  888. *
  889. * This enforces a rate limit: not more than one kernel message
  890. * every printk_ratelimit_jiffies to make a denial-of-service
  891. * attack impossible.
  892. */
  893. int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
  894. {
  895. static DEFINE_SPINLOCK(ratelimit_lock);
  896. static unsigned long toks = 10*5*HZ;
  897. static unsigned long last_msg;
  898. static int missed;
  899. unsigned long flags;
  900. unsigned long now = jiffies;
  901. spin_lock_irqsave(&ratelimit_lock, flags);
  902. toks += now - last_msg;
  903. last_msg = now;
  904. if (toks > (ratelimit_burst * ratelimit_jiffies))
  905. toks = ratelimit_burst * ratelimit_jiffies;
  906. if (toks >= ratelimit_jiffies) {
  907. int lost = missed;
  908. missed = 0;
  909. toks -= ratelimit_jiffies;
  910. spin_unlock_irqrestore(&ratelimit_lock, flags);
  911. if (lost)
  912. printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
  913. return 1;
  914. }
  915. missed++;
  916. spin_unlock_irqrestore(&ratelimit_lock, flags);
  917. return 0;
  918. }
  919. EXPORT_SYMBOL(__printk_ratelimit);
  920. /* minimum time in jiffies between messages */
  921. int printk_ratelimit_jiffies = 5*HZ;
  922. /* number of messages we send before ratelimiting */
  923. int printk_ratelimit_burst = 10;
  924. int printk_ratelimit(void)
  925. {
  926. return __printk_ratelimit(printk_ratelimit_jiffies,
  927. printk_ratelimit_burst);
  928. }
  929. EXPORT_SYMBOL(printk_ratelimit);