Ejemplo n.º 1
0
Pipe::Pipe(const CloseOnExec& close_on_exec)
{
#ifdef HAVE_PIPE2

	if(0 != pipe2(fd_, O_CLOEXEC))
	{
		this->close(); // not sure if call to close matters here
		throw std::runtime_error("could not create pipe");
	}

#else

	if(0 != pipe(fd_))
	{
		this->close(); // not sure if call to close matters here
		throw std::runtime_error("could not create pipe");
	}

	if(CLOSE_ON_EXEC == close_on_exec)
	{
		if(-1 == fcntl(readfd(), F_SETFD, FD_CLOEXEC) ||
		   -1 == fcntl(writefd(), F_SETFD, FD_CLOEXEC))
		{
			this->close();
			throw std::runtime_error("could not set FD_CLOEXEC on pipe");
		}
	}

#endif
}
Ejemplo n.º 2
0
rt_public void app_tpipe(STREAM *stream)
{
	/* Initialize the file descriptor to be used in data exchanges with the
	 * remote process. This enables us to omit this parameter whenever an I/O
	 * with the remote process has to be made.
	 */

	if (app_sp != NULL) { 
		unregister_packet_functions (app_sp); 
		close_stream (app_sp);
	}
	if (stream != NULL) {
		app_sp = stream;
		register_packet_functions (app_sp, &app_send_packet, &app_recv_packet);
	} else {
		app_sp = (STREAM *) 0;
	};

#ifdef DEBUG
#ifdef USE_ADD_LOG
	add_log(20, "stream set up as (rd = #%d, wr = #%d)",
		readfd(app_sp), writefd(app_sp));
#endif
#endif
}
Ejemplo n.º 3
0
int at_cmgl(char *mode) {
	char buffer[256];
	
	zsnprintf(buffer, "AT+CMGL=%s", mode);
	writefd(buffer);
	
	return 1;
}
Ejemplo n.º 4
0
int at_cpms(char *s1, char *s2, char *s3) {
	char buffer[256];
	
	zsnprintf(buffer, "AT+CPMS=\"%s\",\"%s\",\"%s\"", s1, s2, s3);
	writefd(buffer);
	
	return checkok();
}
Ejemplo n.º 5
0
int at_cmgf(int mode) {
	char buffer[256];
	
	zsnprintf(buffer, "AT+CMGF=%d", mode);
	writefd(buffer);
	
	return checkok();
}
Ejemplo n.º 6
0
int at_echo(int value) {
	char buffer[64];
	
	zsnprintf(buffer, "ATE%d", value);
	writefd(buffer);
	
	return checkok();
}
Ejemplo n.º 7
0
int at_curc(int value) {
	char buffer[256];
	
	zsnprintf(buffer, "AT^CURC=%d", value);
	writefd(buffer);
	
	return checkok();
}
Ejemplo n.º 8
0
int at_cmgd(int index, int mode) {
	char buffer[256];
	
	zsnprintf(buffer, "AT+CMGD=%d,%d", index, mode);
	writefd(buffer);
	
	return checkok();
}
Ejemplo n.º 9
0
int at_cnmi(int mode, int i1, int i2, int i3, int i4) {
	char buffer[256];
	
	zsnprintf(buffer, "AT+CNMI=%d,%d,%d,%d,%d", mode, i1, i2, i3, i4);
	writefd(buffer);
	
	return checkok();
}
Ejemplo n.º 10
0
int
my_sendfd(int fd, int wfd)
{
    if (writefd(fd, wfd) >= 0) {
	return 0;
    } else {
	return -1;
    }
}
Ejemplo n.º 11
0
static void
pathinfo (char *path)
{
  int fd;
  struct stat sb;
  dev_t dev;
  char cwd[PATH_MAX+1];
  char buf[2*PATH_MAX+3];
  char res[3*PATH_MAX+40];
  char *rp;
  FILE *dfpipe;

  if (chdir (path) < 0
      || (fd = open (".", O_RDONLY)) < 0
      || fstat (fd, &sb) < 0)
    fperror (path);
  dev = sb.st_dev;

  if (!getcwd (cwd, sizeof (cwd)))
    fperror ("getcwd");

  strcpy (buf, cwd);
  while ((rp = strrchr (buf, '/'))) {
    rp[1] = '\0';
    if (stat (buf, &sb) < 0)
      fperror (buf);
    if (sb.st_dev != dev) {
      if (!(rp = strchr (cwd + (rp - buf) + 1, '/')))
	rp = "/";
      break;
    }
    rp[0] = '\0';
  }
  if (!rp)
    rp = cwd;

  dfpipe = popen (PATH_DF
#ifdef DF_NEEDS_DASH_K
		  " -k"
#endif /* DF_NEEDS_DASH_K */
		  " . | sed -ne '2s/ .*//; 2p'", "r");
  if (!dfpipe || !fgets (buf, sizeof (buf) - 1, dfpipe)
      || pclose (dfpipe) < 0)
    fperror (PATH_DF);
  if (!strchr (buf, '\n'))
    strcat (buf, "\n");

  sprintf (res, "0x%" U64F "x\n%s%s\n", (u_int64_t) dev, buf, rp);
  if (isunixsocket (1) < 0) {
    int i = write (1, res, strlen (res));
    i++;
  } else
    writefd (1, res, strlen (res), fd);
}
Ejemplo n.º 12
0
bool
EnableRDS(uint32_t aMask)
{
  if (!sRadioEnabled || !sRDSSupported)
    return false;

  if (sMsmFMMode)
    setControl(V4L2_CID_PRIVATE_TAVARUA_RDSGROUP_MASK, aMask);

  if (sRDSEnabled)
    return true;

  int pipefd[2];
  int rc = pipe2(pipefd, O_NONBLOCK);
  if (rc < 0) {
    HAL_LOG("Could not create RDS thread signaling pipes (%d)", rc);
    return false;
  }

  ScopedClose writefd(pipefd[1]);
  ScopedClose readfd(pipefd[0]);

  rc = setControl(V4L2_CID_RDS_RECEPTION, true);
  if (rc < 0) {
    HAL_LOG("Could not enable RDS reception (%d)", rc);
    return false;
  }

  sRDSPipeFD = writefd;

  sRDSEnabled = true;

  rc = pthread_create(&sRDSThread, nullptr,
                      readRDSDataThread, (void*)pipefd[0]);
  if (rc) {
    HAL_LOG("Could not start RDS reception thread (%d)", rc);
    setControl(V4L2_CID_RDS_RECEPTION, false);
    sRDSEnabled = false;
    return false;
  }

  readfd.forget();
  writefd.forget();
  return true;
}
Ejemplo n.º 13
0
rt_public void ewb_send_ack_ok(void)
      		/* The socket descriptor */
         	/* The acknowledgment code */
{
	/* Send an acknowledgment report. In case it is a negative one, the error
	 * parameter gives some complementary informations. It is possible to
	 * omit the third parameters for AK_OK or AK_DENIED reports.
	 */

	Request pack;		/* The answer we'll send back */

	Request_Clean (pack);
	pack.rq_type = ACKNLGE;				/* We are sending an acknowledgment */
	pack.rq_ack.ak_type = AK_OK;		/* Report code */

#ifdef USE_ADD_LOG
	add_log(100, "sending ack %d on pipe %d", ACKNLGE, writefd(ewb_sp));
#endif
	ewb_send_packet (ewb_sp, &pack);
}
Ejemplo n.º 14
0
int mcpWrite(int fd, void *ptr, unsigned int nbytes)
{
  unsigned int nleft;
  int nwritten;
  char *buf = (char *) ptr;

  nleft = nbytes;

  while (nleft > 0)
  {
    nwritten = writefd(fd, buf, nleft);

    if (nwritten <= 0)
    {
      return nwritten;		/* Error, return < 0 */
    }

    nleft -= nwritten;
    buf   += nwritten;
  }

  return nbytes - nleft;	/* Return nbytes */
}
Ejemplo n.º 15
0
int at_single() {
	at_commit();
	writefd("AT");
	return checkok();
}
Ejemplo n.º 16
0
rt_public int net_send(EIF_PSTREAM cs, char *buf, size_t size)
/* The connected socket descriptor */
/* Where data are stored */
/* Amount of data to be sent */
{
    /* Write to network */

    size_t amount;

#ifdef EIF_WINDOWS
    DWORD error;
    size_t length;
    BOOL fSuccess;
#else
    size_t length;
    int error;
    Signal_t (*oldpipe)(int);
#endif

#ifdef USE_ADD_LOG
    add_log(2, "in net_send %d bytes on fd %d", size, writefd(cs));
#endif

    REQUIRE("Valid size", size <= INT32_MAX);

#ifdef EIF_WINDOWS
    if (0 != setjmp(env)) {
        errno = EPIPE;
        fprintf (stderr, "net_send: setjmp /= 0\n");
        return -1;
    }


    ReleaseSemaphore (writeev(cs),1,NULL);
    for (length = 0; length < size; length += error) {
        amount = size - length;
        if (amount > BUFSIZ)    /* do not write more than BUFSIZ */
            amount = BUFSIZ;
        fSuccess = WriteFile(writefd(cs), buf + length, (DWORD) amount, &error, NULL);
        if (!fSuccess) {
            fprintf (stderr, "net_send: write failed. fdesc = %p, errno = %u\n", writefd(cs), (unsigned int) GetLastError());
            return -1;
        }
    }


#else  /* (not) EIF_WINDOWS */

    oldpipe = signal(SIGPIPE, broken);	/* Trap SIGPIPE within this function */
    if (0 != setjmp(env)) {
        signal(SIGPIPE, oldpipe);
        errno = EPIPE;
        printf ("net_send: setjmp /= 0\n");
        return -1;
    }


    for (length = 0; length < size; length += error) {
        amount = size - length;
        if (amount > BUFSIZ) {	/* do not write more than BUFSIZ */
            amount = BUFSIZ;
        }
        error = write(writefd(cs), buf + length, amount);
        if (error == -1) {
            if (errno != EINTR) {
#ifdef EIF_VMS
                printf ("%s: net_send: write failed. fdesc = %i, errno = %i (VMS %i)\n",
                        eifrt_vms_get_progname (NULL,0), writefd(cs), errno, vaxc$errno);
                perror (" ");
#else
                printf ("net_send: write failed. fdesc = %i, errno = %i\n", writefd(cs),  errno);
#endif
                return -1;
            }
            else
                error = 0;		/* number of bytes send */
        }
    }
    signal(SIGPIPE, oldpipe);	/* restore default handler */

#endif /* EIF_WINDOWS */
    return 0;
}
Ejemplo n.º 17
0
int at_csq() {
	writefd("AT+CSQ");
	return 1;
}
Ejemplo n.º 18
0
static char *authenticate(struct conninfo *ci)
{
	char buf[1024];
	int n;
	int noauth=0;
	int uidpw=0;

	D(DEBUG_CONNECT)
		{
			fprintf(stderr, "DEBUG: Connection from ");
			printaddr(stderr, &ci->clientaddr);
			fprintf(stderr, "\n");
			fflush(stderr);
		}

	readfd(ci, buf, 2);

	if (buf[0] != 5)
	{
		fprintf(stderr, "ERR: ");
		printaddr(stderr, &ci->clientaddr);
		fprintf(stderr, " - received unknown protocol version: %d\n",
			(int)(unsigned char)buf[0]);
		fflush(stderr);
		exit(0);
	}

	readfd(ci, buf+2, (int)(unsigned char)buf[1]);

	for (n=0; n<(int)(unsigned char)buf[1]; n++)
		switch (buf[2+n]) {
		case 0:
			noauth=1;
			break;
		case 2:
			uidpw=1;
			break;
		}

	if (uidpw)
	{
		int uidl;
		int pwdl;
		char *pw;

		buf[0]=5;
		buf[1]=2;
		writefd(ci, buf, 2);

		readfd(ci, buf, 2);
		if (buf[0] != 1)
		{
			fprintf(stderr, "ERR: ");
			printaddr(stderr, &ci->clientaddr);
			fprintf(stderr, " - received unknown userid/password authentication version: %d\n",
				(int)(unsigned char)buf[0]);
			fflush(stderr);
			exit(0);
		}
		uidl=(int)(unsigned char)buf[1];

		readfd(ci, buf, uidl+1);

		pw=buf+uidl;
		pwdl=(int)(unsigned char)*pw;
		*pw++=0;
		readfd(ci, pw, pwdl);
		pw[pwdl]=0;

		if (buf[0] == 0)
		{
			/* Empty userid - no authentication */

			buf[0]=5;
			buf[1]=0;
			writefd(ci, buf, 2);

			D(DEBUG_CONNECT)
				{
					fprintf(stderr,"DEBUG: Empty userid - unauthenticated connection from ");
					printaddr(stderr, &ci->clientaddr);
					fprintf(stderr, "\n");
					fflush(stderr);
				}
			return NULL;
		}

		if (validateuseridpw(buf, pw))
		{
			buf[0]=5;
			buf[1]=1;
			writefd(ci, buf, 2);
			fprintf(stderr, "ERR: ");
			printaddr(stderr, &ci->clientaddr);
			fprintf(stderr, " - userid/password invalid.\n");
			fflush(stderr);
			fcntl(ci->clientfd, F_SETFL, 0);
			close(ci->clientfd);
			exit(0);
		}

		pw=strdup(buf);

		buf[0]=5;
		buf[1]=0;
		writefd(ci, buf, 2);

		D(DEBUG_CONNECT)
			{
				fprintf(stderr, "DEBUG: Connection from ");
				printaddr(stderr, &ci->clientaddr);
				fprintf(stderr, " authenticated as %s\n",
					buf);
				fflush(stderr);
			}

		if (!pw)
		{
			perror("ERR: malloc");
			exit(0);
		}
		return pw;
	}