Austin Group Defect Tracker

Aardvark Mark IV


Viewing Issue Simple Details Jump to Notes ] Issue History ] Print ]
ID Category Severity Type Date Submitted Last Update
0000335 [1003.1(2008)/Issue 7] System Interfaces Editorial Error 2010-10-16 17:52 2013-04-16 13:06
Reporter weeks View Status public  
Assigned To ajosey
Priority normal Resolution Accepted As Marked  
Status Closed  
Name Nathan Weeks
Organization
User Reference
Section semget, semop
Page Number 1837,1842
Line Number 58680,58875
Interp Status ---
Final Accepted Text Note: 0000594
Summary 0000335: semget() and semop() examples do not initialize semval
Description The examples for semget() and semop() do not initialize the semaphore value with
semctl() after creating the semaphore set with the second call to semget() and
before operating on it with the first call to semop().

The description for semget() contains the bullet:

"The data structure associated with each semaphore in the set need not be
initialized. The semctl() function with the command SETVAL or SETALL can be used
to initialize each semaphore."
Desired Action A couple possibilities:

1. Replace the first call to semop() with "semctl(semid, 0, SETVAL, arg)", where
   "arg" is of type "union semun" as described in semctl(), and arg.val = 2.

2. Before the first call to semop(), call "semctl(semid, 0, SETVAL, arg)", where
   arg.val = 0. By keeping the call to semop(), the sem_otime member of the
   semid_ds data structure associated with semid will be updated, which, while
   not explicitly referenced in the examples, may be referenced in applications
   that wish to avoid the race condition described in "Unix Network Programming
   Volume 2 (2nd edition)" on pages 284-285.

The sentence "After creating the semaphore, the program uses a call to semop() to initialize it to the values in the sbuf array" may need to be reworded depending on the solution.
Tags tc1-2008
Attached Files

- Relationships
related to 0000345Closedajosey 1003.1(2008)/Issue 7 semop must update sem_otime 
related to 0000377Appliedajosey 1003.1(2008)/Issue 7 Semaphore values should be initialized to zero when a semaphore set is created. 
related to 0000439Closedajosey 2008-TC1 XSH/TC1/D1/0262 redundant 
related to 0000575Closedajosey 2008-TC1 semget() Future Directions should be updated 

-  Notes
(0000594)
eblake (manager)
2010-10-28 23:07
edited on: 2010-11-04 15:25

Rather than maintain two similar examples, it is easier to follow the lead of
dlopen() and just refer to the longer example of the two. There are some
additional cleanups to be made while improving the example, such as removing
unused variables, and removing headers that are not necessary for compilation
to succeed (for example, <sys/ipc.h> is required to already be included by
<sys/sem.h>). The following changes implement desired action 2, along with
enhancing the example to check sem_otime to detect the race.

Replace line 58574 [XSH semctl EXAMPLES] with:

Refer to semop() (on page 1841).

Replace the entire example on lines 58642-58701 [XSH semget EXAMPLES] with:

Refer to semop() (on page 1841).

After line 58825 [XSH semop EXAMPLES], insert:

/* Code to initialize semid. */
...

At line 58841, change "uses a call to semop() to initialize it" to "uses calls to semctl() and semop() to initialize it"

After line 58844, add a paragraph:

Processes that obtain semid without creating it check that sem_otime is
non-zero, to ensure that the creating process has completed the semop()
initialization.

Delete lines 58848 (<sys/types.h>), 58850 (<sys/ipc.h>), 58854 (<unistd.h>),
and 58856-58858 (<pwd.h>, <fcntl.h>, and <limits.h>).

At line 58861, delete ", pfd, fv", leaving just "int semid;".

Delete lines 58863-58866 (lgn, filename, outstat, pw), and replace them with:

union semun {
  int val;
  struct semid_ds *buf;
  unsigned short *array;
} arg;
struct semid_ds ds;

After line 58878, insert:

arg.val = 0;

Replace line 58882 with:

if (semctl(semid, 0, SETVAL, arg) == -1
    || semop(semid, &sbuf, 1) == -1) {

After line 58889, insert:

goto check_init;

After line 58894, insert:

else
{
    /* Check that semid has completed initialization. */
    /* An application may use a retry loop at this point rather than exiting. */
    check_init:
    arg.buf = &ds;
    if (semctl(semid, 0, IPC_STAT, arg) < 0) {
        perror("IPC error 3: semctl"); exit(1);
    }
    if (ds.sem_otime == 0) {
        perror("IPC error 4: semctl"); exit(1);
    }
}


- Issue History
Date Modified Username Field Change
2010-10-16 17:52 weeks New Issue
2010-10-16 17:52 weeks Status New => Under Review
2010-10-16 17:52 weeks Assigned To => ajosey
2010-10-16 17:52 weeks Name => Nathan Weeks
2010-10-16 17:52 weeks Section => semget, semop
2010-10-16 17:52 weeks Page Number => 0
2010-10-16 17:52 weeks Line Number => 0
2010-10-16 17:52 weeks Issue Monitored: weeks
2010-10-28 23:07 eblake Note Added: 0000594
2010-10-28 23:41 eblake Relationship added related to 0000345
2010-10-29 18:55 Don Cragun Page Number 0 => 1837,1842
2010-10-29 18:55 Don Cragun Line Number 0 => 58680,58875
2010-10-29 18:55 Don Cragun Interp Status => ---
2010-11-04 15:25 eblake Note Edited: 0000594
2010-11-04 15:29 eblake Final Accepted Text => Note: 0000594
2010-11-04 15:29 eblake Status Under Review => Resolved
2010-11-04 15:29 eblake Resolution Open => Accepted As Marked
2010-11-04 15:30 eblake Tag Attached: tc1-2008
2011-02-09 07:49 Don Cragun Relationship added related to 0000377
2011-05-13 15:34 eblake Relationship added related to 0000439
2012-08-22 16:01 eblake Relationship added related to 0000575
2013-04-16 13:06 ajosey Status Resolved => Closed


Mantis 1.1.6[^]
Copyright © 2000 - 2008 Mantis Group
Powered by Mantis Bugtracker