/*
* Copyright (c) 2000-2001, 2005-2008 Proofpoint, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*/
#include <stdio.h>
#if SM_CONF_SEM
# include <stdlib.h>
# include <unistd.h>
# include <sysexits.h>
static void
delay(t, s)
int t;
char *s;
{
if (t > 0)
{
#if DEBUG
#endif /* DEBUG */
sleep(t);
}
#if DEBUG
#endif /* DEBUG */
}
/*
** SEMINTER -- interactive testing of semaphores.
**
** Parameters:
** owner -- create semaphores.
**
** Returns:
** 0 on success
** < 0 on failure.
*/
static int
bool owner;
{
int semid;
int t;
if (semid < 0)
{
perror("sm_sem_start failed");
return 1;
}
{
switch (t)
{
case 'a':
delay(0, "try to acq");
{
perror("sm_sem_acq failed");
return 1;
}
delay(0, "acquired");
break;
case 'r':
delay(0, "try to rel");
{
perror("sm_sem_rel failed");
return 1;
}
delay(0, "released");
break;
case 'v':
if ((t = sm_sem_get(semid, 0)) < 0)
{
perror("get_sem failed");
return 1;
}
printf("semval: %d\n", t);
break;
}
}
if (owner)
return sm_sem_stop(semid);
return 0;
}
/*
** SEM_CLEANUP -- cleanup if something breaks
**
** Parameters:
** sig -- signal.
**
** Returns:
** none.
*/
void
int sig;
{
if (semid_c >= 0)
(void) sm_sem_stop(semid_c);
}
static int
{
int r;
if (r != 0)
return r;
return r;
}
/*
** SEMTEST -- test of semaphores
**
** Parameters:
** owner -- create semaphores.
**
** Returns:
** 0 on success
** < 0 on failure.
*/
static int
int owner;
{
int semid, r;
int cnt = 0;
{
if (r < 0)
{
perror("drop_priv child failed");
return -1;
}
}
if (semid < 0)
{
perror("sm_sem_start failed");
return -1;
}
if (owner)
{
if (uid != 0)
{
if (r < 0)
{
perror("sm_semsetowner failed");
return -1;
}
if (r < 0)
{
perror("drop_priv owner failed");
return -1;
}
}
/* just in case someone kills the program... */
cnt = 0;
do
{
r = sm_sem_acq(semid, 0, 0);
if (r < 0)
{
sleep(1);
++cnt;
}
SM_TEST(r >= 0);
if (r < 0)
return r;
cnt = 0;
do
{
r = sm_sem_rel(semid, 0, 0);
if (r < 0)
{
sleep(1);
++cnt;
}
SM_TEST(r >= 0);
if (r < 0)
return r;
cnt = 0;
do
{
r = sm_sem_get(semid, 0);
if (r <= 0)
{
sleep(1);
++cnt;
}
SM_TEST(r > 0);
if (r <= 0)
return r;
cnt = 0;
do
{
r = sm_sem_acq(semid, 0, 0);
if (r < 0)
{
sleep(1);
++cnt;
}
SM_TEST(r >= 0);
if (r < 0)
return r;
cnt = 0;
do
{
r = sm_sem_rel(semid, 0, 0);
if (r < 0)
{
sleep(1);
++cnt;
}
SM_TEST(r >= 0);
if (r < 0)
return r;
}
else
{
cnt = 0;
do
{
r = sm_sem_acq(semid, 0, 0);
if (r < 0)
{
sleep(1);
++cnt;
}
SM_TEST(r >= 0);
if (r < 0)
return r;
cnt = 0;
do
{
r = sm_sem_rel(semid, 0, 0);
if (r < 0)
{
sleep(1);
++cnt;
}
SM_TEST(r >= 0);
if (r < 0)
return r;
}
if (owner)
return sm_sem_stop(semid);
return 0;
}
int
int argc;
char *argv[];
{
bool interactive = false;
bool owner = false;
int ch, r;
uid = 0;
gid = 0;
r = 0;
{
switch ((char) ch)
{
case 'g':
break;
case 'i':
interactive = true;
break;
case 'u':
break;
case 'o':
owner = true;
break;
default:
break;
}
}
if (interactive)
else
{
printf("This test takes about 8 seconds.\n");
printf("If it takes longer than 30 seconds, please interrupt it\n");
printf("and compile again without semaphore support, i.e.,");
printf("-DSM_CONF_SEM=0\n");
{
perror("fork failed\n");
return -1;
}
if (pid == 0)
{
/* give the parent the chance to setup data */
sleep(1);
}
else
{
}
SM_TEST(r == 0);
return sm_test_end();
}
return r;
}
#else /* SM_CONF_SEM */
int
int argc;
char *argv[];
{
printf("No support for semaphores configured on this machine\n");
return 0;
}
#endif /* SM_CONF_SEM */