asterisk.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * General Definitions for Asterisk top level program
  5. *
  6. * Copyright (C) 1999-2006, Digium, Inc.
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. /*! \file
  14. * \brief Asterisk main include file. File version handling, generic pbx functions.
  15. */
  16. #ifndef _ASTERISK_H
  17. #define _ASTERISK_H
  18. #include "asterisk/autoconfig.h"
  19. #if !defined(NO_MALLOC_DEBUG) && !defined(STANDALONE) && !defined(STANDALONE2) && defined(MALLOC_DEBUG)
  20. #include "asterisk/astmm.h"
  21. #endif
  22. #include "asterisk/compat.h"
  23. /* Default to allowing the umask or filesystem ACLs to determine actual file
  24. * creation permissions
  25. */
  26. #ifndef AST_DIR_MODE
  27. #define AST_DIR_MODE 0777
  28. #endif
  29. #ifndef AST_FILE_MODE
  30. #define AST_FILE_MODE 0666
  31. #endif
  32. /* Make sure PATH_MAX is defined on platforms (HURD) that don't define it.
  33. * Also be sure to handle the case of a path larger than PATH_MAX
  34. * (err safely) in the code.
  35. */
  36. #ifndef PATH_MAX
  37. #define PATH_MAX 4096
  38. #endif
  39. #define DEFAULT_LANGUAGE "en"
  40. #define DEFAULT_SAMPLE_RATE 8000
  41. #define DEFAULT_SAMPLES_PER_MS ((DEFAULT_SAMPLE_RATE)/1000)
  42. #define setpriority __PLEASE_USE_ast_set_priority_INSTEAD_OF_setpriority__
  43. #define sched_setscheduler __PLEASE_USE_ast_set_priority_INSTEAD_OF_sched_setscheduler__
  44. #if defined(DEBUG_FD_LEAKS) && !defined(STANDALONE) && !defined(STANDALONE2) && !defined(STANDALONE_AEL)
  45. /* These includes are all about ordering */
  46. #include <stdio.h>
  47. #include <sys/types.h>
  48. #include <sys/stat.h>
  49. #include <sys/socket.h>
  50. #include <fcntl.h>
  51. #define open(a,...) __ast_fdleak_open(__FILE__,__LINE__,__PRETTY_FUNCTION__, a, __VA_ARGS__)
  52. #define pipe(a) __ast_fdleak_pipe(a, __FILE__,__LINE__,__PRETTY_FUNCTION__)
  53. #define socket(a,b,c) __ast_fdleak_socket(a, b, c, __FILE__,__LINE__,__PRETTY_FUNCTION__)
  54. #define close(a) __ast_fdleak_close(a)
  55. #define fopen(a,b) __ast_fdleak_fopen(a, b, __FILE__,__LINE__,__PRETTY_FUNCTION__)
  56. #define fclose(a) __ast_fdleak_fclose(a)
  57. #define dup2(a,b) __ast_fdleak_dup2(a, b, __FILE__,__LINE__,__PRETTY_FUNCTION__)
  58. #define dup(a) __ast_fdleak_dup(a, __FILE__,__LINE__,__PRETTY_FUNCTION__)
  59. #if defined(__cplusplus) || defined(c_plusplus)
  60. extern "C" {
  61. #endif
  62. int __ast_fdleak_open(const char *file, int line, const char *func, const char *path, int flags, ...);
  63. int __ast_fdleak_pipe(int *fds, const char *file, int line, const char *func);
  64. int __ast_fdleak_socket(int domain, int type, int protocol, const char *file, int line, const char *func);
  65. int __ast_fdleak_close(int fd);
  66. FILE *__ast_fdleak_fopen(const char *path, const char *mode, const char *file, int line, const char *func);
  67. int __ast_fdleak_fclose(FILE *ptr);
  68. int __ast_fdleak_dup2(int oldfd, int newfd, const char *file, int line, const char *func);
  69. int __ast_fdleak_dup(int oldfd, const char *file, int line, const char *func);
  70. #if defined(__cplusplus) || defined(c_plusplus)
  71. }
  72. #endif
  73. #endif
  74. int ast_set_priority(int); /*!< Provided by asterisk.c */
  75. int ast_fd_init(void); /*!< Provided by astfd.c */
  76. int ast_pbx_init(void); /*!< Provided by pbx.c */
  77. /*!
  78. * \brief Register a function to be executed before Asterisk exits.
  79. * \param func The callback function to use.
  80. *
  81. * \retval 0 on success.
  82. * \retval -1 on error.
  83. *
  84. * \note This function should be rarely used in situations where
  85. * something must be shutdown to avoid corruption, excessive data
  86. * loss, or when external programs must be stopped. All other
  87. * cleanup in the core should use ast_register_cleanup.
  88. */
  89. int ast_register_atexit(void (*func)(void));
  90. /*!
  91. * \since 11.9
  92. * \brief Register a function to be executed before Asterisk gracefully exits.
  93. *
  94. * If Asterisk is immediately shutdown (core stop now, or sending the TERM
  95. * signal), the callback is not run. When the callbacks are run, they are run in
  96. * sequence with ast_register_atexit() callbacks, in the reverse order of
  97. * registration.
  98. *
  99. * \param func The callback function to use.
  100. *
  101. * \retval 0 on success.
  102. * \retval -1 on error.
  103. */
  104. int ast_register_cleanup(void (*func)(void));
  105. /*!
  106. * \brief Unregister a function registered with ast_register_atexit().
  107. * \param func The callback function to unregister.
  108. */
  109. void ast_unregister_atexit(void (*func)(void));
  110. /*!
  111. * \brief Cancel an existing shutdown and return to normal operation.
  112. *
  113. * \note Shutdown can be cancelled while the server is waiting for
  114. * any existing channels to be destroyed before shutdown becomes
  115. * irreversible.
  116. *
  117. * \return non-zero if shutdown cancelled.
  118. */
  119. int ast_cancel_shutdown(void);
  120. /*!
  121. * \details
  122. * The server is preventing new channel creation in preparation for
  123. * shutdown and may actively be releasing resources. The shutdown
  124. * process may be canceled by ast_cancel_shutdown() if it is not too
  125. * late.
  126. *
  127. * \note The preparation to shutdown phase can be quite lengthy
  128. * if we are gracefully shutting down. How long existing calls will
  129. * last is not up to us.
  130. *
  131. * \return non-zero if the server is preparing to or actively shutting down.
  132. */
  133. int ast_shutting_down(void);
  134. /*!
  135. * \return non-zero if the server is actively shutting down.
  136. * \since 13.3.0
  137. *
  138. * \details
  139. * The server is releasing resources and unloading modules.
  140. * It won't be long now.
  141. */
  142. int ast_shutdown_final(void);
  143. #if !defined(LOW_MEMORY)
  144. /*!
  145. * \brief Register the version of a source code file with the core.
  146. * \param file the source file name
  147. * \param version the version string (typically a SVN revision keyword string)
  148. * \return nothing
  149. *
  150. * \note As of 13.4.0, the \c version parameter is ignored.
  151. *
  152. * This function should not be called directly, but instead the
  153. * ASTERISK_FILE_VERSION macro should be used to register a file with the core.
  154. */
  155. void ast_register_file_version(const char *file, const char *version);
  156. /*!
  157. * \brief Unregister a source code file from the core.
  158. * \param file the source file name
  159. * \return nothing
  160. *
  161. * This function should not be called directly, but instead the
  162. * ASTERISK_FILE_VERSION macro should be used to automatically unregister
  163. * the file when the module is unloaded.
  164. */
  165. void ast_unregister_file_version(const char *file);
  166. /*!
  167. * \brief Find version for given module name
  168. * \param file Module name (i.e. chan_sip.so)
  169. *
  170. * \note As of 13.4.0, the file version is no longer tracked. As such,
  171. * if the file exists, the Asterisk version will be returned.
  172. *
  173. * \retval NULL if the file doesn't exist.
  174. * \retval The Asterisk version if the file does exist.
  175. */
  176. const char *ast_file_version_find(const char *file);
  177. /*!
  178. * \brief Complete a source file name
  179. * \param partial The partial name of the file to look up.
  180. * \param n The n-th match to return.
  181. *
  182. * \retval NULL if there is no match for partial at the n-th position
  183. * \retval Matching source file name
  184. *
  185. * \note A matching source file is allocataed on the heap, and must be
  186. * free'd by the caller.
  187. */
  188. char *ast_complete_source_filename(const char *partial, int n);
  189. /*!
  190. * \brief Register/unregister a source code file with the core.
  191. * \param file the source file name
  192. * \param version the version string (typically a SVN revision keyword string)
  193. *
  194. * This macro will place a file-scope constructor and destructor into the
  195. * source of the module using it; this will cause the version of this file
  196. * to registered with the Asterisk core (and unregistered) at the appropriate
  197. * times.
  198. *
  199. * Example:
  200. *
  201. * \code
  202. * ASTERISK_FILE_VERSION(__FILE__, "\$Revision\$")
  203. * \endcode
  204. *
  205. * \note The dollar signs above have been protected with backslashes to keep
  206. * SVN from modifying them in this file; under normal circumstances they would
  207. * not be present and SVN would expand the Revision keyword into the file's
  208. * revision number.
  209. *
  210. * \deprecated All new files should use ASTERISK_REGISTER_FILE instead.
  211. * \version 11.22.0 deprecated
  212. */
  213. #ifdef MTX_PROFILE
  214. #define HAVE_MTX_PROFILE /* used in lock.h */
  215. #define ASTERISK_FILE_VERSION(file, version) \
  216. static int mtx_prof = -1; /* profile mutex */ \
  217. static void __attribute__((constructor)) __register_file_version(void) \
  218. { \
  219. mtx_prof = ast_add_profile("mtx_lock_" file, 0); \
  220. ast_register_file_version(file, version); \
  221. } \
  222. static void __attribute__((destructor)) __unregister_file_version(void) \
  223. { \
  224. ast_unregister_file_version(file); \
  225. }
  226. #else /* !MTX_PROFILE */
  227. #define ASTERISK_FILE_VERSION(file, version) \
  228. static void __attribute__((constructor)) __register_file_version(void) \
  229. { \
  230. ast_register_file_version(file, version); \
  231. } \
  232. static void __attribute__((destructor)) __unregister_file_version(void) \
  233. { \
  234. ast_unregister_file_version(file); \
  235. }
  236. #endif /* !MTX_PROFILE */
  237. #else /* LOW_MEMORY */
  238. #define ASTERISK_FILE_VERSION(file, x)
  239. #endif /* LOW_MEMORY */
  240. /*!
  241. * \since 11.22.0
  242. * \brief Register/unregister a source code file with the core.
  243. *
  244. * This macro will place a file-scope constructor and destructor into the
  245. * source of the module using it; this will cause the file to be
  246. * registered with the Asterisk core (and unregistered) at the appropriate
  247. * times.
  248. *
  249. * Example:
  250. *
  251. * \code
  252. * ASTERISK_REGISTER_FILE()
  253. * \endcode
  254. */
  255. #define ASTERISK_REGISTER_FILE() ASTERISK_FILE_VERSION(__FILE__, NULL)
  256. #if !defined(LOW_MEMORY)
  257. /*!
  258. * \brief support for event profiling
  259. *
  260. * (note, this must be documented a lot more)
  261. * ast_add_profile allocates a generic 'counter' with a given name,
  262. * which can be shown with the command 'core show profile &lt;name&gt;'
  263. *
  264. * The counter accumulates positive or negative values supplied by
  265. * \see ast_add_profile(), dividing them by the 'scale' value passed in the
  266. * create call, and also counts the number of 'events'.
  267. * Values can also be taked by the TSC counter on ia32 architectures,
  268. * in which case you can mark the start of an event calling ast_mark(id, 1)
  269. * and then the end of the event with ast_mark(id, 0).
  270. * For non-i386 architectures, these two calls return 0.
  271. */
  272. int ast_add_profile(const char *, uint64_t scale);
  273. int64_t ast_profile(int, int64_t);
  274. int64_t ast_mark(int, int start1_stop0);
  275. #else /* LOW_MEMORY */
  276. #define ast_add_profile(a, b) 0
  277. #define ast_profile(a, b) do { } while (0)
  278. #define ast_mark(a, b) do { } while (0)
  279. #endif /* LOW_MEMORY */
  280. /*! \brief
  281. * Definition of various structures that many asterisk files need,
  282. * but only because they need to know that the type exists.
  283. *
  284. */
  285. struct ast_channel;
  286. struct ast_frame;
  287. struct ast_module;
  288. struct ast_variable;
  289. struct ast_str;
  290. struct ast_sched_context;
  291. struct ast_json;
  292. /* Some handy macros for turning a preprocessor token into (effectively) a quoted string */
  293. #define __stringify_1(x) #x
  294. #define __stringify(x) __stringify_1(x)
  295. /*!
  296. * \brief Retrieve the PBX UUID
  297. * \param pbx_uuid A buffer of at least AST_UUID_STR_LEN (36 + 1) size to receive the UUID
  298. * \param length The buffer length
  299. */
  300. int ast_pbx_uuid_get(char *pbx_uuid, int length);
  301. #endif /* _ASTERISK_H */