예제 #1
0
int
main(int argc, char *argv[])
{
    int semid, shmid, xfrs, bytes;
    struct shmseg *shmp;

    /* Get IDs for semaphore set and shared memory created by writer */

    semid = semget(SEM_KEY, 0, 0);
    if (semid == -1)
        errExit("semget");

    shmid  = shmget(SHM_KEY, 0, 0);
    if (shmid == -1)
        errExit("shmget");

    /* Attach shared memory read-only, as we will only read */

    shmp = shmat(shmid, NULL, SHM_RDONLY);
    if (shmp == (void *) -1)
        errExit("shmat");

    /* Transfer blocks of data from shared memory to stdout */

    for (xfrs = 0, bytes = 0; ; xfrs++) {
        if (reserveSem(semid, READ_SEM) == -1)          /* Wait for our turn */
            errExit("reserveSem");

        if (shmp->cnt == 0)                     /* Writer encountered EOF */
            break;
        bytes += shmp->cnt;

        if (write(STDOUT_FILENO, shmp->buf, shmp->cnt) != shmp->cnt)
            fatal("partial/failed write");

        if (releaseSem(semid, WRITE_SEM) == -1)         /* Give writer a turn */
            errExit("releaseSem");
    }

    if (shmdt(shmp) == -1)
        errExit("shmdt");

    /* Give writer one more turn, so it can clean up */

    if (releaseSem(semid, WRITE_SEM) == -1)
        errExit("releaseSem");

    fprintf(stderr, "Received %d bytes (%d xfrs)\n", bytes, xfrs);
    exit(EXIT_SUCCESS);
}
예제 #2
0
int
_sfcCertificateAuthenticate(X509 * cert, char **principal, int mode)
{
  unsigned int    i;
  size_t          der_buflen = 0;
  unsigned char   der_buf[MAX_CERTIFICATE];
  unsigned char  *der_bufp = der_buf;

#ifdef DEBUG
  fprintf(stderr, "_sfcCertificateAuthenticate: mode = %d\n", mode);
#endif
  if (cert && principal) {
    der_buflen = i2d_X509(cert, &der_bufp);
    if (der_buflen > 0 && der_buflen <= MAX_CERTIFICATE && aquireSem()) {
#ifdef DEBUG
      fprintf(stderr, "_sfcCertificateAuthenticate: cert len = %d\n",
              der_buflen);
#endif
      for (i = 0; i < CertStore->maxcert; i++) {
        if (CertStore->certs[i].cert_length == der_buflen &&
            memcmp(CertStore->certs[i].cert_der, der_buf,
                   der_buflen) == 0) {
          if (mode == 0) {
            *principal = CertStore->certs[i].cert_principal;
#ifdef DEBUG
            fprintf(stderr, "_sfcCertificateAuthenticate: found cert\n");
#endif
            return 1;
          } else {
            break;
          }
        }
      }
      if (mode == 1 && i < NUM_CERTS && *principal &&
          strlen(*principal) < MAX_PRINCIPAL) {
        CertStore->certs[i].cert_length = der_buflen;
        memcpy(CertStore->certs[i].cert_der, der_buf, der_buflen);
        strcpy(CertStore->certs[i].cert_principal, *principal);
        CertStore->maxcert = i + 1;
#ifdef DEBUG
        fprintf(stderr, "_sfcCertificateAuthenticate: inserted cert\n");
#endif
        return 1;
      }
    }
    releaseSem();
  }
#ifdef DEBUG
  fprintf(stderr, "_sfcCertificateAuthenticate: failed\n");
#endif
  return 0;
}
예제 #3
0
int
main(int argc, char *argv[])
{
    int semid, shmid, bytes, xfrs;
    struct shmseg *shmp;
    union semun dummy;

    /* Create set containing two semaphores; initialize so that
       writer has first access to shared memory. */

    semid = semget(SEM_KEY, 2, IPC_CREAT | OBJ_PERMS);
    if (semid == -1)
        errExit("semget");

    if (initSemAvailable(semid, WRITE_SEM) == -1)
        errExit("initSemAvailable");
    if (initSemInUse(semid, READ_SEM) == -1)
        errExit("initSemInUse");

    /* Create shared memory; attach at address chosen by system */

    shmid = shmget(SHM_KEY, sizeof(struct shmseg), IPC_CREAT | OBJ_PERMS);
    if (shmid == -1)
        errExit("shmget");

    shmp = shmat(shmid, NULL, 0);
    if (shmp == (void *) -1)
        errExit("shmat");

    /* Transfer blocks of data from stdin to shared memory */

    for (xfrs = 0, bytes = 0; ; xfrs++, bytes += shmp->cnt) {
        if (reserveSem(semid, WRITE_SEM) == -1)         /* Wait for our turn */
            errExit("reserveSem");

        shmp->cnt = read(STDIN_FILENO, shmp->buf, BUF_SIZE);
        if (shmp->cnt == -1)
            errExit("read");

        if (releaseSem(semid, READ_SEM) == -1)          /* Give reader a turn */
            errExit("releaseSem");

        /* Have we reached EOF? We test this after giving the reader
           a turn so that it can see the 0 value in shmp->cnt. */

        if (shmp->cnt == 0)
            break;
    }

    /* Wait until reader has let us have one more turn. We then know
       reader has finished, and so we can delete the IPC objects. */

    if (reserveSem(semid, WRITE_SEM) == -1)
        errExit("reserveSem");

    if (semctl(semid, 0, IPC_RMID, dummy) == -1)
        errExit("semctl");
    if (shmdt(shmp) == -1)
        errExit("shmdt");
    if (shmctl(shmid, IPC_RMID, 0) == -1)
        errExit("shmctl");

    fprintf(stderr, "Sent %d bytes (%d xfrs)\n", bytes, xfrs);
    exit(EXIT_SUCCESS);
}