Esempio n. 1
0
my_perror()
{
  register int len;
  char         *sys_err_str();

  len = strlen(emesgstr);
  sprintf(emesgstr + len, " %s", sys_err_str());
}
void server(int readfd, int writefd)
{
char buff[MAXBUFF], errmesg[256], *sys_err_str();
int n, fd;
// Read the filename from the IPC descriptor.
if ( (n = read(readfd, buff, MAXBUFF)) <= 0) err_sys("server: filename read error");
buff[n] = '\0'; /* null terminate filename */
if ( (fd = open(buff, 0)) < 0) {
// Error. Format an error message and send it back to the client.
sprintf(errmesg, ": can't open, %s\n", sys_err_str());
strcat(buff, errmesg);
n = strlen(buff);
if (write(writefd, buff, n) != n) err_sys("server: errmesg write error");
} else {
// Read the data from the file and write to the IPC descriptor.
while ( (n = read(fd, buff, MAXBUFF)) > 0)
if (write(writefd, buff, n) != n) err_sys("server: data write error");
if (n < 0) err_sys("server: read error");
}
}
Esempio n. 3
0
/*
 * Open the audio device.
 *
 * Return 0 on success and -1 on error.
 */
int
rplay_audio_open()
{
    int flags;

    rplay_audio_fd = open(rplay_audio_device, O_WRONLY | O_NDELAY, 0);
    if (rplay_audio_fd < 0)
    {
	return -1;
    }

    if (fcntl(rplay_audio_fd, F_SETFD, 1) < 0)
    {
	report(REPORT_ERROR,
	       "rplay_audio_open: close-on-exec %d\n",
	       sys_err_str(errno));
	/* return -1; */
    }

    if (rplay_audio_init() < 0)
    {
	return -1;
    }

    /*
     * Make sure the audio device writes are non-blocking.
     */
    flags = fcntl(rplay_audio_fd, F_GETFL, 0);
    if (flags < 0)
    {
	return -1;
    }
    flags |= FNDELAY;
    if (fcntl(rplay_audio_fd, F_SETFL, flags) < 0)
    {
	return -1;
    }

    return 0;
}
Esempio n. 4
0
my_perror()
{
  char *sys_err_str();

  fprintf(stderr, " %s\n", sys_err_str());
}
Esempio n. 5
0
/*
 * Initialize the audio device.
 * This routine must set the following external variables:
 *      rplay_audio_sample_rate
 *      rplay_audio_precision
 *      rplay_audio_channels
 *      rplay_audio_format
 *      rplay_audio_port
 *
 * and may use the following optional parameters:
 *      optional_sample_rate
 *      optional_precision
 *      optional_channels
 *      optional_format
 *      optional_port
 *
 * optional_* variables with values of zero should be ignored.
 *
 * Return 0 on success and -1 on error.
 */
int
rplay_audio_init()
{
    audio_info_t a;
    audio_device_t d;

    if (rplay_audio_fd == -1)
    {
	rplay_audio_open();
	if (rplay_audio_fd == -1)
	{
	    report(REPORT_ERROR, "rplay_audio_init: cannot open %s\n",
		   rplay_audio_device);
	    return -1;
	}
    }

    if (ioctl(rplay_audio_fd, AUDIO_GETDEV, &d) < 0)
    {
	report(REPORT_ERROR, "rplay_audio_init: AUDIO_GETDEV: %s\n",
	       sys_err_str(errno));
	return -1;
    }

    if (strcmp(d.name, "SUNW,dbri") == 0)
    {
	report(REPORT_DEBUG, "%s device detected\n", d.name);
	rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 11025;
	rplay_audio_precision = optional_precision ? optional_precision : 16;
	rplay_audio_channels = optional_channels ? optional_channels : 1;
	rplay_audio_format = optional_format ? optional_format :
	    rplay_audio_precision == 16 ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_LINEAR_8;
	rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_SPEAKER;
	rplay_audio_table = dbri_table;
    }
    else if (strcmp(d.name, "SUNW,CS4231") == 0)
    {
	report(REPORT_DEBUG, "%s device detected\n", d.name);
	rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 11025;
	rplay_audio_precision = optional_precision ? optional_precision : 16;
	rplay_audio_channels = optional_channels ? optional_channels : 1;
	rplay_audio_format = optional_format ? optional_format :
	    rplay_audio_precision == 16 ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_LINEAR_8;
	rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_SPEAKER;
	rplay_audio_table = dbri_table;		/* use the dbri table */
    }
    else if (strcmp(d.name, "SUNW,am79c30") == 0)
    {
	report(REPORT_DEBUG, "%s device detected\n", d.name);
	rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 8000;
	rplay_audio_precision = optional_precision ? optional_precision : 8;
	rplay_audio_channels = optional_channels ? optional_channels : 1;
	rplay_audio_format = optional_format ? optional_format : RPLAY_FORMAT_ULAW;
	rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_SPEAKER;
	rplay_audio_table = amd_table;
    }
    else if (strcmp(d.name, "SUNW,sb16") == 0)
    {
	report(REPORT_DEBUG, "%s device detected\n", d.name);
	rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 44100;
	rplay_audio_precision = optional_precision ? optional_precision : 16;
	rplay_audio_channels = optional_channels ? optional_channels : 2;
	rplay_audio_format = optional_format ? optional_format :
	    rplay_audio_precision == 16 ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_LINEAR_8;
	rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_SPEAKER;
	rplay_audio_table = dbri_table;		/* use the dbri table */
    }
    else
    {
	report(REPORT_ERROR, "`%s' unknown audio device detected\n", d.name);
	return -1;
    }

    /* Verify the precision and format. */
    switch (rplay_audio_precision)
    {
    case 8:
	if (rplay_audio_format != RPLAY_FORMAT_ULAW
	    && rplay_audio_format != RPLAY_FORMAT_LINEAR_8)
	{
	    report(REPORT_ERROR, "rplay_audio_init: can't use %d bits with format=%d\n",
		   rplay_audio_precision, rplay_audio_format);
	    return -1;
	}
	break;

    case 16:
	if (rplay_audio_format != RPLAY_FORMAT_LINEAR_16)
	{
	    report(REPORT_ERROR, "rplay_audio_init: can't use %d bits with format=%d\n",
		   rplay_audio_precision, rplay_audio_format);
	    return -1;
	}
	break;

    default:
	report(REPORT_ERROR, "rplay_audio_init: `%d' unsupported audio precision\n",
	       rplay_audio_precision);
	return -1;
    }

    AUDIO_INITINFO(&a);

    switch (rplay_audio_format)
    {
    case RPLAY_FORMAT_ULAW:
	a.play.encoding = AUDIO_ENCODING_ULAW;
	break;

    case RPLAY_FORMAT_LINEAR_8:
    case RPLAY_FORMAT_LINEAR_16:
	a.play.encoding = AUDIO_ENCODING_LINEAR;
	break;

    default:
	report(REPORT_ERROR, "rplay_audio_init: unsupported audio format `%d'\n",
	       rplay_audio_format);
	return -1;
    }

    /* Audio port. */
    if (rplay_audio_port == RPLAY_AUDIO_PORT_NONE)
    {
	a.play.port = ~0;	/* see AUDIO_INITINFO in /usr/include/sys/audioio.h. */
    }
    else
    {
	a.play.port = 0;
	if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_LINEOUT))
	{
#ifdef AUDIO_LINE_OUT
	    SET_BIT(a.play.port, AUDIO_LINE_OUT);
#else
	    CLR_BIT(rplay_audio_port, RPLAY_AUDIO_PORT_LINEOUT);
#endif
	}
	if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_HEADPHONE))
	{
#ifdef AUDIO_HEADPHONE
	    SET_BIT(a.play.port, AUDIO_HEADPHONE);
#else
	    CLR_BIT(rplay_audio_port, RPLAY_AUDIO_PORT_HEADPHONE);
#endif
	}
	if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_SPEAKER))
	{
#ifdef AUDIO_SPEAKER
	    SET_BIT(a.play.port, AUDIO_SPEAKER);
#endif
	    /* Assume speaker is okay. */
	}
    }

    a.play.sample_rate = rplay_audio_sample_rate;
    a.play.precision = rplay_audio_precision;
    a.play.channels = rplay_audio_channels;

    if (ioctl(rplay_audio_fd, AUDIO_SETINFO, &a) < 0)
    {
	report(REPORT_ERROR, "rplay_audio_init: AUDIO_SETINFO: %s\n", sys_err_str(errno));
	return -1;
    }

    return 0;
}
Esempio n. 6
0
server()
{
	register int	i, n, filefd;
	char		errmesg[256], *sys_err_str();

	/*
	 * Wait for the client to write the filename into shared memory,
	 * then try to open the file.
	 */

	sem_wait(servsem);
	mesgptr[0]->mesg_data[mesgptr[0]->mesg_len] = '\0';
					/* null terminate filename */
	if ( (filefd = open(mesgptr[0]->mesg_data, 0)) < 0) {
		/*
		 * Error.  Format an error message and send it back
		 * to the client.
		 */

		sprintf(errmesg, ": can't open, %s\n", sys_err_str());
		strcat(mesgptr[0]->mesg_data, errmesg);
		mesgptr[0]->mesg_len = strlen(mesgptr[0]->mesg_data);
		sem_signal(clisem);	/* wake up client */

		sem_wait(servsem);	/* wait for client to process */
		mesgptr[1]->mesg_len = 0;
		sem_signal(clisem);	/* wake up client */

	} else {
		/*
		 * Initialize the server semaphore to the number
		 * of buffers.  We know its value is 0 now, since
		 * it was initialized to 0, and the client has done a
		 * sem_signal(), followed by our sem_wait() above.
		 * What we do is increment the semaphore value
		 * once for every buffer (i.e., the number of resources
		 * that we have).
		 */

		for (i = 0; i < NBUFF; i++)
			sem_signal(servsem);

		/*
		 * Read the data from the file right into shared memory.
		 * The -1 in the number-of-bytes-to-read is because some
		 * Unices have a bug if you try and read into the final byte
		 * of a shared memory segment.
		 */

		for ( ; ; ) {
			for (i = 0; i < NBUFF; i++) {
				sem_wait(servsem);
				n = read(filefd, mesgptr[i]->mesg_data,
							MAXMESGDATA-1);
				if (n < 0)
					err_sys("server: read error");
				mesgptr[i]->mesg_len = n;
				sem_signal(clisem);
				if (n == 0)
					goto alldone;
			}
		}
alldone:
		/* we've already written the 0-length final buffer */
		close(filefd);
	}
}