/src/os/linux/ftk_linux.c

http://ftk.googlecode.com/ · C · 113 lines · 67 code · 17 blank · 29 comment · 3 complexity · bcc4ca30442ccae87912dbc06060d1cd MD5 · raw file

  1. /*
  2. * File: ftk_linux.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: linux specific functions.
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2009-11-17 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include <signal.h>
  31. #include "ftk_typedef.h"
  32. #ifdef HAVE_EXECINFO_H
  33. #include <execinfo.h>
  34. #endif
  35. static void signal_handler(int sig)
  36. {
  37. #ifdef HAVE_EXECINFO_H
  38. int i = 0;
  39. void* buffer[100] = {0};
  40. int n = backtrace(buffer, 100);
  41. const char** symbols = (const char**)backtrace_symbols(buffer, n);
  42. for(i = 0; i < n; i++)
  43. {
  44. printf("%p: %s\n", buffer[i], symbols[i]);
  45. }
  46. fflush(stdout);
  47. signal(SIGABRT, NULL);
  48. signal(SIGSEGV, NULL);
  49. signal(SIGILL, NULL);
  50. #endif
  51. _exit(0);
  52. return;
  53. }
  54. static void signal_int_handler(int sig)
  55. {
  56. exit(0);
  57. return;
  58. }
  59. static void ftk_install_crash_signal(void)
  60. {
  61. signal(SIGABRT, signal_handler);
  62. signal(SIGSEGV, signal_handler);
  63. signal(SIGILL, signal_handler);
  64. signal(SIGINT, signal_int_handler);
  65. return ;
  66. }
  67. static int ftk_set_tty_mode(int graphics)
  68. {
  69. int r = 0;
  70. #if !defined(PC_EMU) && defined(USE_LINUX_NATIVE)
  71. int fd = 0;
  72. fd = open("/dev/tty0", O_RDWR | O_SYNC);
  73. if (fd < 0)
  74. return -1;
  75. r = ioctl(fd, KDSETMODE, (void*) (graphics ? KD_GRAPHICS : KD_TEXT));
  76. close(fd);
  77. #endif
  78. return r;
  79. }
  80. size_t ftk_get_relative_time(void)
  81. {
  82. struct timeval now = {0};
  83. gettimeofday(&now, NULL);
  84. return now.tv_sec*1000 + now.tv_usec/1000;
  85. }
  86. int ftk_platform_init(int argc, char** argv)
  87. {
  88. ftk_set_tty_mode(1);
  89. ftk_install_crash_signal();
  90. return RET_OK;
  91. }
  92. void ftk_platform_deinit(void)
  93. {
  94. ftk_set_tty_mode(0);
  95. return;
  96. }