📄 messages.c
字号:
BOOL message_send_pid_with_timeout(struct process_id pid, int msg_type, const void *buf, size_t len, BOOL duplicates_allowed, unsigned int timeout){ return message_send_pid_internal(pid, msg_type, buf, len, duplicates_allowed, timeout);}/**************************************************************************** Count the messages pending for a particular pid. Expensive....****************************************************************************/unsigned int messages_pending_for_pid(struct process_id pid){ TDB_DATA kbuf; TDB_DATA dbuf; char *buf; unsigned int message_count = 0; kbuf = message_key_pid(pid); dbuf = tdb_fetch(tdb, kbuf); if (dbuf.dptr == NULL || dbuf.dsize == 0) { SAFE_FREE(dbuf.dptr); return 0; } for (buf = dbuf.dptr; dbuf.dsize > sizeof(struct message_rec);) { struct message_rec rec; memcpy(&rec, buf, sizeof(rec)); buf += (sizeof(rec) + rec.len); dbuf.dsize -= (sizeof(rec) + rec.len); message_count++; } SAFE_FREE(dbuf.dptr); return message_count;}/**************************************************************************** Retrieve all messages for the current process.****************************************************************************/static BOOL retrieve_all_messages(char **msgs_buf, size_t *total_len){ TDB_DATA kbuf; TDB_DATA dbuf; TDB_DATA null_dbuf; ZERO_STRUCT(null_dbuf); *msgs_buf = NULL; *total_len = 0; kbuf = message_key_pid(pid_to_procid(sys_getpid())); if (tdb_chainlock(tdb, kbuf) == -1) return False; dbuf = tdb_fetch(tdb, kbuf); /* * Replace with an empty record to keep the allocated * space in the tdb. */ tdb_store(tdb, kbuf, null_dbuf, TDB_REPLACE); tdb_chainunlock(tdb, kbuf); if (dbuf.dptr == NULL || dbuf.dsize == 0) { SAFE_FREE(dbuf.dptr); return False; } *msgs_buf = dbuf.dptr; *total_len = dbuf.dsize; return True;}/**************************************************************************** Parse out the next message for the current process.****************************************************************************/static BOOL message_recv(char *msgs_buf, size_t total_len, int *msg_type, struct process_id *src, char **buf, size_t *len){ struct message_rec rec; char *ret_buf = *buf; *buf = NULL; *len = 0; if (total_len - (ret_buf - msgs_buf) < sizeof(rec)) return False; memcpy(&rec, ret_buf, sizeof(rec)); ret_buf += sizeof(rec); if (rec.msg_version != MESSAGE_VERSION) { DEBUG(0,("message version %d received (expected %d)\n", rec.msg_version, MESSAGE_VERSION)); return False; } if (rec.len > 0) { if (total_len - (ret_buf - msgs_buf) < rec.len) return False; } *len = rec.len; *msg_type = rec.msg_type; *src = rec.src; *buf = ret_buf; return True;}/**************************************************************************** Receive and dispatch any messages pending for this process. Notice that all dispatch handlers for a particular msg_type get called, so you can register multiple handlers for a message. *NOTE*: Dispatch functions must be able to cope with incoming messages on an *odd* byte boundary.****************************************************************************/void message_dispatch(void){ int msg_type; struct process_id src; char *buf; char *msgs_buf; size_t len, total_len; struct dispatch_fns *dfn; int n_handled; if (!received_signal) return; DEBUG(10,("message_dispatch: received_signal = %d\n", received_signal)); received_signal = 0; if (!retrieve_all_messages(&msgs_buf, &total_len)) return; for (buf = msgs_buf; message_recv(msgs_buf, total_len, &msg_type, &src, &buf, &len); buf += len) { DEBUG(10,("message_dispatch: received msg_type=%d " "src_pid=%u\n", msg_type, (unsigned int) procid_to_pid(&src))); n_handled = 0; for (dfn = dispatch_fns; dfn; dfn = dfn->next) { if (dfn->msg_type == msg_type) { DEBUG(10,("message_dispatch: processing message of type %d.\n", msg_type)); dfn->fn(msg_type, src, len ? (void *)buf : NULL, len); n_handled++; } } if (!n_handled) { DEBUG(5,("message_dispatch: warning: no handlers registed for " "msg_type %d in pid %u\n", msg_type, (unsigned int)sys_getpid())); } } SAFE_FREE(msgs_buf);}/**************************************************************************** Register a dispatch function for a particular message type. *NOTE*: Dispatch functions must be able to cope with incoming messages on an *odd* byte boundary.****************************************************************************/void message_register(int msg_type, void (*fn)(int msg_type, struct process_id pid, void *buf, size_t len)){ struct dispatch_fns *dfn; dfn = SMB_MALLOC_P(struct dispatch_fns); if (dfn != NULL) { ZERO_STRUCTPN(dfn); dfn->msg_type = msg_type; dfn->fn = fn; DLIST_ADD(dispatch_fns, dfn); } else { DEBUG(0,("message_register: Not enough memory. malloc failed!\n")); }}/**************************************************************************** De-register the function for a particular message type.****************************************************************************/void message_deregister(int msg_type){ struct dispatch_fns *dfn, *next; for (dfn = dispatch_fns; dfn; dfn = next) { next = dfn->next; if (dfn->msg_type == msg_type) { DLIST_REMOVE(dispatch_fns, dfn); SAFE_FREE(dfn); } } }struct msg_all { int msg_type; uint32 msg_flag; const void *buf; size_t len; BOOL duplicates; int n_sent;};/**************************************************************************** Send one of the messages for the broadcast.****************************************************************************/static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state){ struct connections_data crec; struct msg_all *msg_all = (struct msg_all *)state; if (dbuf.dsize != sizeof(crec)) return 0; memcpy(&crec, dbuf.dptr, sizeof(crec)); if (crec.cnum != -1) return 0; /* Don't send if the receiver hasn't registered an interest. */ if(!(crec.bcast_msg_flags & msg_all->msg_flag)) return 0; /* If the msg send fails because the pid was not found (i.e. smbd died), * the msg has already been deleted from the messages.tdb.*/ if (!message_send_pid(crec.pid, msg_all->msg_type, msg_all->buf, msg_all->len, msg_all->duplicates)) { /* If the pid was not found delete the entry from connections.tdb */ if (errno == ESRCH) { DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n", procid_str_static(&crec.pid), crec.cnum, crec.name)); tdb_delete(the_tdb, kbuf); } } msg_all->n_sent++; return 0;}/** * Send a message to all smbd processes. * * It isn't very efficient, but should be OK for the sorts of * applications that use it. When we need efficient broadcast we can add * it. * * @param n_sent Set to the number of messages sent. This should be * equal to the number of processes, but be careful for races. * * @retval True for success. **/BOOL message_send_all(TDB_CONTEXT *conn_tdb, int msg_type, const void *buf, size_t len, BOOL duplicates_allowed, int *n_sent){ struct msg_all msg_all; msg_all.msg_type = msg_type; if (msg_type < 1000) msg_all.msg_flag = FLAG_MSG_GENERAL; else if (msg_type > 1000 && msg_type < 2000) msg_all.msg_flag = FLAG_MSG_NMBD; else if (msg_type > 2000 && msg_type < 2100) msg_all.msg_flag = FLAG_MSG_PRINT_NOTIFY; else if (msg_type > 2100 && msg_type < 3000) msg_all.msg_flag = FLAG_MSG_PRINT_GENERAL; else if (msg_type > 3000 && msg_type < 4000) msg_all.msg_flag = FLAG_MSG_SMBD; else return False; msg_all.buf = buf; msg_all.len = len; msg_all.duplicates = duplicates_allowed; msg_all.n_sent = 0; tdb_traverse(conn_tdb, traverse_fn, &msg_all); if (n_sent) *n_sent = msg_all.n_sent; return True;}/** @} **/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -