Home | History | Annotate | Download | only in tests
      1 #define _GNU_SOURCE
      2 
      3 #include <config.h>
      4 #include <errno.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <sys/ipc.h>
      8 #include <sys/sem.h>
      9 #include <time.h>
     10 #include <unistd.h>
     11 int main(int argc, char **argv)
     12 {
     13    int semid;
     14    struct sembuf sop;
     15 #ifdef HAVE_SEMTIMEDOP
     16    struct timespec ts;
     17 #endif
     18 
     19    if ((semid = semget(IPC_PRIVATE, 1, 0600)) < 0)
     20    {
     21       perror("semget");
     22       exit(1);
     23    }
     24 
     25    sop.sem_num = 0;
     26    sop.sem_op = 1;
     27    sop.sem_flg = 0;
     28 
     29    if (semop(semid, &sop, 1) < 0)
     30    {
     31       perror("semop");
     32       semctl(semid, 0, IPC_RMID);
     33       exit(1);
     34    }
     35 
     36    if (semctl(semid, 0, GETVAL) != 1)
     37    {
     38       perror("semctl GETVAL");
     39       semctl(semid, 0, IPC_RMID);
     40       exit(1);
     41    }
     42 
     43    if (semctl(semid, 0, GETPID) != getpid())
     44    {
     45       perror("semctl GETPID");
     46       semctl(semid, 0, IPC_RMID);
     47       exit(1);
     48    }
     49 
     50    /* The next call to semtimedop causes the program to hang on
     51       ppc32-linux (Yellow Dog 4.0).  I don't know why.  Hence the
     52       extended ifdef. */
     53 #if defined(HAVE_SEMTIMEDOP) && !defined(__powerpc__)
     54    sop.sem_num = 0;
     55    sop.sem_op = 0;
     56    sop.sem_flg = 0;
     57 
     58    ts.tv_sec = 0;
     59    ts.tv_nsec = 1000000;
     60 
     61    if (semtimedop(semid, &sop, 1, &ts) < 0 && errno != EAGAIN)
     62    {
     63       perror("semtimedop");
     64       semctl(semid, 0, IPC_RMID);
     65       exit(1);
     66    }
     67 #endif
     68 
     69    sop.sem_num = 0;
     70    sop.sem_op = -1;
     71    sop.sem_flg = 0;
     72 
     73    if (semop(semid, &sop, 1) < 0)
     74    {
     75       perror("semop");
     76       semctl(semid, 0, IPC_RMID);
     77       exit(1);
     78    }
     79 
     80 #ifdef HAVE_SEMTIMEDOP
     81    sop.sem_num = 0;
     82    sop.sem_op = 0;
     83    sop.sem_flg = 0;
     84 
     85    ts.tv_sec = 0;
     86    ts.tv_nsec = 1000;
     87 
     88    if (semtimedop(semid, &sop, 1, &ts) < 0)
     89    {
     90       perror("semtimedop");
     91       semctl(semid, 0, IPC_RMID);
     92       exit(1);
     93    }
     94 #endif
     95 
     96    if (semctl(semid, 0, IPC_RMID) < 0)
     97    {
     98       perror("semctl(IPC_RMID)");
     99       exit(1);
    100    }
    101 
    102    exit(0);
    103 }
    104