Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

558 Interprocess Communication Chapter 15


thenftokusually returns two different keys for the two pathnames. However,because
both i-node numbers and keys areoften stored in long integers, information loss can
occur when creating a key.This means that two different pathnames to different files
can generate the same key if the same project ID is used.
The threegetfunctions (msgget, semget,andshmget)all have two similar
arguments: akeyand an integerflag.Anew IPC structure is created (normally by a
server) if eitherkeyisIPC_PRIVATEorkeyis not currently associated with an IPC
structure of the particular type and theIPC_CREATbit offlagis specified. To reference
an existing queue (normally done by a client),keymust equal the key that was specified
when the queue was created, andIPC_CREATmust not be specified.
Note that it’s never possible to specifyIPC_PRIVATEto reference an existing
queue, since this specialkeyvalue always creates a new queue.To reference an existing
queue that was created with akeyofIPC_PRIVATE, we must know the associated
identifier and then use that identifier in the other IPC calls (such asmsgsndand
msgrcv), bypassing thegetfunction.
If we want to create a new IPC structure, making surethat we don’t reference an
existing one with the same identifier, we must specify aflagwith both theIPC_CREAT
andIPC_EXCLbits set. Doing this causes an error return ofEEXISTif the IPC structure
already exists. (This is similar to anopenthat specifies theO_CREATandO_EXCL
flags.)

15.6.2 Per mission Str ucture


XSI IPC associates anipc_permstructurewith each IPC structure. This structure
defines the permissions and owner and includes at least the following members:

struct ipc_perm {
uid_t uid; /* owner’s effective user ID */
gid_t gid; /* owner’s effective group ID */
uid_t cuid; /* creator’s effective user ID */
gid_t cgid; /* creator’s effective group ID */
mode_t mode; /* access modes */
..
.
};

Each implementation includes additional members. See<sys/ipc.h>on your system
for the complete definition.
All the fields areinitialized when the IPC structure is created. Atalater time, we
can modify theuid,gid,andmodefields by callingmsgctl,semctl,orshmctl.To
change these values, the calling process must be either the creator of the IPC structure
or the superuser.Changing these fields is similar to callingchownorchmodfor a file.
The values in themodefield aresimilar to the values we saw in Figure4.6, but there
is nothing corresponding to execute permission for any of the IPC structures. Also,
message queues and shared memory use the termsreadandwrite,but semaphores use
the termsreadandalter.Figure15.24 shows the six permissions for each form of IPC.
Free download pdf