Exemplo n.º 1
0
int main()
{
    Mkfifo(write_fifo_name, FILE_MODE);
    Mkfifo(read_fifo_name, FILE_MODE);

    cout << "Wating for connection...\n";
    int res = open(write_fifo_name, O_WRONLY);
    if(res == -1){
        cout << write_fifo_name << " open error!\n";
        exit(2);
    }else{
        cout << "Ser OK." << endl;
    }

    int cli = open(read_fifo_name, O_RDONLY);
    while(cli == -1){
         cout << "Waiting for Cli..." << endl;
         sleep(1);
         cli = open(read_fifo_name, O_RDONLY);
    }

    char sendbuf[BUF_LEN];
    char recvbuf[BUF_LEN];
    int chid = fork();
    if(chid == 0){
        while(1){
            cout << "Me:>";
            memset(sendbuf, 0x00, BUF_LEN);
            fgets(sendbuf, BUF_LEN, stdin);
            write(res, sendbuf, strlen(sendbuf) + 1);
        }
    }else if(chid > 0){
        sleep(1);
        while(1){
            memset(recvbuf, 0x00, BUF_LEN);
            int rst = read(cli, recvbuf, BUF_LEN);
            while(rst == 0 || rst == -1){
                sleep(1);
                cout << "Read once.\n";
                rst = read(cli, recvbuf, BUF_LEN);
            }
            cout << "Cli:>";
            cout << recvbuf;
            cout << endl;
        }
    }else if(chid < 0){
        cout << "Fork error!\n";
    }



    return 0;
}
Exemplo n.º 2
0
static FILE *getfile(PVStr(path),PCStr(mode),int fifo){
	const char *tmpdir;
	IStr(dgtmp,1024);
	CStr(npath,1024);
	int max = 64;
	FILE *fp;
	int fi;
	int fx;

	/*
	sprintf(path,"/tmp/sftpgw.%d.%d",getpid(),time(NULL));
	 */
	tmpdir = "/tmp";
	if( DELEGATE_getEnv("TMPDIR") )
	if( tmpdir = getTMPDIR() ){
		DEBUG("--SFTP TMPDIR=%s\n",tmpdir);
	}
	sprintf(dgtmp,"%s/dg-sftpgw",tmpdir);
	if( !fileIsdir(dgtmp) ){
		if( mkdirRX(dgtmp) != 0 ){
		}
	}
	sprintf(path,"%s/sftpgw-%d.%d",dgtmp,getpid(),itime(NULL));
	if( fifo ){
		if( Mkfifo(path,0600) != 0 ){
			DEBUG("--SFTP cant create FIFO: %s\n",path);
			return 0;
		}
		fp = NULL;
	}else{
		if( (fp = fopen(path,mode)) == 0 ){
			DEBUG("--SFTP cant create FILE: %s\n",path);
			return 0;
		}
	}
	fx = trand1(max);
	for( fi = 0; fi < max; fi++ ){
		/*
		sprintf(npath,"/tmp/sftpgw.%02x",fx);
		 */
		sprintf(npath,"%s/sftpgw-%02x",dgtmp,fx);
		if( rename(path,npath) == 0 ){
			DEBUG("--SFTP TEMP FILE: %s\n",npath);
			strcpy(path,npath);
			return fp;
		}
		fx = (fx + 1) % max;
	}
	DEBUG("--SFTP ERROR: can't rename %s\n",path);
	return fp;
}
Exemplo n.º 3
0
void test33a()
{				/* Test normal operation. */
  int stat_loc;			/* For the wait(&stat_loc) call. */

  subtest = 1;
  System("rm -rf ../DIR_33/*");

  /* To test normal access first make some files for real uid. */
  switch (fork()) {
      case -1:	printf("Can't fork\n");	break;
      case 0:
	alarm(20);
	setuid(getuid());	/* (Re)set the effective ids to the
				 * real ids. */
	setgid(getgid());
	System("> rwx; chmod 700 rwx");
	System("> rw_; chmod 600 rw_");
	System("> r_x; chmod 500 r_x");
	System("> r__; chmod 400 r__");
	System("> _wx; chmod 300 _wx");
	System("> _w_; chmod 200 _w_");
	System("> __x; chmod 100 __x");
	System("> ___; chmod 000 ___");
	exit(0);

      default:
	wait(&stat_loc);
	if (stat_loc != 0) e(1);/* Alarm? */
  }
  test_access();

  /* Let's test access() on directorys. */
  switch (fork()) {
      case -1:	printf("Can't fork\n");	break;
      case 0:
	alarm(20);
	setuid(getuid());	/* (Re)set the effective ids to the
				 * real ids. */
	setgid(getgid());
	System("rm -rf [_r][_w][_x]");
	System("mkdir rwx; chmod 700 rwx");
	System("mkdir rw_; chmod 600 rw_");
	System("mkdir r_x; chmod 500 r_x");
	System("mkdir r__; chmod 400 r__");
	System("mkdir _wx; chmod 300 _wx");
	System("mkdir _w_; chmod 200 _w_");
	System("mkdir __x; chmod 100 __x");
	System("mkdir ___; chmod 000 ___");
	exit(0);

      default:
	wait(&stat_loc);
	if (stat_loc != 0) e(2);/* Alarm? */
  }
  test_access();

  switch (fork()) {
      case -1:	printf("Can't fork\n");	break;
      case 0:
	alarm(20);
	setuid(getuid());	/* (Re)set the effective ids to the
				 * real ids. */
	setgid(getgid());
	System("rmdir [_r][_w][_x]");
	Mkfifo("rwx");
	System("chmod 700 rwx");
	Mkfifo("rw_");
	System("chmod 600 rw_");
	Mkfifo("r_x");
	System("chmod 500 r_x");
	Mkfifo("r__");
	System("chmod 400 r__");
	Mkfifo("_wx");
	System("chmod 300 _wx");
	Mkfifo("_w_");
	System("chmod 200 _w_");
	Mkfifo("__x");
	System("chmod 100 __x");
	Mkfifo("___");
	System("chmod 000 ___");
	exit(0);

      default:
	wait(&stat_loc);
	if (stat_loc != 0) e(3);/* Alarm? */
  }
  test_access();

  /* Remove all the fifos. */
  switch (fork()) {
      case -1:	printf("Can't fork\n");	break;
      case 0:
	alarm(20);
	setuid(getuid());
	setgid(getgid());
	System("rm -rf [_r][_w][_x]");
	exit(0);

      default:
	wait(&stat_loc);
	if (stat_loc != 0) e(4);/* Alarm? */
  }
}
Exemplo n.º 4
0
void test26c()
{				/* Test error returns. */
  int fd;
  int tube[2];
  int i, stat_loc;

  subtest = 3;
  System("rm -rf ../DIR_26/*");

  /* Fifo's can't be lseeked(). */
  Mkfifo("fifo");
  switch (fork()) {
      case -1:	printf("Can't fork\n");	break;
      case 0:
	alarm(3);		/* Try for max 3 secs. */
	if ((fd = open("fifo", O_RDONLY)) != 3) e(1);
	if (lseek(fd, (off_t) 0, SEEK_SET) != (off_t) - 1) e(2);
	if (errno != ESPIPE) e(3);
	if (close(fd) != 0) e(4);
	exit(0);
      default:
	if ((fd = open("fifo", O_WRONLY)) != 3) e(5);
	wait(&stat_loc);
	if (stat_loc != 0) e(6);/* Alarm? */
	if (close(fd) != 0) e(7);
  }

  /* Pipes can't be lseeked() eigther. */
  if (pipe(tube) != 0) e(8);
  switch (fork()) {
      case -1:	printf("Can't fork\n");	break;
      case 0:
	alarm(3);		/* Max 3 sconds wait. */
	if (lseek(tube[0], (off_t) 0, SEEK_SET) != (off_t) - 1) e(9);
	if (errno != ESPIPE) e(10);
	if (lseek(tube[1], (off_t) 0, SEEK_SET) != (off_t) - 1) e(11);
	if (errno != ESPIPE) e(12);
	exit(0);
      default:
	wait(&stat_loc);
	if (stat_loc != 0) e(14);	/* Alarm? */
  }

  /* Close the pipe. */
  if (close(tube[0]) != 0) e(15);
  if (close(tube[1]) != 0) e(16);

  /* Whence arument invalid. */
  System("echo -n contact > file");
  if ((fd = open("file", O_RDWR)) != 3) e(17);
  for (i = -1000; i < 1000; i++) {
	if (i == SEEK_SET || i == SEEK_END || i == SEEK_CUR) continue;
	if (lseek(fd, (off_t) 0, i) != (off_t) -1) e(18);
	if (errno != EINVAL) e(19);
  }
  if (close(fd) != 0) e(20);

  /* EBADF for bad fides. */
  for (i = -1000; i < 1000; i++) {
	if (i >= 0 && i < OPEN_MAX) continue;
	if (lseek(i, (off_t) 0, SEEK_SET) != (off_t) - 1) e(21);
	if (lseek(i, (off_t) 0, SEEK_END) != (off_t) - 1) e(22);
	if (lseek(i, (off_t) 0, SEEK_CUR) != (off_t) - 1) e(23);
  }
}
Exemplo n.º 5
0
/* open a named or unnamed pipe/fifo */
static int xioopen_fifo(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *fd, unsigned groups, int dummy1, int dummy2, int dummy3) {
   const char *pipename = argv[1];
   int rw = (xioflags & XIO_ACCMODE);
#if HAVE_STAT64
   struct stat64 pipstat;
#else
   struct stat pipstat;
#endif /* !HAVE_STAT64 */
   bool opt_unlink_early = false;
   bool opt_unlink_close = true;
   mode_t mode = 0666;
   int result;

   if (argc == 1) {
      return xioopen_fifo_unnamed(fd, fd->stream.opts);
   }

   if (argc != 2) {
      Error2("%s: wrong number of parameters (%d instead of 1)", argv[0], argc-1);
   }

   if (applyopts_single(&fd->stream, opts, PH_INIT) < 0)  return -1;
   applyopts(-1, opts, PH_INIT);

   retropt_bool(opts, OPT_UNLINK_EARLY, &opt_unlink_early);
   applyopts_named(pipename, opts, PH_EARLY);	/* umask! */
   applyopts(-1, opts, PH_EARLY);

   if (opt_unlink_early) {
      if (Unlink(pipename) < 0) {
	 if (errno == ENOENT) {
	    Warn2("unlink(%s): %s", pipename, strerror(errno));
	 } else {
	    Error2("unlink(%s): %s", pipename, strerror(errno));
	    return STAT_RETRYLATER;
	 }
      }
   }

   retropt_bool(opts, OPT_UNLINK_CLOSE, &opt_unlink_close);
   retropt_modet(opts, OPT_PERM, &mode);
   if (applyopts_named(pipename, opts, PH_EARLY) < 0) {
      return STAT_RETRYLATER;
   }
   if (applyopts_named(pipename, opts, PH_PREOPEN) < 0) {
      return STAT_RETRYLATER;
   }
   if (
#if HAVE_STAT64
       Stat64(pipename, &pipstat) < 0
#else
       Stat(pipename, &pipstat) < 0
#endif /* !HAVE_STAT64 */
      ) {
      if (errno != ENOENT) {
	 Error3("stat(\"%s\", %p): %s", pipename, &pipstat, strerror(errno));
      } else {
	 Debug1("xioopen_fifo(\"%s\"): does not exist, creating fifo", pipename);
#if 0
	 result = Mknod(pipename, S_IFIFO|mode, 0);
	 if (result < 0) {
	    Error3("mknod(%s, %d, 0): %s", pipename, mode, strerror(errno));
	    return STAT_RETRYLATER;
	 }
#else
	 result = Mkfifo(pipename, mode);
	 if (result < 0) {
	    Error3("mkfifo(%s, %d): %s", pipename, mode, strerror(errno));
	    return STAT_RETRYLATER;
	 }
#endif
	 Notice2("created named pipe \"%s\" for %s", pipename, ddirection[rw]);
	 applyopts_named(pipename, opts, PH_ALL);

      }
      if (opt_unlink_close) {
	 if ((fd->stream.unlink_close = strdup(pipename)) == NULL) {
	    Error1("strdup(\"%s\"): out of memory", pipename);
	 }
	 fd->stream.opt_unlink_close = true;
      }
   } else {
      /* exists */
      Debug1("xioopen_fifo(\"%s\"): already exist, opening it", pipename);
      Notice3("opening %s \"%s\" for %s",
	      filetypenames[(pipstat.st_mode&S_IFMT)>>12],
	      pipename, ddirection[rw]);
      /*applyopts_early(pipename, opts);*/
      applyopts_named(pipename, opts, PH_EARLY);
   }

   if ((result = _xioopen_open(pipename, rw, opts)) < 0) {
      return result;
   }
   fd->stream.fd = result;

   applyopts_named(pipename, opts, PH_FD);
   applyopts(fd->stream.fd, opts, PH_FD);
   applyopts_cloexec(fd->stream.fd, opts);
   return _xio_openlate(&fd->stream, opts);
}