Back to home page

LXR

 
 

    


0001 /*
0002  * linux/ipc/util.h
0003  * Copyright (C) 1999 Christoph Rohland
0004  *
0005  * ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com>
0006  * namespaces support.      2006 OpenVZ, SWsoft Inc.
0007  *                               Pavel Emelianov <xemul@openvz.org>
0008  */
0009 
0010 #ifndef _IPC_UTIL_H
0011 #define _IPC_UTIL_H
0012 
0013 #include <linux/unistd.h>
0014 #include <linux/err.h>
0015 
0016 #define SEQ_MULTIPLIER  (IPCMNI)
0017 
0018 void sem_init(void);
0019 void msg_init(void);
0020 void shm_init(void);
0021 
0022 struct ipc_namespace;
0023 
0024 #ifdef CONFIG_POSIX_MQUEUE
0025 extern void mq_clear_sbinfo(struct ipc_namespace *ns);
0026 extern void mq_put_mnt(struct ipc_namespace *ns);
0027 #else
0028 static inline void mq_clear_sbinfo(struct ipc_namespace *ns) { }
0029 static inline void mq_put_mnt(struct ipc_namespace *ns) { }
0030 #endif
0031 
0032 #ifdef CONFIG_SYSVIPC
0033 void sem_init_ns(struct ipc_namespace *ns);
0034 void msg_init_ns(struct ipc_namespace *ns);
0035 void shm_init_ns(struct ipc_namespace *ns);
0036 
0037 void sem_exit_ns(struct ipc_namespace *ns);
0038 void msg_exit_ns(struct ipc_namespace *ns);
0039 void shm_exit_ns(struct ipc_namespace *ns);
0040 #else
0041 static inline void sem_init_ns(struct ipc_namespace *ns) { }
0042 static inline void msg_init_ns(struct ipc_namespace *ns) { }
0043 static inline void shm_init_ns(struct ipc_namespace *ns) { }
0044 
0045 static inline void sem_exit_ns(struct ipc_namespace *ns) { }
0046 static inline void msg_exit_ns(struct ipc_namespace *ns) { }
0047 static inline void shm_exit_ns(struct ipc_namespace *ns) { }
0048 #endif
0049 
0050 struct ipc_rcu {
0051     struct rcu_head rcu;
0052     atomic_t refcount;
0053 } ____cacheline_aligned_in_smp;
0054 
0055 #define ipc_rcu_to_struct(p)  ((void *)(p+1))
0056 
0057 /*
0058  * Structure that holds the parameters needed by the ipc operations
0059  * (see after)
0060  */
0061 struct ipc_params {
0062     key_t key;
0063     int flg;
0064     union {
0065         size_t size;    /* for shared memories */
0066         int nsems;  /* for semaphores */
0067     } u;            /* holds the getnew() specific param */
0068 };
0069 
0070 /*
0071  * Structure that holds some ipc operations. This structure is used to unify
0072  * the calls to sys_msgget(), sys_semget(), sys_shmget()
0073  *      . routine to call to create a new ipc object. Can be one of newque,
0074  *        newary, newseg
0075  *      . routine to call to check permissions for a new ipc object.
0076  *        Can be one of security_msg_associate, security_sem_associate,
0077  *        security_shm_associate
0078  *      . routine to call for an extra check if needed
0079  */
0080 struct ipc_ops {
0081     int (*getnew)(struct ipc_namespace *, struct ipc_params *);
0082     int (*associate)(struct kern_ipc_perm *, int);
0083     int (*more_checks)(struct kern_ipc_perm *, struct ipc_params *);
0084 };
0085 
0086 struct seq_file;
0087 struct ipc_ids;
0088 
0089 void ipc_init_ids(struct ipc_ids *);
0090 #ifdef CONFIG_PROC_FS
0091 void __init ipc_init_proc_interface(const char *path, const char *header,
0092         int ids, int (*show)(struct seq_file *, void *));
0093 #else
0094 #define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
0095 #endif
0096 
0097 #define IPC_SEM_IDS 0
0098 #define IPC_MSG_IDS 1
0099 #define IPC_SHM_IDS 2
0100 
0101 #define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
0102 #define ipcid_to_seqx(id) ((id) / SEQ_MULTIPLIER)
0103 #define IPCID_SEQ_MAX min_t(int, INT_MAX/SEQ_MULTIPLIER, USHRT_MAX)
0104 
0105 /* must be called with ids->rwsem acquired for writing */
0106 int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
0107 
0108 /* must be called with ids->rwsem acquired for reading */
0109 int ipc_get_maxid(struct ipc_ids *);
0110 
0111 /* must be called with both locks acquired. */
0112 void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
0113 
0114 /* must be called with ipcp locked */
0115 int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flg);
0116 
0117 /* for rare, potentially huge allocations.
0118  * both function can sleep
0119  */
0120 void *ipc_alloc(int size);
0121 void ipc_free(void *ptr);
0122 
0123 /*
0124  * For allocation that need to be freed by RCU.
0125  * Objects are reference counted, they start with reference count 1.
0126  * getref increases the refcount, the putref call that reduces the recount
0127  * to 0 schedules the rcu destruction. Caller must guarantee locking.
0128  */
0129 void *ipc_rcu_alloc(int size);
0130 int ipc_rcu_getref(void *ptr);
0131 void ipc_rcu_putref(void *ptr, void (*func)(struct rcu_head *head));
0132 void ipc_rcu_free(struct rcu_head *head);
0133 
0134 struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
0135 struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id);
0136 
0137 void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
0138 void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
0139 int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out);
0140 struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
0141                          struct ipc_ids *ids, int id, int cmd,
0142                          struct ipc64_perm *perm, int extra_perm);
0143 
0144 #ifndef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
0145 /* On IA-64, we always use the "64-bit version" of the IPC structures.  */
0146 # define ipc_parse_version(cmd) IPC_64
0147 #else
0148 int ipc_parse_version(int *cmd);
0149 #endif
0150 
0151 extern void free_msg(struct msg_msg *msg);
0152 extern struct msg_msg *load_msg(const void __user *src, size_t len);
0153 extern struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst);
0154 extern int store_msg(void __user *dest, struct msg_msg *msg, size_t len);
0155 
0156 extern void recompute_msgmni(struct ipc_namespace *);
0157 
0158 static inline int ipc_buildid(int id, int seq)
0159 {
0160     return SEQ_MULTIPLIER * seq + id;
0161 }
0162 
0163 static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int uid)
0164 {
0165     return uid / SEQ_MULTIPLIER != ipcp->seq;
0166 }
0167 
0168 static inline void ipc_lock_object(struct kern_ipc_perm *perm)
0169 {
0170     spin_lock(&perm->lock);
0171 }
0172 
0173 static inline void ipc_unlock_object(struct kern_ipc_perm *perm)
0174 {
0175     spin_unlock(&perm->lock);
0176 }
0177 
0178 static inline void ipc_assert_locked_object(struct kern_ipc_perm *perm)
0179 {
0180     assert_spin_locked(&perm->lock);
0181 }
0182 
0183 static inline void ipc_unlock(struct kern_ipc_perm *perm)
0184 {
0185     ipc_unlock_object(perm);
0186     rcu_read_unlock();
0187 }
0188 
0189 /*
0190  * ipc_valid_object() - helper to sort out IPC_RMID races for codepaths
0191  * where the respective ipc_ids.rwsem is not being held down.
0192  * Checks whether the ipc object is still around or if it's gone already, as
0193  * ipc_rmid() may have already freed the ID while the ipc lock was spinning.
0194  * Needs to be called with kern_ipc_perm.lock held -- exception made for one
0195  * checkpoint case at sys_semtimedop() as noted in code commentary.
0196  */
0197 static inline bool ipc_valid_object(struct kern_ipc_perm *perm)
0198 {
0199     return !perm->deleted;
0200 }
0201 
0202 struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id);
0203 int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
0204             const struct ipc_ops *ops, struct ipc_params *params);
0205 void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
0206         void (*free)(struct ipc_namespace *, struct kern_ipc_perm *));
0207 #endif