PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/samba-3.5.6/source3/lib/messages_local.c

https://github.com/theuni/XBMC-deps
C | 462 lines | 279 code | 91 blank | 92 comment | 45 complexity | b3a468a72439a20263c3eb3b9305ace1 MD5 | raw file
  1. /*
  2. Unix SMB/CIFS implementation.
  3. Samba internal messaging functions
  4. Copyright (C) 2007 by Volker Lendecke
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /**
  17. @defgroup messages Internal messaging framework
  18. @{
  19. @file messages.c
  20. @brief Module for internal messaging between Samba daemons.
  21. The idea is that if a part of Samba wants to do communication with
  22. another Samba process then it will do a message_register() of a
  23. dispatch function, and use message_send_pid() to send messages to
  24. that process.
  25. The dispatch function is given the pid of the sender, and it can
  26. use that to reply by message_send_pid(). See ping_message() for a
  27. simple example.
  28. @caution Dispatch functions must be able to cope with incoming
  29. messages on an *odd* byte boundary.
  30. This system doesn't have any inherent size limitations but is not
  31. very efficient for large messages or when messages are sent in very
  32. quick succession.
  33. */
  34. #include "includes.h"
  35. #include "librpc/gen_ndr/messaging.h"
  36. #include "librpc/gen_ndr/ndr_messaging.h"
  37. struct messaging_tdb_context {
  38. struct messaging_context *msg_ctx;
  39. struct tdb_wrap *tdb;
  40. struct tevent_signal *se;
  41. int received_messages;
  42. };
  43. static NTSTATUS messaging_tdb_send(struct messaging_context *msg_ctx,
  44. struct server_id pid, int msg_type,
  45. const DATA_BLOB *data,
  46. struct messaging_backend *backend);
  47. static void message_dispatch(struct messaging_context *msg_ctx);
  48. static void messaging_tdb_signal_handler(struct tevent_context *ev_ctx,
  49. struct tevent_signal *se,
  50. int signum, int count,
  51. void *_info, void *private_data)
  52. {
  53. struct messaging_tdb_context *ctx = talloc_get_type(private_data,
  54. struct messaging_tdb_context);
  55. ctx->received_messages++;
  56. DEBUG(10, ("messaging_tdb_signal_handler: sig[%d] count[%d] msgs[%d]\n",
  57. signum, count, ctx->received_messages));
  58. message_dispatch(ctx->msg_ctx);
  59. }
  60. /****************************************************************************
  61. Initialise the messaging functions.
  62. ****************************************************************************/
  63. NTSTATUS messaging_tdb_init(struct messaging_context *msg_ctx,
  64. TALLOC_CTX *mem_ctx,
  65. struct messaging_backend **presult)
  66. {
  67. struct messaging_backend *result;
  68. struct messaging_tdb_context *ctx;
  69. if (!(result = TALLOC_P(mem_ctx, struct messaging_backend))) {
  70. DEBUG(0, ("talloc failed\n"));
  71. return NT_STATUS_NO_MEMORY;
  72. }
  73. ctx = TALLOC_ZERO_P(result, struct messaging_tdb_context);
  74. if (!ctx) {
  75. DEBUG(0, ("talloc failed\n"));
  76. TALLOC_FREE(result);
  77. return NT_STATUS_NO_MEMORY;
  78. }
  79. result->private_data = ctx;
  80. result->send_fn = messaging_tdb_send;
  81. ctx->msg_ctx = msg_ctx;
  82. ctx->tdb = tdb_wrap_open(ctx, lock_path("messages.tdb"), 0,
  83. TDB_CLEAR_IF_FIRST|TDB_DEFAULT|TDB_VOLATILE,
  84. O_RDWR|O_CREAT,0600);
  85. if (!ctx->tdb) {
  86. NTSTATUS status = map_nt_error_from_unix(errno);
  87. DEBUG(0, ("ERROR: Failed to initialise messages database: "
  88. "%s\n", strerror(errno)));
  89. TALLOC_FREE(result);
  90. return status;
  91. }
  92. ctx->se = tevent_add_signal(msg_ctx->event_ctx,
  93. ctx,
  94. SIGUSR1, 0,
  95. messaging_tdb_signal_handler,
  96. ctx);
  97. if (!ctx->se) {
  98. NTSTATUS status = map_nt_error_from_unix(errno);
  99. DEBUG(0, ("ERROR: Failed to initialise messages signal handler: "
  100. "%s\n", strerror(errno)));
  101. TALLOC_FREE(result);
  102. return status;
  103. }
  104. sec_init();
  105. *presult = result;
  106. return NT_STATUS_OK;
  107. }
  108. /*******************************************************************
  109. Form a static tdb key from a pid.
  110. ******************************************************************/
  111. static TDB_DATA message_key_pid(TALLOC_CTX *mem_ctx, struct server_id pid)
  112. {
  113. char *key;
  114. TDB_DATA kbuf;
  115. key = talloc_asprintf(talloc_tos(), "PID/%s", procid_str_static(&pid));
  116. SMB_ASSERT(key != NULL);
  117. kbuf.dptr = (uint8 *)key;
  118. kbuf.dsize = strlen(key)+1;
  119. return kbuf;
  120. }
  121. /*
  122. Fetch the messaging array for a process
  123. */
  124. static NTSTATUS messaging_tdb_fetch(TDB_CONTEXT *msg_tdb,
  125. TDB_DATA key,
  126. TALLOC_CTX *mem_ctx,
  127. struct messaging_array **presult)
  128. {
  129. struct messaging_array *result;
  130. TDB_DATA data;
  131. DATA_BLOB blob;
  132. enum ndr_err_code ndr_err;
  133. if (!(result = TALLOC_ZERO_P(mem_ctx, struct messaging_array))) {
  134. return NT_STATUS_NO_MEMORY;
  135. }
  136. data = tdb_fetch(msg_tdb, key);
  137. if (data.dptr == NULL) {
  138. *presult = result;
  139. return NT_STATUS_OK;
  140. }
  141. blob = data_blob_const(data.dptr, data.dsize);
  142. ndr_err = ndr_pull_struct_blob(
  143. &blob, result, NULL, result,
  144. (ndr_pull_flags_fn_t)ndr_pull_messaging_array);
  145. SAFE_FREE(data.dptr);
  146. if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
  147. TALLOC_FREE(result);
  148. return ndr_map_error2ntstatus(ndr_err);
  149. }
  150. if (DEBUGLEVEL >= 10) {
  151. DEBUG(10, ("messaging_tdb_fetch:\n"));
  152. NDR_PRINT_DEBUG(messaging_array, result);
  153. }
  154. *presult = result;
  155. return NT_STATUS_OK;
  156. }
  157. /*
  158. Store a messaging array for a pid
  159. */
  160. static NTSTATUS messaging_tdb_store(TDB_CONTEXT *msg_tdb,
  161. TDB_DATA key,
  162. struct messaging_array *array)
  163. {
  164. TDB_DATA data;
  165. DATA_BLOB blob;
  166. enum ndr_err_code ndr_err;
  167. TALLOC_CTX *mem_ctx;
  168. int ret;
  169. if (array->num_messages == 0) {
  170. tdb_delete(msg_tdb, key);
  171. return NT_STATUS_OK;
  172. }
  173. if (!(mem_ctx = talloc_new(array))) {
  174. return NT_STATUS_NO_MEMORY;
  175. }
  176. ndr_err = ndr_push_struct_blob(
  177. &blob, mem_ctx, NULL, array,
  178. (ndr_push_flags_fn_t)ndr_push_messaging_array);
  179. if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
  180. talloc_free(mem_ctx);
  181. return ndr_map_error2ntstatus(ndr_err);
  182. }
  183. if (DEBUGLEVEL >= 10) {
  184. DEBUG(10, ("messaging_tdb_store:\n"));
  185. NDR_PRINT_DEBUG(messaging_array, array);
  186. }
  187. data.dptr = blob.data;
  188. data.dsize = blob.length;
  189. ret = tdb_store(msg_tdb, key, data, TDB_REPLACE);
  190. TALLOC_FREE(mem_ctx);
  191. return (ret == 0) ? NT_STATUS_OK : NT_STATUS_INTERNAL_DB_CORRUPTION;
  192. }
  193. /****************************************************************************
  194. Notify a process that it has a message. If the process doesn't exist
  195. then delete its record in the database.
  196. ****************************************************************************/
  197. static NTSTATUS message_notify(struct server_id procid)
  198. {
  199. pid_t pid = procid.pid;
  200. int ret;
  201. uid_t euid = geteuid();
  202. /*
  203. * Doing kill with a non-positive pid causes messages to be
  204. * sent to places we don't want.
  205. */
  206. SMB_ASSERT(pid > 0);
  207. if (euid != 0) {
  208. /* If we're not root become so to send the message. */
  209. save_re_uid();
  210. set_effective_uid(0);
  211. }
  212. ret = kill(pid, SIGUSR1);
  213. if (euid != 0) {
  214. /* Go back to who we were. */
  215. int saved_errno = errno;
  216. restore_re_uid_fromroot();
  217. errno = saved_errno;
  218. }
  219. if (ret == 0) {
  220. return NT_STATUS_OK;
  221. }
  222. /*
  223. * Something has gone wrong
  224. */
  225. DEBUG(2,("message to process %d failed - %s\n", (int)pid,
  226. strerror(errno)));
  227. /*
  228. * No call to map_nt_error_from_unix -- don't want to link in
  229. * errormap.o into lots of utils.
  230. */
  231. if (errno == ESRCH) return NT_STATUS_INVALID_HANDLE;
  232. if (errno == EINVAL) return NT_STATUS_INVALID_PARAMETER;
  233. if (errno == EPERM) return NT_STATUS_ACCESS_DENIED;
  234. return NT_STATUS_UNSUCCESSFUL;
  235. }
  236. /****************************************************************************
  237. Send a message to a particular pid.
  238. ****************************************************************************/
  239. static NTSTATUS messaging_tdb_send(struct messaging_context *msg_ctx,
  240. struct server_id pid, int msg_type,
  241. const DATA_BLOB *data,
  242. struct messaging_backend *backend)
  243. {
  244. struct messaging_tdb_context *ctx = talloc_get_type(backend->private_data,
  245. struct messaging_tdb_context);
  246. struct messaging_array *msg_array;
  247. struct messaging_rec *rec;
  248. NTSTATUS status;
  249. TDB_DATA key;
  250. struct tdb_wrap *tdb = ctx->tdb;
  251. TALLOC_CTX *frame = talloc_stackframe();
  252. /* NULL pointer means implicit length zero. */
  253. if (!data->data) {
  254. SMB_ASSERT(data->length == 0);
  255. }
  256. /*
  257. * Doing kill with a non-positive pid causes messages to be
  258. * sent to places we don't want.
  259. */
  260. SMB_ASSERT(procid_to_pid(&pid) > 0);
  261. key = message_key_pid(frame, pid);
  262. if (tdb_chainlock(tdb->tdb, key) == -1) {
  263. TALLOC_FREE(frame);
  264. return NT_STATUS_LOCK_NOT_GRANTED;
  265. }
  266. status = messaging_tdb_fetch(tdb->tdb, key, talloc_tos(), &msg_array);
  267. if (!NT_STATUS_IS_OK(status)) {
  268. goto done;
  269. }
  270. if ((msg_type & MSG_FLAG_LOWPRIORITY)
  271. && (msg_array->num_messages > 1000)) {
  272. DEBUG(5, ("Dropping message for PID %s\n",
  273. procid_str_static(&pid)));
  274. status = NT_STATUS_INSUFFICIENT_RESOURCES;
  275. goto done;
  276. }
  277. if (!(rec = TALLOC_REALLOC_ARRAY(talloc_tos(), msg_array->messages,
  278. struct messaging_rec,
  279. msg_array->num_messages+1))) {
  280. status = NT_STATUS_NO_MEMORY;
  281. goto done;
  282. }
  283. rec[msg_array->num_messages].msg_version = MESSAGE_VERSION;
  284. rec[msg_array->num_messages].msg_type = msg_type & MSG_TYPE_MASK;
  285. rec[msg_array->num_messages].dest = pid;
  286. rec[msg_array->num_messages].src = procid_self();
  287. rec[msg_array->num_messages].buf = *data;
  288. msg_array->messages = rec;
  289. msg_array->num_messages += 1;
  290. status = messaging_tdb_store(tdb->tdb, key, msg_array);
  291. if (!NT_STATUS_IS_OK(status)) {
  292. goto done;
  293. }
  294. status = message_notify(pid);
  295. if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
  296. DEBUG(2, ("pid %s doesn't exist - deleting messages record\n",
  297. procid_str_static(&pid)));
  298. tdb_delete(tdb->tdb, message_key_pid(talloc_tos(), pid));
  299. }
  300. done:
  301. tdb_chainunlock(tdb->tdb, key);
  302. TALLOC_FREE(frame);
  303. return status;
  304. }
  305. /****************************************************************************
  306. Retrieve all messages for the current process.
  307. ****************************************************************************/
  308. static NTSTATUS retrieve_all_messages(TDB_CONTEXT *msg_tdb,
  309. TALLOC_CTX *mem_ctx,
  310. struct messaging_array **presult)
  311. {
  312. struct messaging_array *result;
  313. TDB_DATA key = message_key_pid(mem_ctx, procid_self());
  314. NTSTATUS status;
  315. if (tdb_chainlock(msg_tdb, key) == -1) {
  316. TALLOC_FREE(key.dptr);
  317. return NT_STATUS_LOCK_NOT_GRANTED;
  318. }
  319. status = messaging_tdb_fetch(msg_tdb, key, mem_ctx, &result);
  320. /*
  321. * We delete the record here, tdb_set_max_dead keeps it around
  322. */
  323. tdb_delete(msg_tdb, key);
  324. tdb_chainunlock(msg_tdb, key);
  325. if (NT_STATUS_IS_OK(status)) {
  326. *presult = result;
  327. }
  328. TALLOC_FREE(key.dptr);
  329. return status;
  330. }
  331. /****************************************************************************
  332. Receive and dispatch any messages pending for this process.
  333. JRA changed Dec 13 2006. Only one message handler now permitted per type.
  334. *NOTE*: Dispatch functions must be able to cope with incoming
  335. messages on an *odd* byte boundary.
  336. ****************************************************************************/
  337. static void message_dispatch(struct messaging_context *msg_ctx)
  338. {
  339. struct messaging_tdb_context *ctx = talloc_get_type(msg_ctx->local->private_data,
  340. struct messaging_tdb_context);
  341. struct messaging_array *msg_array = NULL;
  342. struct tdb_wrap *tdb = ctx->tdb;
  343. NTSTATUS status;
  344. uint32 i;
  345. if (ctx->received_messages == 0) {
  346. return;
  347. }
  348. DEBUG(10, ("message_dispatch: received_messages = %d\n",
  349. ctx->received_messages));
  350. status = retrieve_all_messages(tdb->tdb, NULL, &msg_array);
  351. if (!NT_STATUS_IS_OK(status)) {
  352. DEBUG(0, ("message_dispatch: failed to retrieve messages: %s\n",
  353. nt_errstr(status)));
  354. return;
  355. }
  356. ctx->received_messages = 0;
  357. for (i=0; i<msg_array->num_messages; i++) {
  358. messaging_dispatch_rec(msg_ctx, &msg_array->messages[i]);
  359. }
  360. TALLOC_FREE(msg_array);
  361. }
  362. /** @} **/