Example #1
0
void open_terminal(int *child_fd, int *parent_fd) {
    int fd1, fd2, i;
    char opentermw[5+OPEN_MAX+1];
    char opentermr[5+OPEN_MAX+1];
    char *term[] = {"f","e","d","c","b","a","9","8","7","6","5","4","3","2","1"};
#define TERMS (sizeof(term)/sizeof(term[0]))

    if (!child_fd || !parent_fd) exit(EXIT_FAILURE);

    for (i = 0; i < TERMS; i++) {
        snprintf(opentermw, 5+OPEN_MAX, "/dev/ttyp%s", term[i]);
        snprintf(opentermr, 5+OPEN_MAX, "/dev/ptyp%s", term[i]);

        /* Open master terminal for writing */
        if((fd1 = open(opentermw, O_WRONLY)) == -1) continue;

        /* Open slave terminal for reading */
        if((fd2 = open(opentermr, O_RDONLY)) == -1) {
            close(fd1);
            continue;
        }

        *child_fd = fd1;
        *parent_fd = fd2;
        return;
    }

    /* If we get here we failed to find a terminal pair */
    exit(EXIT_FAILURE);
}
/*
 * Write to a file that we create with mode 0000.
 * POSIX standard declares that when a file is created with ordinary O_CREAT:
 * the provided mode only applies after the creation of the file.
 */
int main(int argc, char *argv[])
{
	/* Should be able to write file even if we set 0 perms. */
	int fd = open("/tmp/test_file", O_CREAT | O_TRUNC | O_RDWR, 0);

	if (fd == -1) {
		printf("Error: Could not open file!\n");
		exit(EXIT_FAILURE);
	}

	if (write(fd, "hello\n", 7) == -1) {
		printf("Error: Failed to write to file\n");
		exit(EXIT_FAILURE);
	}

	printf("No failures detected, we wrote to a file with mode 0000\n");
	close(fd);

	/* Now, when the file is closed, we can't open it again because we
	 * have no permissions to do so.
	 */
	fd = open("/tmp/test_file", O_CREAT | O_TRUNC | O_RDWR, 0);

	if (fd == -1) {
		printf("Error: Could not reopen file, permission denied\n");
		exit(EXIT_FAILURE);
	}

	exit(EXIT_SUCCESS);
}
Example #3
0
void libc_system_init(const char* tty_name)
{
#ifdef RT_USING_DFS
	int fd;

#ifndef RT_USING_DFS_DEVFS
#error Please enable devfs by defining RT_USING_DFS_DEVFS in rtconfig.h
#endif

	/* init console device */
	rt_console_init(tty_name);

	/* open console as stdin/stdout/stderr */
	fd = open("/dev/console", O_RDONLY, 0);	/* for stdin */
	fd = open("/dev/console", O_WRONLY, 0);	/* for stdout */
	fd = open("/dev/console", O_WRONLY, 0);	/* for stderr */
#endif

	/* set PATH and HOME */
	putenv("PATH=/");
	putenv("HOME=/");

#ifdef RT_USING_PTHREADS
	pthread_system_init();
#endif
}
int main(int argc, char* argv[]) {
    char buffer[BUFSIZE]; // buffer for reading and writing
    if (argc != 3) { // this is the only valid structure for arguments
        printf("usage: %s source dest\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    // infile is only for reading
    int inFile = open(argv[1], O_RDONLY);
    // outfile is for writing - or overwriting if it already exists
    int outFile = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR, S_IWUSR);

    // number of bytes read, potentially negative (for error condition)
    ssize_t numRead;
    // how many nulls we have in a row - always positive (unsigned)
    size_t holeSize = 0;

    // read in up to BUFSIZE
    while ((numRead = read(inFile, buffer, BUFSIZE)) > 0) {
        // for each byte of buffer
        for (int i = 0; i < numRead; i++) {
            // if the byte is a null, increment our counter of nulls
            if (buffer[i] == '\0') {
                holeSize += 1;
            } else if (holeSize > 0) {
                // if the byte is not a null, and is the first byte after some nulls, we should seek
                // the number of null bytes past. so if we have 0 0 0 0 y, we would have a null counter
                // of four, and then would seek right past them to y
                lseek(outFile, holeSize, SEEK_CUR);
                // now write just one byte - just our new non-null byte to the first location for y in
                // the new file
                write(outFile, &buffer[i], 1);
                // we've seeked past the nulls, so clear our counter
                holeSize = 0;
            } else {
                // in this case, we are a non-null after a non-null (or the first byte of the file
                // which happens to be non-null). so we just write one byte and keep moving
                write(outFile, &buffer[i], 1);
            }
        }
    }

    // error reading (e.g. perms)
    if (numRead == -1) {
        perror("reading");
        exit(EXIT_FAILURE);
    }

    // error closing (but who cares, we're exiting anyway)
    if (close(inFile) == -1) {
        perror("close infile");
        exit(EXIT_FAILURE);
    }

    if (close(outFile) == -1) {
        perror("close outfile");
        exit(EXIT_FAILURE);
    }

    return 0;
}
Example #5
0
int basicExecute(char* com,int mode,char* input,char* output,char** args)
{
   int st,rv,fd;
   pid_t child = fork();
   switch(child) {
      case -1: {
         printf("fork failure\n");
      }break;
      case 0: { /* Child */
         if (mode & R_INP) {
            close(0);
            open(input,O_RDONLY);
         }
         if (mode & R_OUTP) {
            close(1);
            fd = open(output,O_WRONLY|O_TRUNC|O_CREAT,0666);
         }
         if (mode & R_APPD) {
            close(1);
            fd = open(output,O_WRONLY|O_APPEND|O_CREAT,0666);
         }
         rv = execvp(com,args);
         if (rv)
            printf("execvp failed with status %d ->[%s]\n",errno,strerror(errno));
      }break;
      default:{ /* parent */
         rv = waitpid(child,&st,0);
         assert(rv==child);
      }
   }
   return 1;
}
Example #6
0
File: cmd.c Project: embedthis/mpr
static int makeChannel(MprCmd *cmd, int index)
{
    MprCmdFile      *file;
    int             nonBlock;
    static int      tempSeed = 0;

    file = &cmd->files[index];
    file->name = sfmt("/pipe/%s_%d_%d", ME_NAME, taskIdSelf(), tempSeed++);

    if (pipeDevCreate(file->name, 6, ME_BUFSIZE) < 0) {
        mprLog("error mpr cmd", 0, "Cannot create pipes to run %s", cmd->program);
        return MPR_ERR_CANT_OPEN;
    }
    /*
        Open the server end of the pipe. MPR_CMD_STDIN is from the client's perspective.
     */
    if (index == MPR_CMD_STDIN) {
        file->fd = open(file->name, O_WRONLY, 0644);
    } else {
        file->fd = open(file->name, O_RDONLY, 0644);
    }
    if (file->fd < 0) {
        mprLog("error mpr cmd", 0, "Cannot create stdio pipes. Err %d", mprGetOsError());
        return MPR_ERR_CANT_CREATE;
    }
    nonBlock = 1;
    ioctl(file->fd, FIONBIO, (int) &nonBlock);
    return 0;
}
Example #7
0
void exec_cmd_opt_in_write(char** cmd1, char* infile, char* outfile)
{
    pid_t pid;
    logInfo("exec_cmd_opt_in_write: Forking");
    if ((pid = fork()) == -1)
    {
        logInfo("exec_cmd_opt_in_write: Error Forking");
        perror("fork error");
    }
    else if (pid == 0)
    {
        logInfo("exec_cmd_opt_in_write: Opening outfile");
        if(outfile[0] != '\0')
        {
            int outFD = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
            if(infile[0] != '\0')
            {
                int inFD = open(infile, O_RDONLY);
                dup2(inFD, 0);
                close(inFD);
            }
            dup2(outFD, 1);
            close(outFD);
            
            logInfo("exec_cmd_opt_in_write: Executing command");
            execvp(cmd1[0], cmd1);
            exit(1);
        }
    }
    else
    {
        waitpid(pid, NULL, 0);
        
    }
}
Example #8
0
int main(int argc,char *argv[])
{
	int fd1,fd2;
	char buff1[1024];
	char buff2[1024];
	int m,n;
	fd1=open(argv[1],O_RDONLY);
	if(fd1<0)
	{
		printf("open the first file failed!\n");
		return -1;
	}
	fd2=open(argv[2],O_RDONLY);
	if(fd2<0)
	{
		printf("open the second file failed!\n");
		return -1;
	}
	while(1)
	{	
		n=read(fd1,buff1,1024);
		m=read(fd2,buff2,1024);
		if(strcmp(buff1,buff2)!=0 || n!=m )
		{
			printf("not the same!\n");
			return -1;
		}
		if(n==0)
			break;		
	}
	printf("the two files are the same!\n");
	return 0;
}
Example #9
0
int		ft_consmap(t_env *env)
{
	char	*line;
	int		nb_line;
	int		fd;

	line = NULL;
	if ((fd = open("./maplaby.txt", O_RDONLY)) > 2)
	{
		nb_line = ft_count_line(fd, line);
		env->map = (int**)malloc(sizeof(int*) * (nb_line + 1));
		env->map[nb_line] = NULL;
	}
	close(fd);
	if ((fd = open("./maplaby.txt", O_RDONLY)) > 2)
	{
		nb_line = 0;
		while (get_next_line(fd, &line) > 0)
		{
			nb_line += ft_consline(env, line, nb_line);
			free(line);
		}
		free(line);
	}
	close(fd);
	return (0);
}
Example #10
0
int main(int argc, char *argv[]){
	int file;
	int read_dir;

	if (argc != 2){
		fprintf(stderr, "Usage: %s dir_name\n", argv[0]);
		exit(1);
	}

	open("files.txt",O_TRUNC );
	file = open("files.txt",O_WRONLY | O_APPEND | O_CREAT , S_IRWXG | S_IRWXU | S_IROTH);

	if(file == -1){
		perror("file ERROR");
		close(file);
		exit(1);
	}

	read_dir = read_directory(argv[1],file);

	if(read_dir == 1){
		perror(argv[1]);
		close(file);
		exit(2);
	}else if(read_dir == 2){
		perror("lstat ERROR");
		close(file);
		exit(3);
	}

	close(file);
	execlp("sort", "sort", FILE_NAME,"-no", FILE_NAME, (char*)NULL);

	exit(0);
}
Example #11
0
int
main (void)
{
	int fd_r;
	int fd_w;
	int res;
	int n;
	char buf[BUFF_SIZE+1];
	char *path_r = "test.txt";
	char *path_w = "write.txt";
	fd_r = open(path_r, O_CREAT | O_RDWR, 0666);
	fd_w = open(path_w, O_CREAT | O_RDWR, 0666);
	//fd_w = open(path_w, O_CREAT | O_RDWR | O_SYNC, 0666);

	while ((n = read(fd_r, buf, BUFF_SIZE)) > 0){
		if (write(fd_w, buf, n) != n){
			printf("write error\n");
		}
		res = fdatasync(fd_w);
	}	
		//res = fdatasync(fd_w);

	close(fd_r);
	close(fd_w);

	return 0;
}
Example #12
0
static void openFiles( void )
{
    int objhdl;

    objhdl = open( ObjFileName, O_RDONLY | O_BINARY );
    if( objhdl != -1 ) {
        if( ListFileName != NULL ) {
            OutputDest = open( ListFileName, O_WRONLY | O_CREAT | O_TRUNC, PMODE_RW );
            if( OutputDest == -1 )
                openError( ListFileName );
            ChangePrintDest( OutputDest );
        }
        objFileLen = filelength( objhdl );
        if( objFileLen == 0 ) {
            LeaveProgram( RC_OKAY, WHERE_OBJ_ZERO_LEN );
        }
        objFileBuf = MemAlloc( objFileLen );
        objFilePos = 0;
        if( posix_read( objhdl, objFileBuf, objFileLen ) == -1 ) {
            openError( ObjFileName );
        }
        close( objhdl );
    } else {
        openError( ObjFileName );
    }
}
Example #13
0
/**
 * Tests to see if files can be moved/renamed between two directories.
 * Returns 1 on success, 0 on failure
 */
int dirs_movable(const char *dir1, const char *dir2)
{
    char tempf1[MAXPATHNAME], tempf2[MAXPATHNAME];
    int fd;

    snprintf(tempf1, sizeof(tempf1)-1, "%s%c_uftptmp1", dir1, PATH_SEP);
    tempf1[sizeof(tempf1)-1] = '\x0';
    snprintf(tempf2, sizeof(tempf1)-1, "%s%c_uftptmp2", dir2, PATH_SEP);
    tempf2[sizeof(tempf2)-1] = '\x0';
    if ((fd = open(tempf1, O_WRONLY | O_CREAT, 0644)) < 0) {
        fprintf(stderr, "couldn't write to directory %s: %s\n",
                         dir1, strerror(errno));
        return 0;
    }
    close(fd);
    if ((fd = open(tempf2, O_WRONLY | O_CREAT, 0644)) < 0) {
        fprintf(stderr, "couldn't write to directory %s: %s\n",
                         dir2, strerror(errno));
        return 0;
    }
    close(fd);
    unlink(tempf2);
    if (rename(tempf1, tempf2) == -1) {
        fprintf(stderr, "couldn't move between directories %s and %s: %s\n",
                         dir1, dir2, strerror(errno));
        unlink(tempf1);
        return 0;
    }
    unlink(tempf1);
    unlink(tempf2);
    return 1;
}
Example #14
0
static int
sunos_usb_open_ep0(sunos_dev_handle_priv_t *hpriv, sunos_dev_priv_t *dpriv)
{
	char filename[PATH_MAX + 1];

	if (hpriv->eps[0].datafd > 0) {

		return (LIBUSB_SUCCESS);
	}
	snprintf(filename, PATH_MAX, "%s/cntrl0", dpriv->ugenpath);

	usbi_dbg("opening %s", filename);
	hpriv->eps[0].datafd = open(filename, O_RDWR);
	if (hpriv->eps[0].datafd < 0) {
		return(_errno_to_libusb(errno));
	}

	snprintf(filename, PATH_MAX, "%s/cntrl0stat", dpriv->ugenpath);
	hpriv->eps[0].statfd = open(filename, O_RDONLY);
	if (hpriv->eps[0].statfd < 0) {
		close(hpriv->eps[0].datafd);
		hpriv->eps[0].datafd = -1;

		return(_errno_to_libusb(errno));
	}

	return (LIBUSB_SUCCESS);
}
Example #15
0
static void create_daemon(void)
{
    pid_t pid;
    uid_t euid;

    pid = fork();
    if (pid == -1) {
        perror("fork");
        exit(1);
    } else if (pid != 0) {
        exit(0);
    }

    close(0);
    close(1);
    close(2);
    open("/dev/null", O_RDWR);
    open("/dev/null", O_RDWR);
    open("/dev/null", O_RDWR);

    if (chdir("/")) {}	/* Silence warn_unused_result warning */
    (void) setsid();
    euid = geteuid();
    if (setreuid(euid, euid) < 0)
        die("setreuid");
}
Example #16
0
void	ft_load_text(t_env *env)
{
	char	*line;
	int		nb_line;
	int		fd;

	line = NULL;
	if ((fd = open("./text.txt", O_RDONLY)) > 2)
	{
		nb_line = ft_count_line(fd, line);
		env->text = (t_text**)malloc(sizeof(t_text*) * (nb_line + 1));
		env->text[nb_line] = NULL;
	}
	close(fd);
	if ((fd = open("./text.txt", O_RDONLY)) > 2)
	{
		nb_line = 0;
		while (get_next_line(fd, &line) > 0)
		{
			ft_set_text(env, line, nb_line++);
			free(line);
		}
	}
	close(fd);
	free(line);
}
Example #17
0
int main()
{
    pid_t pid;
    int result = 0;

    if((pid = fork()) < 0){
	err_sys("fork error");
    }else if(pid == 0){
	int result  = open("/dev/tty", O_RDWR);
	if(result < 0){
	    printf("In child process can't open /dev/tty before setsid()\n");
	}else{
	    printf("In child process can open /dev/tty before setsid()\n");
	}
	printf("In child process,before setsid() the process group id is %d  and process id is %d \n",getpgrp(),getpid());
	if (setsid() < -1) {
	      printf("setsid error\n");
	      exit(1);
	}
	printf("In child process,before setsid() the process group id is %d and process id is %d \n",getpgrp(),getpid());
	result  = open("/dev/tty", O_RDWR);
	if(result < 0){
	    printf("In child process can't open /dev/tty after setsid()\n");
	}
    }else{
	result  = open("/dev/tty", O_RDWR);
	if(result < 0){
	    printf("In parent process can't open /dev/tty after setsid()\n");
	    exit(1);
	}
	printf("In parent process,process group id is %d , process id is %d \n",getpgrp(),getpid());
    }
    return 0;
}
Example #18
0
File: init.c Project: cams93/os-xv6
int
main(void)
{
  int pid, wpid;

  if(open("console", O_RDWR) < 0){
    mknod("console", 1, 1);
    open("console", O_RDWR);
  }
  dup(0);  // stdout
  dup(0);  // stderr

  for(;;){
    pid = fork();
    if(pid < 0){
      printf(1, "init: fork failed\n");
      exit();
    }
    if(pid == 0){
      exec("login", argv);  //ejecutar login cuando termine solo modificar sh -> login
      printf(1, "init: exec sh failed\n");
      //exit();
    }
    wait();
    while((wpid=wait()) >= 0 && wpid != pid) //ocupamos esto
      printf(1, "zombie!\n");  //break para que termine este e init crea otro login
  }
}
Example #19
0
void exec_cmd_opt_in_append(char** cmd1, char* infile, char* outfile)
{
    pid_t pid;
    logInfo("exec_cmd_opt_in: Starting fork");
    if ((pid = fork()) == -1)
    {
        logInfo("exec_cmd_opt_in: fork error");
        perror("fork error");
    }
    else if (pid == 0)
    {
        if(outfile[0] != '\0')
        {
            logInfo("exec_cmd_opt_in: Opening outfile");
            int outFD = open(outfile, O_APPEND | O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
            if(infile[0] != '\0')
            {
                logInfo("exec_cmd_opt_in: Opening infile");
                int inFD = open(infile, O_RDONLY);
                dup2(inFD, 0);
                close(inFD);
            }
            dup2(outFD, 1);
            close(outFD);
            logInfo("exec_cmd_opt_in: Executing command");
            execvp(cmd1[0], cmd1);
            exit(1);
        }
    }
    else
    {
        waitpid(pid, NULL, 0);
        logInfo("exec_cmd_opt_in: Done");
    }
}
Example #20
0
int main(int argc, char **argv){
    if(argc == 1){
        fprintf(stderr, "copy: Missing arguments list\nTry 'copy -h' for help\n");
        return 1;
    }
    
    int c;
    int mflag = 0;
    
    opterr = 0;

    while((c = getopt(argc, argv, "hm")) != -1){        //if set "hm:" then -m requirs 1 argument and i have no idea how to force 2.
        switch (c){
            case 'h':
                printf("Usage:\n");
                printf(" copy <file_name> <new_file_name>\n");
                printf(" copy [-m] <file_name> <new_file_name>\n");
                return 0;
            case 'm':
                if(argc != 4){
                    fprintf(stderr, "copy: Option -m requires 2 arguments.\nTry 'copy -h' for help\n");
                    return 1;
                }
                mflag = 1;
                break;
            case '?':
                if(isprint (optopt))
                    fprintf(stderr, "copy: Unknown option '-%c'\nTry 'copy -h' for help\n", optopt);
                else
                    fprintf(stderr, "copy: Unknown option character `\\x%x`.\n", optopt);
                return 1;
            default:
                fprintf(stderr, "copy: abort()");
                abort();
        }
    }   //while
    //Possible cases: 
        //argc == 4 && mflag == 1
        //argc == 3 && mflag == 0
    if(argc != 3 && mflag == 0){
        fprintf(stderr, "copy: Wrong arguments list\nTry 'copy -h' for help\n");
        return 1;
    }
    
    int fd_from, fd_to;     //declaring file descriptors

    if((fd_from = open(argv[mflag+1], O_RDONLY)) == -1 || (fd_to = open(argv[mflag+2], O_RDWR | O_CREAT, 0666)) == -1){   //O_RDWD
        perror("open");
        return 1;
    }

    if(mflag == 0) //without -m option
        copy_read_write(fd_from, fd_to);
    else
        copy_mmap(fd_from, fd_to);

    close(fd_from);
    close(fd_to);
    return 0;
}
Example #21
0
File: GUIInit.c Project: orlv/fos
void GUIInit()
{
  int gui_handle;

  do {
    gui_handle = open("/dev/pgs/main", 0);
    sched_yield();
  } while (!gui_handle);
  __gui = (fd_t) gui_handle;

  int gui_canvas_h;

  do {
    gui_canvas_h = open("/dev/pgs/mapping", 0);
    sched_yield();
  } while (!gui_canvas_h);
  __gui_canvas = (fd_t) gui_canvas_h;
  struct message msg;

  msg.flags = 0;
  msg.send_size = 0;
  msg.recv_size = 0;
  msg.tid = __gui->thread;
  msg.arg[0] = WIN_CMD_SCREEN_INFO;
  send(&msg);
  screen_width = msg.arg[0];
  screen_height = msg.arg[1];
}
Example #22
0
static int process_io_open(void)
{
	uid_t uid;

	if (proc_io_fd != -1)
		return proc_io_fd;

	if (proc_io_disabled)
		return -1;
	proc_io_fd = open(PROC_IO_PATH, O_RDONLY);
	if (proc_io_fd == -1 && errno == EACCES) {
		/* kludge: if we're running with permissions temporarily
		   dropped, get them temporarily back so we can open
		   /proc/self/io. */
		uid = geteuid();
		if (seteuid(0) == 0) {
			proc_io_fd = open(PROC_IO_PATH, O_RDONLY);
			if (seteuid(uid) < 0) {
				/* oops, this is bad */
				i_fatal("stats: seteuid(%s) failed", dec2str(uid));
			}
		}
		errno = EACCES;
	}
	if (proc_io_fd == -1) {
		if (errno != ENOENT)
			i_error("open(%s) failed: %m", PROC_IO_PATH);
		proc_io_disabled = TRUE;
		return -1;
	}
	return proc_io_fd;
}
Example #23
0
static void setup(void)
{
	int ret;

	TEST_PAUSE;

	tst_tmpdir();

	in_fd = creat(in_file, 0700);
	if (in_fd == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "Create %s failed", in_file);

	ret = write(in_fd, TEST_MSG_IN, strlen(TEST_MSG_IN));
	if (ret == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "Write %s failed", in_file);
	close(in_fd);

	in_fd = open(in_file, O_RDONLY);
	if (in_fd == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "Open %s failed", in_file);

	out_fd = open(out_file, O_TRUNC | O_CREAT | O_RDWR, 0777);
	if (out_fd == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "Open %s failed", out_file);
	ret = write(out_fd, TEST_MSG_OUT, strlen(TEST_MSG_OUT));
	if (ret == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "Write %s failed", out_file);
}
Example #24
0
int
closedesc_all(const int closestdin)
{
    int fodder;

    if (closestdin != 0) {
        (void) close(0);
        if ((fodder = open(_PATH_DEVNULL, O_RDONLY)) == -1) {
            return -1;
        }
        (void) dup2(fodder, 0);
        if (fodder > 0) {
            (void) close(fodder);
        }
    }
    if ((fodder = open(_PATH_DEVNULL, O_WRONLY)) == -1) {
        return -1;
    }
    (void) dup2(fodder, 1);
    (void) dup2(1, 2);
    if (fodder > 2) {
        (void) close(fodder);
    }

    return 0;
}
Example #25
0
int setupCommandPipeline(Command* c) 
{ 
   assert(c->_kind == pipelineCMD);
   int st,fd[2];
   if (c->_mode & R_INP)      
      setStageInput(c->_stages[0],open(c->_input,O_RDONLY));   
   if (c->_mode & R_OUTP) 
      setStageOutput(c->_stages[c->_nbs-1],open(c->_output,O_WRONLY|O_TRUNC|O_CREAT,0666));   
   if (c->_mode & R_APPD) 
      setStageOutput(c->_stages[c->_nbs-1],open(c->_output,O_WRONLY|O_APPEND|O_CREAT,0666));

   for(int k=0;k<c->_nbs-1;k++) {
      st = pipe(fd);
      if (st!=0) {
         perror(strerror(errno));
         exit(1);
      }
      setStageOutput(c->_stages[k],fd[1]);
      setStageInput(c->_stages[k+1],fd[0]);
   }   
   for(int k=0;k<c->_nbs;k++) 
      spawnStage(c->_stages[k]);
   for(int k=0;k<c->_nbs;k++) 
      waitStage(c->_stages[k]);   
   return 1;
}
Example #26
0
/** @todo O_DIRECT is broken in older linuxes (eg 2.4).  The build script should disable it on such platforms. */
stasis_page_handle_t*  openPageFile(stasis_log_t * log, stasis_dirty_page_table_t * dpt) {
  stasis_page_handle_t * ret = stasis_alloc(stasis_page_handle_t);
  ret->read = pfPageRead;
  ret->write = pfPageWrite;
  ret->force_file = pfForcePageFile;
  ret->force_range = pfForceRangePageFile;
  ret->close = pfClosePageFile;
  ret->log = log;
  ret->dirtyPages = dpt;
  DEBUG("Opening storefile.\n");

#ifdef PAGE_FILE_O_DIRECT
  stable = open (stasis_store_file_name,
                 O_CREAT | O_RDWR | O_DIRECT, FILE_PERM);
#else
  stable = open (stasis_store_file_name,
                 O_CREAT | O_RDWR, FILE_PERM);
#endif
  if(!pageFile_isDurable) {
    fprintf(stderr, "\n**********\n");
    fprintf  (stderr, "pageFile.c: pageFile_isDurable==0; the page file will not force writes to disk.\n");
    fprintf  (stderr, "            Transactions will not be durable if the system crashes.\n**********\n");
  }
  if(stable == -1) {
    perror("couldn't open storefile");
    fflush(NULL);
    abort();
  }

  pthread_mutex_init(&stable_mutex, NULL);
  return ret;
}
Example #27
0
int
_access_endpoint(struct libusb_transfer *transfer)
{
	struct handle_priv *hpriv;
	struct device_priv *dpriv;
	char *s, devnode[16];
	int fd, endpt;
	mode_t mode;

	hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
	dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;

	endpt = UE_GET_ADDR(transfer->endpoint);
	mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;

	usbi_dbg("endpoint %d mode %d", endpt, mode);

	if (hpriv->endpoints[endpt] < 0) {
		/* Pick the right node given the control one */
		strlcpy(devnode, dpriv->devnode, sizeof(devnode));
		s = strchr(devnode, '.');
		snprintf(s, 4, ".%02d", endpt);

		/* We may need to read/write to the same endpoint later. */
		if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
			if ((fd = open(devnode, mode)) < 0)
				return (-1);

		hpriv->endpoints[endpt] = fd;
	}

	return (hpriv->endpoints[endpt]);
}
Example #28
0
int main(int argc , char *argv[])
{
	int fds1,fds2,a;
	char ch;
	if (argc !=3 )
		{
			printf("exactly 2 arguments expected");
			return 0;
		}
	else
		{
			fds1=open(argv[1],O_RDONLY);
			fds2=open(argv[2],O_WRONLY | O_CREAT | O_TRUNC);
			a=read(fds1,&ch,1);
			while(a!=0 && a!=-1)
			{
				printf("%c",ch);

				a=write(fds2,&ch,1);
				if(a==-1) continue;
				a=read(fds1,&ch,1);
			}
		}
			
}
Example #29
0
int main(int argc, char **argv)
{
    int id = geteuid();
    if (id != 0)
    {
        printf("You must run this program as root\n");
        return -1;
    }
    if (argc != 2)
    {
        printf("You must specify the rpm for the fan, value must be between 2000 and 6200\n");
        return -1;
    }
    int parameter = atoi(argv[1]);
    
    if (parameter >= 2000 && parameter <= 6200)
    {
        int file;
        file = open("/sys/devices/platform/applesmc.768/fan1_manual", O_WRONLY | O_CREAT);
        write(file, "1", (ssize_t)1);
        close(file);
        
        file = open("/sys/devices/platform/applesmc.768/fan1_output", O_WRONLY | O_CREAT);
        write(file, argv[1], (ssize_t)4);
        close(file);
        printf("Fan RPM changed to %s\n", argv[1]);
    }
    else
    {
        printf("Value must be between 2000 and 6200\n");
    }
    return 0;
}
Example #30
0
wxJoystick::wxJoystick(int joystick)
    : m_device(-1),
      m_joystick(joystick),
      m_thread(NULL)
{
    wxString dev_name;

    // old /dev structure
    dev_name.Printf( wxT("/dev/js%d"), (joystick == wxJOYSTICK1) ? 0 : 1);
    m_device = open(dev_name.fn_str(), O_RDONLY);

    // new /dev structure with "input" subdirectory
    if (m_device == -1)
    {
        dev_name.Printf( wxT("/dev/input/js%d"), (joystick == wxJOYSTICK1) ? 0 : 1);
        m_device = open(dev_name.fn_str(), O_RDONLY);
    }

    if (m_device != -1)
    {
        m_thread = new wxJoystickThread(m_device, m_joystick);
        m_thread->Create();
        m_thread->Run();
    }
}