Пример #1
0
void 
devmem_init(void) {
	resmgr_attr_t	rattr;

	// allocate mem directory entry
	memset(&rattr, 0x00, sizeof(rattr));
	rattr.flags = RESMGR_FLAG_CROSS_ENDIAN;

	mem_handle = resmgr_attach(dpp, NULL, "/dev/mem", 0, 0, &mem_connect_funcs, &mem_io_funcs, 0);
	resmgr_attach(dpp, &rattr, "/dev/shmem", 0, _RESMGR_FLAG_DIR, &shmem_connect_funcs, &mem_io_funcs, 0);
	//RUSH3: Decide if "/dev/tymem" should be a fully usable directory
	//RUSH3: structure
	resmgr_attach(dpp, NULL, "/dev/tymem", _FTYPE_TYMEM, _RESMGR_FLAG_DIR, &tymem_connect_funcs, &tymem_io_funcs, 0);
	rsrcdbmgr_proc_devno("dev", &mem_devno, -1, 0);
}
Пример #2
0
int shm_resinit(dispatch_t *dpp)
{
	int ret;
	
	/* shmget() doesn't talk about umask but shm_open() does. */
	umask(0);

	iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &shm_connect,
	    _RESMGR_IO_NFUNCS, &shm_io);
	shm_io.msg = shm_msg;
    
	iofunc_attr_init(&shm_ioattr, 0666, 0, 0);
	shm_resid = resmgr_attach(dpp, 0, PATH_SHMMGR, _FTYPE_ANY, 0,
	    &shm_connect, &shm_io, &shm_ioattr);
	if (shm_resid == -1) {
		return -1;
	}
    
	if ((ret = pthread_create(NULL, NULL, shmres_handle, dpp)) != EOK) {
		errno = ret;
		resmgr_detach(dpp, shm_resid, _RESMGR_DETACH_ALL);
		return -1;
	}
	
	return 0;
}
Пример #3
0
static int
create_device(char *name, enum _file_type type, MQDEV *dev) {
	char	 path[PATH_MAX];
	unsigned	*count, limit;
	int			id;

	if (type == _FTYPE_MQUEUE)
		count = &num_mq, limit = max_num_mq;
	else
		count = &num_sem, limit = max_num_sem;
	if (*count >= limit)
		errno = ENFILE, id = -1;
	else if ((id = resmgr_attach(dpp, NULL, strcat(strcpy(path, "/"), name), type, 0, &mq_connect_funcs, &mq_io_funcs, dev)) != -1)
		++*count;
	return(id);
}
Пример #4
0
int main(int argc, char *argv[])
{
	dispatch_t              *dpp;
	resmgr_attr_t           resmgr_attr;
	dispatch_context_t      *ctp;
	resmgr_connect_funcs_t  connect_funcs;
	resmgr_io_funcs_t       io_funcs;
	iofunc_attr_t           io_attr;

	printf("Start resource manager\n");

	fifoHandler = init_fifo();
	pthread_mutex_init(&resource_mutex, NULL);

	// create dispatch.
	if (!(dpp = dispatch_create()))
		error("dispatch_create()");

	// initialize resource manager attributes.
	memset(&resmgr_attr, 0, sizeof(resmgr_attr));
	resmgr_attr.nparts_max = 1;
	resmgr_attr.msg_max_size = 2048;

	// set standard connect and io functions.
	iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs,
	_RESMGR_IO_NFUNCS, &io_funcs);
	iofunc_attr_init(&io_attr, S_IFNAM | 0666, 0, 0);

	io_funcs.read = io_read;
	io_funcs.write = io_write;

	// establish resource manager
	if (resmgr_attach(dpp, &resmgr_attr, "/dev/myresource", _FTYPE_ANY, 0, &connect_funcs, &io_funcs, &io_attr) < 0)
		error("resmgr_attach()");

	// wait forever, handling messages.
	ctp = dispatch_context_alloc(dpp);
	while(1)
	{
		if (!(ctp = dispatch_block(ctp)))
			error("dispatch_block()");
		dispatch_handler(ctp);
	}
	exit(EXIT_SUCCESS);
}
Пример #5
0
Файл: main.c Проект: orlv/fos
int connect_pci() {
  resmgr_attach("/dev/uip");
  char *buffer = malloc(RECV_BUF_SIZE);
  struct message msg;
  while (1) {
    msg.tid = 0;
    msg.recv_size = RECV_BUF_SIZE;
    msg.recv_buf = buffer;
    msg.flags = 0;
    receive(&msg);
    switch (msg.arg[0]) {
    case FS_CMD_ACCESS:
      msg.arg[0] = 1;
      msg.arg[1] = RECV_BUF_SIZE;
      msg.arg[2] = NO_ERR;
      break;

    case PCI_NOTIFY_NEW: {
      if(msg.arg[1] == 0x10EC && msg.arg[2] == 0x8139) {
        printf("Realtek 8139 starting up\n");
        pci_addr_t *addr = malloc(sizeof(pci_addr_t));
        memcpy(addr, buffer, sizeof(pci_addr_t));
        if(rtl8139_init(addr) < 0) {
          printf("Failure!\n");
          free(addr);
          return -1;
       } else {
          printf("Card started up successfully\n");
	  return 0;
       }
      }
      break;
    }
    default:
      msg.arg[0] = 0;
      msg.arg[2] = ERR_UNKNOWN_METHOD;
    }

    msg.send_size = 0;
    reply(&msg);
  }

}
static int _spi_register_interface(void *data)
{
	spi_dev_t		*dev = data;
	SPIDEV			*drvhdl;
	resmgr_attr_t	rattr;
	char			devname[PATH_MAX + 1];
	struct	passwd*  pw;
	uid_t            uid = 0;
	gid_t            gid = 0;

	if ((drvhdl = dev->funcs->init(dev, dev->opts)) == NULL) {
		free(dev->opts);
		dev->opts = NULL;
		return (!EOK);
	}

	dev->drvhdl = drvhdl;

	/* set up i/o handler functions */
	memset(&rattr, 0, sizeof(rattr));
	rattr.nparts_max   = SPI_RESMGR_NPARTS_MIN;
	rattr.msg_max_size = SPI_RESMGR_MSGSIZE_MIN;

	iofunc_attr_init(&drvhdl->attr, S_IFCHR | devperm, NULL, NULL);
	drvhdl->attr.mount = &_spi_mount;

	/* register device name */
	snprintf(devname, PATH_MAX, "/dev/spi%d", dev->devnum);
	if (-1 == (dev->id = resmgr_attach(dev->dpp, &rattr, devname, _FTYPE_ANY, 0,
					&_spi_connect_funcs, &_spi_io_funcs, (void *)drvhdl))) {
		perror("resmgr_attach() failed");
		goto failed1;
	}

	resmgr_devino(dev->id, &drvhdl->attr.mount->dev, &drvhdl->attr.inode);

	if (UserParm != NULL) {
		if(*UserParm >= '0' && *UserParm <= '9') {
			uid = strtol(UserParm, &UserParm, 0);
			if(*UserParm++ == ':') {
				gid = strtol(UserParm, NULL, 0);
			}
		}
		else if (( pw = getpwnam( UserParm ) ) != NULL ) {
			uid = pw->pw_uid;
			gid = pw->pw_gid;
		}

		if(setgid(gid) == -1 ||	setuid(uid) == -1 ) {
			perror("setgid() / setuid() failed");
		}

	}

	if ((dev->ctp = dispatch_context_alloc(dev->dpp)) != NULL)
		return (EOK);

	perror("dispatch_context_alloc() failed");

	resmgr_detach(dev->dpp, dev->id, _RESMGR_DETACH_ALL);
failed1:
	dev->funcs->fini(drvhdl);

	return (!EOK);
}
Пример #7
0
main (int argc, char *argv[])
{
    int pathID;

    setvbuf (stdout, NULL, _IOLBF, 0);

    printf ("%s:  starting...\n", progname);

    options (argc, argv);

    /*
     * allocate and initialize a dispatch structure for use by our
     * main loop
    */
    
    dpp = dispatch_create ();
    if (dpp == NULL) {
        fprintf (stderr, "%s:  couldn't dispatch_create: %s\n",
                         progname, strerror (errno));
        exit (1);
    }
    
    /*
     * set up the resource manager attributes structure, we'll
     * use this as a way of passing information to resmgr_attach().
     * For now, we just use defaults.
    */
    
    memset (&rattr, 0, sizeof (rattr)); /* using the defaults for rattr */
    
    /*
     * intialize the connect functions and I/O functions tables to
     * their defaults by calling iofunc_func_init().
     * 
     * connect_funcs, and io_funcs variables are already declared.
     * 
    */
     iofunc_func_init (_RESMGR_CONNECT_NFUNCS, &connect_funcs,
                      _RESMGR_IO_NFUNCS, &io_funcs);
    
                      
    /* over-ride the connect_funcs handler for open with our io_open,
     * and over-ride the io_funcs handlers for read and write with our
     * io_read and io_write handlers
     */
    connect_funcs.open = io_open;
    io_funcs.read = io_read;
    io_funcs.write = io_write;

    
    /* initialize our device description structure
     */
     
    iofunc_attr_init (&ioattr, S_IFCHR | 0666, NULL, NULL);
    /*
     *  call resmgr_attach to register our prefix with the
     *  process manager, and also to let it know about our connect
     *  and I/O functions.
     *
     *  On error, returns -1 and errno is set.
    */
    pathID = resmgr_attach (dpp, &rattr, EXAMPLE_NAME, _FTYPE_ANY, 0,
                                     &connect_funcs, &io_funcs, &ioattr);
    if (pathID == -1) {
        fprintf (stderr, "%s:  couldn't attach pathname: %s\n",
                         progname, strerror (errno));
        exit (1);
    }

    ctp = dispatch_context_alloc (dpp);
    
    while (1) {
        if ((ctp = dispatch_block (ctp)) == NULL) {
            fprintf (stderr, "%s:  dispatch_block failed: %s\n",
                             progname, strerror (errno));
            exit (1);
        }
        dispatch_handler (ctp);
    }
}
Пример #8
0
int main(int argc, char *argv[]) {
	dispatch_t				*dpp;
	resmgr_context_t		*ctp;
	resmgr_attr_t			res_attr;
	resmgr_connect_funcs_t	connect_funcs1;
	resmgr_connect_funcs_t	connect_funcs2;
	resmgr_io_funcs_t		io_funcs1;
	resmgr_io_funcs_t		io_funcs2;
	struct slogdev			*trp;
	int				daemon_flag = 0;

	// Parse any options.
	options(argc, argv);

	//	
	// Buffer allocation.
	// The design puts all the data structures in one big contiguious
	// chunk of memory. Makes it easy to analyze if it was in a memory
	// mapped SRAM card which survives a crash or power failure.
	//
	trp = &SlogDev;
	trp->beg = malloc(NumInts*sizeof(int));
	trp->end = trp->beg + NumInts;
	if(trp->beg == NULL) {
		fprintf(stderr, "%s: Insufficient memory to allocate buffers.\n", __progname);
		exit(EXIT_FAILURE);
	}
	slogger_init(trp);

	// Create a dispatch context to receive messages.
	if((dpp = dispatch_create()) == NULL) {
		fprintf(stderr, "%s: Unable to allocate dispatch context\n", __progname);
		exit(EXIT_FAILURE);
	}

	// Init resmgr attributes
	memset(&res_attr, 0, sizeof res_attr);
	res_attr.nparts_max = 2;
	// All slogf/slogb calls should fit in the intial receive
	res_attr.msg_max_size = _SLOG_MAXSIZE + _SLOG_HDRINTS + sizeof(io_msg_t);

	// Init funcs for handling /dev/slog messages
	iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs1, _RESMGR_IO_NFUNCS, &io_funcs1);
	connect_funcs1.unlink = io_unlink;
	io_funcs1.read        = io_read;
	io_funcs1.write       = io_write;
	io_funcs1.unblock     = io_unblock;

	// Create /dev/slog in the pathname space. At this point we can get opens.
	iofunc_attr_init(&trp->attr, S_IFCHR | 0666, 0, 0);
	if((trp->id = resmgr_attach(dpp, &res_attr, "/dev/slog", _FTYPE_ANY, _RESMGR_FLAG_SELF,
		&connect_funcs1, &io_funcs1, &trp->attr)) == -1) {
		fprintf(stderr, "%s: Unable to allocate device %s (%s)\n", __progname, "/dev/slog", strerror(errno));
		exit(EXIT_FAILURE);
	}

	// Init funcs for handling /dev/slog messages
	iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs2, _RESMGR_IO_NFUNCS, &io_funcs2);
	io_funcs2.write       = io_console_write;

	// Create /dev/console in the pathname space. At this point we can get opens.
	if(resmgr_attach(dpp, &res_attr, "/dev/console", _FTYPE_ANY, 0,
		&connect_funcs2, &io_funcs2, &trp->attr) == -1) {
		fprintf(stderr, "%s: Unable to allocate device %s (%s)\n", __progname, "/dev/console", strerror(errno));
		exit(EXIT_FAILURE);
	}

	// If a logfile was specified then create a thread to write it
	if(LogFname) {
		sigset_t	signalset;

		if(pthread_create(0, NULL, &logger, NULL) != 0) {
			fprintf(stderr, "%s: Unable to create logger thread.\n", __progname);
			exit(EXIT_FAILURE);
		}

	// We want the logger thread to catch signals. Not us!
	sigemptyset(&signalset);
	sigfillset(&signalset);
	pthread_sigmask(SIG_BLOCK, &signalset, NULL);
	}

	// Run in backgound
	if(Verbose) {
		daemon_flag = PROCMGR_DAEMON_NODEVNULL;
	}
	if (procmgr_daemon(EXIT_SUCCESS, daemon_flag) == -1) {
		fprintf(stderr, "%s: Couldn't become daemon.\n", argv[0]);
	}

	// Slogger is single-threaded
	ctp = resmgr_context_alloc(dpp);

	for(;;) {
		if((ctp = resmgr_block(ctp)) == NULL)
			exit(EXIT_FAILURE);

		resmgr_handler(ctp);
	}

	exit(EXIT_SUCCESS);
}
Пример #9
0
int
resmgr_create_device(event_bus_line_t *line)
{
        devi_attr_t				*attr;
        resmgr_attr_t				res_attr;
	static resmgr_connect_funcs_t		connect_funcs;
	static resmgr_io_funcs_t		io_funcs;
        static int				once = 0;
        int					i;
	char					name[48];
        char					path[_POSIX_PATH_MAX];
	int					id;
        dev_t					d;

	if ((attr = malloc(sizeof(devi_attr_t))) == NULL) {
                errno_print("malloc");
                return (-1);
        }

	if (!once) {
                iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs,
                                 _RESMGR_IO_NFUNCS, &io_funcs);
                connect_funcs.open = devi_open;
                io_funcs.devctl = devi_devctl;
                io_funcs.read = devi_read;
                io_funcs.close_ocb = devi_close;
                io_funcs.unblock = devi_unblock;
                io_funcs.notify = devi_notify;
                once = 1;
        }

        iofunc_attr_init(&attr->attr, 0666 | S_IFCHR, NULL, NULL);
	attr->flags = line->type;
        attr->ocb_list = NULL;
        line->attr = attr;

	switch (attr->flags) {

        case DEVI_CLASS_KBD:
                strcpy(name, "keyboard");
                break;
                
        case DEVI_CLASS_REL:
                strcpy(name, "mouse");
                break;
                
        case DEVI_CLASS_ABS:
                strcpy(name, "touch");
                break;
        }

        attr->line = line;

        memset(&res_attr, 0, sizeof(res_attr));
        res_attr.msg_max_size = 2048;
        res_attr.nparts_max = 3;

        for (i = 0; i < 10; i++) {

                sprintf(path, DEVICE_NAME, name, i);
                if (access(path, F_OK) != 0)
                        break;
        }
        if (i == 10) {

                char * pMsgTxt = "Error: unable to create device %s\n";
                fprintf(stderr, pMsgTxt, path);
                slogf(_SLOG_SETCODE(_SLOGC_INPUT, 0), _SLOG_ERROR, pMsgTxt, path);
                free(attr);
                return (-1);
        }

        id = resmgr_attach(devi_get_dispatch_handle(), &res_attr, path, 
                           _FTYPE_ANY, 0, &connect_funcs, 
                           &io_funcs, (void *) &attr->attr);

        if (id < 0) {
                char * pMsgTxt = "Error: could not attach resource manager\n";
                fprintf(stderr, pMsgTxt);
                slogf(_SLOG_SETCODE(_SLOGC_INPUT, 0), _SLOG_ERROR, pMsgTxt);
                free(attr);
                return (-1);
        }

        attr->attr.rdev = rsrcdbmgr_devno_attach(name, -1, 0);
        resmgr_devino(id, &d, &attr->attr.inode);

        return (0);
}
Пример #10
0
main( int argc, char **argv)
{
  resmgr_attr_t resmgr_attr;
  dispatch_t *dpp;
  dispatch_context_t *ctp;
  int id;
  int pci_id;
  char *device;

  /* check to see if device number is specified when invoked */
  if( argc == 1 ){
    fprintf(stderr, "%s: Invoke with device number (i.e. 1 = /dev/ics660-1)\n",argv[0]);
    return EXIT_FAILURE;
  }
  
  device = calloc((size_t) 64, sizeof(char));
  strcat(device,"/dev/ics660-");
  strcat(device,(const char *) argv[1]);
  printf("_ICS660_DRV: INITIALIZING DEVICE %s\n",device);
  
  sscanf(argv[1],"%d",&pci_id);
  printf("_ICS660_DRV: DEVICE ID %d\n",pci_id);

  ics660 = (struct ics660b *)ics660_drv_init((int) pci_id);

  /* initialize dispatch interface */
  if((dpp = dispatch_create()) == NULL){
    fprintf(stderr, "%s: Unable to allocate dispatch handle.\n", argv[0]);
    return EXIT_FAILURE;
  }
  /* initialize resource manager attributes */
  memset(&resmgr_attr, 0, sizeof resmgr_attr);
  resmgr_attr.nparts_max = 1;
  resmgr_attr.msg_max_size = 2048;

  /* initialize functions for handling messages */
  iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs, 
		   _RESMGR_IO_NFUNCS, &io_funcs);
  io_funcs.read = io_read;
  io_funcs.write = io_write;
  io_funcs.devctl = io_devctl;

 /* initialize attribute structure used by the device */
  iofunc_attr_init(&attr, S_IFNAM | 0666, 0, 0);
  //attr.nbytes = strlen(buffer)+1;

  /* attach our device name */
  id = resmgr_attach(dpp,            // dispatch handle
		     &resmgr_attr,   // resource manager attrs
		     device,  // device name
		     _FTYPE_ANY,     // open type
		     0,              // flags
		     &connect_funcs, // connect routines
		     &io_funcs,      // I/O routines
		     &attr);         // handle
  if(id == -1){
    fprintf(stderr, "%s: Unable to attach name.\n",argv[0]);
    return EXIT_FAILURE;
  }
  /* allocate a context structure */
  ctp = dispatch_context_alloc(dpp);

  /* start the resource manager messager loop */
  while(1){
    if((ctp = dispatch_block(ctp)) == NULL) {
      fprintf(stderr,"block_error\n");
      return EXIT_FAILURE;
    }
    dispatch_handler(ctp);
  }
}
int main(int argc, char **argv)
{
    /* declare variables we'll be using */
    thread_pool_attr_t   pool_attr;
    resmgr_attr_t        resmgr_attr;
    dispatch_t           *dpp;
    thread_pool_t        *tpp;
    dispatch_context_t   *ctp;
    int                  id;

    /* initialize dispatch interface */
    if((dpp = dispatch_create()) == NULL) {
        fprintf(stderr,
                "%s: Unable to allocate dispatch handle.\n",
                argv[0]);
        return EXIT_FAILURE;
    }

    /* initialize resource manager attributes */
    memset(&resmgr_attr, 0, sizeof resmgr_attr);
    resmgr_attr.nparts_max = 1;
    resmgr_attr.msg_max_size = 2048;

    /* initialize functions for handling messages */
    iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs, 
                     _RESMGR_IO_NFUNCS, &io_funcs);
    io_funcs.read = io_read;
    io_funcs.write = io_write;
    io_funcs.devctl = io_devctl;

    /* initialize attribute structure used by the device */
    iofunc_attr_init(&attr, S_IFNAM | 0666, 0, 0);
    attr.nbytes = strlen (buffer)+1;

    /* attach our device name */
    id = resmgr_attach(
            dpp,            /* dispatch handle        */
            &resmgr_attr,   /* resource manager attrs */
            "/dev/sample",  /* device name            */
            _FTYPE_ANY,     /* open type              */
            0,              /* flags                  */
            &connect_funcs, /* connect routines       */
            &io_funcs,      /* I/O routines           */
            &attr);         /* handle                 */
    if(id == -1) {
        fprintf(stderr, "%s: Unable to attach name.\n", argv[0]);
        return EXIT_FAILURE;
    }

    /* initialize thread pool attributes */
    memset(&pool_attr, 0, sizeof pool_attr);
    pool_attr.handle = dpp;
    pool_attr.context_alloc = dispatch_context_alloc;
    pool_attr.block_func = dispatch_block;
    pool_attr.unblock_func = dispatch_unblock;
    pool_attr.handler_func = dispatch_handler;
    pool_attr.context_free = dispatch_context_free;
    pool_attr.lo_water = 4;
    pool_attr.hi_water = 8;
    pool_attr.increment = 2;
    pool_attr.maximum = 50;

    /* allocate a thread pool handle */
    if((tpp = thread_pool_create(&pool_attr, 
                                 POOL_FLAG_EXIT_SELF)) == NULL) {
        fprintf(stderr, "%s: Unable to initialize thread pool.\n",
                argv[0]);
        return EXIT_FAILURE;
    }

    /* start the threads, will not return */
    thread_pool_start(tpp);
    
    return EXIT_SUCCESS;
}
Пример #12
0
int main(int argc, char *argv[])
{
	int 				c;
	char				*size_suffix;
	pid_t 				process = 0;
	dispatch_t			*dpp;
	resmgr_attr_t		resmgr_attr;
	resmgr_context_t	*ctp;

	umask(0222);

	/* We want some pshysical memory reserved for dumper process. */
	init_memory();

	/* We also want some physical memory for our stack.  */
	init_stack();
	
	while ( (c = getopt( argc, argv, "Dd:p:ns:vmPwtz:" )) != -1 ) {
		switch(c) {
		case 'd':
			dump_dir = optarg;
			break;
		case 'n':
			sequential_dumps = 1;
			break;
		case 'p':
			process = atoi(optarg);
			break;
		case 's':
			max_core_size = strtol(optarg, &size_suffix, 10);
			switch(*size_suffix){
				case 'g':
				case 'G':
					max_core_size *= 1024;
				case 'm':
				case 'M':
					max_core_size *= 1024;
				case 'k':
				case 'K':
					max_core_size *= 1024;
					break;
				case 0:
					break;
				default:
					fprintf(stderr, "Warning: suffix '%c' unrecognized\n", *size_suffix);
			}
			break;
		case 'v':
			verbose = 1;
			break;
		case 'm':
			nodumpmem = 1;
			break;
		case 'P':
			nodumpphys = 0;
			break;
		case 't':
			cur_tid_only = 1;
			break;
		case 'w':
			world_readable = 1;
			break;
	    case 'z':
			gzlevel = strtol(optarg, &size_suffix, 10);
			if (gzlevel < 1 || gzlevel > 9) {
				fprintf(stderr, "Compress level must between 1 and 9.\n");
				exit(EXIT_FAILURE);
			}
			break;
		}
	}

	if ( process ) {
		requested = 1;
		return dump( ND_LOCAL_NODE, process, max_core_size );
	}

	if((dpp = dispatch_create()) == NULL) {
		fprintf(stderr, "%s: Unable to allocate dispatch handle.\n",argv[0]);
		exit(EXIT_FAILURE);
	}

	memset(&resmgr_attr, 0, sizeof resmgr_attr);
	resmgr_attr.nparts_max = 1;
	resmgr_attr.msg_max_size = 2048;

	iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect, _RESMGR_IO_NFUNCS, &io);
	io.write = dumper_write;
	io.devctl = dumper_devctl; 
	io.close_ocb = dumper_close_ocb; 

	iofunc_attr_init(&attr, S_IFNAM | 0600, 0, 0);

	if ( -1 == resmgr_attach(dpp, &resmgr_attr, "/proc/dumper", _FTYPE_DUMPER, 0, &connect, &io, &attr)) {
		fprintf( stderr, "%s: Couldn't attach as /proc/dumper: %s\n", argv[0], strerror(errno) );
		exit(EXIT_FAILURE);
	}

	if((ctp = resmgr_context_alloc(dpp)) == NULL) {
		fprintf(stderr, "%s: Unable to allocate dispatch handle.\n",argv[0]);
		exit(EXIT_FAILURE);
	}

	if ( -1 == procmgr_daemon(EXIT_SUCCESS, verbose ? 
		PROCMGR_DAEMON_NODEVNULL | PROCMGR_DAEMON_KEEPUMASK :
		PROCMGR_DAEMON_KEEPUMASK) ) {
		fprintf(stderr, "%s: Couldn't become daemon.\n",argv[0]);
		exit(EXIT_FAILURE);
	}

	while(1) {
		if((ctp = resmgr_block(ctp)) == NULL) {
			fprintf(stderr, "block error\n");
			exit(EXIT_FAILURE);
		}
		resmgr_handler(ctp);
	}
	return EXIT_SUCCESS;
}
Пример #13
0
name_attach_t *name_attach(dispatch_t *dpp, const char *path, unsigned flags) {
	name_attach_t *attach;
	int auto_create, mntid, chid;
	char *newname;

	//Reserve leading /'s for future use
	if (!path || !*path || *path == '/') {
		errno = EINVAL;
		return NULL;
	}

	//Don't allow ../ or .. attaches to escape
	if ((newname = (char *)strstr(path, "..")) && 
	    (newname[2] == '/' || newname[2] == '\0')) {
		errno = EINVAL;
		return NULL;
	}
	newname = NULL;

	mntid = chid = -1;
	auto_create = (dpp) ? 0 : 1;

	if (!dpp && !(dpp = dispatch_create())) {
		errno = EINVAL;
		return NULL;
	}

	if (flags & NAME_FLAG_ATTACH_GLOBAL) {
		if (!(newname = alloca(strlen(__name_prefix_global) + strlen(path) + 1))) {
			errno = ENOMEM;
			goto failure;
		}
		strcpy(newname, __name_prefix_global);
	}
	else {
		if (!(newname = alloca(strlen(__name_prefix_local) + strlen(path) + 1))) {
			errno = ENOMEM;
			goto failure;
		}
		strcpy(newname, __name_prefix_local);
	}
	strcat(newname, path);

	//TODO: Make the last thing really an attribute matching us?
	//TODO: Allow the user to specify a function they want to be called
    
    /* We have to regist with global service manager, this is done by:
	 *  1) do an empty resmgr_attach() to setup things (in case dpp->chid isn't there)
	 *  2) send an _IO_CONNECT_OPEN, _IO_CONNECT_EXTRA_RESMGR_LINK, to the
	 *     manager. (What we really want is _IO_CONENCT_LINK plus
	 *    _IO_CONNECT_EXTRA_RESMGR_LINK, but PathMgr won't pass it down to GNS
	 *  3) if 2) faild, there is no local GNS exist, then ONLY IF the attach
	 *     if for _NAME_FLAG_ATTACH_LOCAL, we will send a _IO_CONNECT_LINK, 
	 *     _IO_CONNECT_EXTRA_RESMGR_LINK, as if pathmgr_link() did, this will 
	 *     regist it with pathmgr. And everything still works in local nodes.
	 */
	{
		unsigned nflag;
		struct link *linkl;
		struct _io_resmgr_link_extra extra;
			
		/* make sure no real attach will happen */
		nflag = flags & ~_RESMGR_FLAG_FTYPEONLY;
		if (resmgr_attach(dpp, NULL, NULL, 0, nflag, NULL, NULL, NULL) == -1) {
			goto failure;
		}
			
		if (!(linkl = _resmgr_link_alloc())) {			
			goto failure;
		}
		linkl->connect_funcs = 0;
		linkl->io_funcs = 0;
		linkl->handle = 0;
		
		extra.nd = ND_LOCAL_NODE;
		extra.pid = getpid();
		extra.chid = dpp->chid;
		extra.handle = mntid = linkl->id;
		extra.file_type = _FTYPE_NAME;
		extra.flags = flags & _RESMGR_FLAG_MASK;
		linkl->link_id = _connect(PATHMGR_COID, newname, 0, 0,
								 SH_DENYNO, _IO_CONNECT_OPEN, 0, 0, _FTYPE_NAME,
								 _IO_CONNECT_EXTRA_RESMGR_LINK, sizeof extra, &extra,
								 0, 0, 0);
		
		if (linkl->link_id == -1 && !(flags & NAME_FLAG_ATTACH_GLOBAL) &&
			(errno == ENOTSUP || errno == ENOENT || errno == ENOSYS))
		{
			linkl->link_id = _connect(PATHMGR_COID, newname, 0, 0,
									 SH_DENYNO, _IO_CONNECT_LINK, 0, 0, _FTYPE_NAME,
									 _IO_CONNECT_EXTRA_RESMGR_LINK, sizeof extra, &extra,
									 0, 0, 0);
		}

		if (linkl->link_id == -1) {
			_resmgr_link_free(linkl->id, _RESMGR_DETACH_ALL);
			mntid = -1;
			goto failure;
		}
		linkl->flags &= ~_RESMGR_LINK_HALFOPEN;
	}
	
	chid = _DPP(dpp)->chid;
	
	if (!(attach = malloc(sizeof(*attach)))) {
		errno = ENOMEM;
		goto failure;
	}	
	attach->dpp = dpp;
	attach->chid = chid;
	attach->mntid = mntid;
	
	return attach;
	
failure:
	if (mntid > 0) {
		(void)resmgr_detach(dpp, mntid, 0);
	}
	if (auto_create && dpp) {
		(void)dispatch_destroy(dpp);
	}
	return NULL;
}
Пример #14
0
int main(int argc, char *argv[]) {
	dispatch_t *dpp;
	resmgr_attr_t resmgr_attr;
	dispatch_context_t *ctp;
	resmgr_connect_funcs_t connect_funcs;
	resmgr_io_funcs_t io_funcs;
	iofunc_attr_t io_attr;

	printf("Start resource manager\n");

	/* Init mutex for counter variable */
	if (pthread_mutex_init(&lock, NULL) != 0){
	        printf("\n mutex init failed\n");
	        return 1;
	}

	/* Create dispatch. */
	if (!(dpp = dispatch_create()))
		error("dispatch_create()");

	/* Initialize resource manager attributes. */
	memset(&resmgr_attr, 0, sizeof(resmgr_attr));
	resmgr_attr.nparts_max = 1;
	resmgr_attr.msg_max_size = 2048;

	/* Set standard connect and io functions. */
	iofunc_func_init(_RESMGR_CONNECT_NFUNCS,
				&connect_funcs,
				_RESMGR_IO_NFUNCS,
				&io_funcs);
	iofunc_attr_init(&io_attr, S_IFNAM | 0666, 0, 0);

	/* Override functions */
	io_funcs.read = io_read;
	io_funcs.write = io_write;

	/* Establish resource manager */
	if (resmgr_attach(dpp,
				&resmgr_attr,
				"/dev/myresource",
				_FTYPE_ANY,
				0,
				&connect_funcs,
				&io_funcs,
				&io_attr)
					< 0)
		error("resmgr_attach()");

	/* Create counter thread */
	if (pthread_create(&tid, NULL, &counter, NULL) != 0)
		printf("can't create counter thread\n");

	/* Wait forever, handling messages. */
	ctp = dispatch_context_alloc(dpp);
	while(1){
		if (!(ctp = dispatch_block(ctp)))
			error("dispatch_block()");
		dispatch_handler(ctp);
	}
	pthread_mutex_destroy(&lock);

	exit(EXIT_SUCCESS);
}
Пример #15
0
//int init_bttvx(int video_mode, int device_number, int w, int h) //Video format, device id, width and height
int 
main (int argc, char * argv[])
{
	int i; 
	/* declare variables we'll be using */
	resmgr_attr_t		resmgr_attr;
	dispatch_t			*dpp;
	dispatch_context_t  *ctp;
	int                 id;
	int video_format = 0;
	int device_id = 0;
	char device_name[100];
	char shell_command[100];
	char buffer[20];
	char buffer2[20];
	int bus, dev_func;


	//Check the arguments
	
	if ( argc == 1 || argc > 3)
	{
		printf("Use: ./bttvx video_format [device_id]\n");
		printf("0 ----> PAL\n");
		printf("1 ----> NTSC\n"); 
		printf("2 ----> S-Video\n");
		exit(0);
	}
	

	//Get video format
	video_format = atoi(argv[1]);

	//Check driver index
	if (argv[2] != NULL)
	{
		device_id = atoi(argv[2]);
		if (device_id < 0 || device_id > BTTV_MAX)
		{
			printf("bttvx: Sorry!, the device id overcomes the maximum number of devices allowed\n");
			exit(0);
		}
	}
	

	//Filling passed width and height
	
	width = W;
	height= H;
	
	image_buffer = (unsigned char *) malloc (width*height*deep);
		
	strcpy(device_name,"/dev/bttvx");
	strcat(device_name,itoa(device_id,buffer,10)); //Complete the name

	init_bttvx(video_format,device_id, width,height, 0, 0);
	
	/* initialize dispatch interface */
	
	if((dpp = dispatch_create()) == NULL) 
	{
		fprintf(stderr, 
			    "%s: Unable to allocate dispatch handle.\n",
				argv[0]);
		return EXIT_FAILURE;
	}
	

	/* initialize resource manager attributes */
	memset(&resmgr_attr, 0, sizeof resmgr_attr);
	
	resmgr_attr.nparts_max = 1;
	resmgr_attr.msg_max_size=VBIBUF_SIZE;

	/* initialize functions for handling messages */
	
	iofunc_func_init(_RESMGR_CONNECT_NFUNCS, 
					 &connect_funcs,
					 _RESMGR_IO_NFUNCS, 
					 &io_funcs);

	io_funcs.read = io_read;
	//io_funcs.devctl = io_devctl;
	connect_funcs.open = io_open;
	
	/* initialize attribute structure used by the device */
	iofunc_attr_init(&attr, 
					 S_IFNAM | 0666, 
					 0, 0);
	//attr.nbytes = VBIBUF_SIZE + 1;
	attr.nbytes = VBIBUF_SIZE;

    /* attach our device name */
    id = resmgr_attach(dpp,            /* dispatch handle        */
                       &resmgr_attr,   /* resource manager attrs */
                       device_name,  /* device name            */
                       _FTYPE_ANY,     /* open type              */
                       0,              /* flags                  */
                       &connect_funcs, /* connect routines       */
                       &io_funcs,      /* I/O routines           */
                       &attr);         /* handle                 */
    if(id == -1) {
        fprintf(stderr, "%s: Unable to attach name.\n", argv[0]);
        return EXIT_FAILURE;
    }

	/* allocate a context structure */
	ctp = dispatch_context_alloc(dpp);

	/* start the resource manager message loop */
	
	while(1) 
	{
		if((ctp = dispatch_block(ctp)) == NULL) 
		{
			fprintf(stderr, "block error\n");
			return EXIT_FAILURE;
		}
		dispatch_handler(ctp);
	}
}