ipc/msg.c
Source file repositories/reference/linux-study-clean/ipc/msg.c
File Facts
- System
- Linux kernel
- Corpus path
ipc/msg.c- Extension
.c- Size
- 33039 bytes
- Lines
- 1377
- Domain
- Core OS
- Bucket
- IPC
- Inferred role
- Core OS: syscall or user/kernel boundary
- Status
- core implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Defines or participates in a user/kernel boundary; inspect argument validation, copy_from_user/copy_to_user, credentials, and dispatch target.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/capability.hlinux/msg.hlinux/spinlock.hlinux/init.hlinux/mm.hlinux/proc_fs.hlinux/list.hlinux/security.hlinux/sched/wake_q.hlinux/syscalls.hlinux/audit.hlinux/seq_file.hlinux/rwsem.hlinux/nsproxy.hlinux/ipc_namespace.hlinux/rhashtable.hlinux/percpu_counter.hasm/current.hlinux/uaccess.hutil.h
Detected Declarations
syscall msggetsyscall msgctlsyscall old_msgctlsyscall msgsndsyscall msgrcvstruct msg_queuestruct msg_receiverstruct msg_senderstruct compat_msqid_dsstruct compat_msgbuffunction msg_rmidfunction msg_rcu_freefunction newquefunction msg_fits_inqueuefunction ss_addfunction ss_delfunction ss_wakeupfunction list_for_each_entry_safefunction expunge_allfunction list_for_each_entry_safefunction freequefunction list_for_each_entry_safefunction ksys_msggetfunction copy_msqid_to_userfunction copy_msqid_from_userfunction msgctl_downfunction msgctl_infofunction msgctl_statfunction ksys_msgctlfunction ksys_old_msgctlfunction copy_compat_msqid_from_userfunction copy_compat_msqid_to_userfunction compat_ksys_msgctlfunction compat_ksys_old_msgctlfunction testmsgfunction pipelined_sendfunction list_for_each_entry_safefunction do_msgsndfunction ksys_msgsndfunction compat_ksys_msgsndfunction convert_modefunction do_msg_fillfunction free_copyfunction free_copyfunction list_for_each_entryfunction do_msgrcvfunction ksys_msgrcvfunction compat_do_msg_fill
Annotated Snippet
SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg)
{
return ksys_msgget(key, msgflg);
}
static inline unsigned long
copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
{
switch (version) {
case IPC_64:
return copy_to_user(buf, in, sizeof(*in));
case IPC_OLD:
{
struct msqid_ds out;
memset(&out, 0, sizeof(out));
ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
out.msg_stime = in->msg_stime;
out.msg_rtime = in->msg_rtime;
out.msg_ctime = in->msg_ctime;
if (in->msg_cbytes > USHRT_MAX)
out.msg_cbytes = USHRT_MAX;
else
out.msg_cbytes = in->msg_cbytes;
out.msg_lcbytes = in->msg_cbytes;
if (in->msg_qnum > USHRT_MAX)
out.msg_qnum = USHRT_MAX;
else
out.msg_qnum = in->msg_qnum;
if (in->msg_qbytes > USHRT_MAX)
out.msg_qbytes = USHRT_MAX;
else
out.msg_qbytes = in->msg_qbytes;
out.msg_lqbytes = in->msg_qbytes;
out.msg_lspid = in->msg_lspid;
out.msg_lrpid = in->msg_lrpid;
return copy_to_user(buf, &out, sizeof(out));
}
default:
return -EINVAL;
}
}
static inline unsigned long
copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)
{
switch (version) {
case IPC_64:
if (copy_from_user(out, buf, sizeof(*out)))
return -EFAULT;
return 0;
case IPC_OLD:
{
struct msqid_ds tbuf_old;
if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
return -EFAULT;
out->msg_perm.uid = tbuf_old.msg_perm.uid;
out->msg_perm.gid = tbuf_old.msg_perm.gid;
out->msg_perm.mode = tbuf_old.msg_perm.mode;
if (tbuf_old.msg_qbytes == 0)
out->msg_qbytes = tbuf_old.msg_lqbytes;
else
out->msg_qbytes = tbuf_old.msg_qbytes;
return 0;
}
default:
return -EINVAL;
}
}
/*
* This function handles some msgctl commands which require the rwsem
* to be held in write mode.
* NOTE: no locks must be held, the rwsem is taken inside this function.
*/
static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
struct ipc64_perm *perm, int msg_qbytes)
{
struct kern_ipc_perm *ipcp;
Annotation
- Immediate include surface: `linux/capability.h`, `linux/msg.h`, `linux/spinlock.h`, `linux/init.h`, `linux/mm.h`, `linux/proc_fs.h`, `linux/list.h`, `linux/security.h`.
- Detected declarations: `syscall msgget`, `syscall msgctl`, `syscall old_msgctl`, `syscall msgsnd`, `syscall msgrcv`, `struct msg_queue`, `struct msg_receiver`, `struct msg_sender`, `struct compat_msqid_ds`, `struct compat_msgbuf`.
- Atlas domain: Core OS / IPC.
- Implementation status: core implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.