#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>

#define ESP_SEMKEY      0x6e7361

static int Semid;
int sem_locked()
{
    int retval;
    union semun null;
    retval = semctl(Semid, 0, GETVAL, null);
    return retval;
}

void sem_lock()
{
    struct sembuf sops = {
        0, 1, SEM_UNDO
    };
    semop(Semid, &sops, 1);
}

int sem_init(char *file)
{
    Semid = semget(ESP_SEMKEY, 1, IPC_CREAT | 0600);
    if(Semid < 0)
        return -errno;
    return 0;
}

#if 0
int main(int argc, char **argv)
{
    sem_init(argv[0]);
    if(sem_locked()) {
        printf("already running\n");
        exit(-1);
    }
    sem_lock();
    pause();
}
#endif
