示例#1
0
extern "C" struct irs_sv *
irs_lcl_sv(struct irs_acc */*acc*/)
{
	struct service_private *service;
	struct irs_sv *sv;

	if ((sv = (irs_sv *)memget(sizeof *sv)) == NULL) {
		errno = ENOMEM;
		return NULL;
	}
	if ((service = (service_private *)memget(sizeof *service)) == NULL) {
		memput(sv, sizeof *sv);
		errno = ENOMEM;
		return NULL;
	}

	memset(service, 0, sizeof *service);

	sv->private_data = service;
	sv->close = sv_close;
	sv->next = sv_next;
	sv->byname = sv_byname;
	sv->byport = sv_byport;
	sv->rewind = sv_rewind;
	sv->minimize = sv_minimize;
	sv->res_get = NULL;
	sv->res_set = NULL;

	return sv;
}
示例#2
0
static PyObject*
immom_saImmOmCcbObjectModify(PyObject *self, PyObject *args)
{
    SaAisErrorT rc;
    PyObject* attrList;
    unsigned int len, i;
    char* dn;
    SaNameT objectName;
    SaImmAttrModificationT_2** attrValues;

    if (!haveCcb) 
        return immom_aisException(SA_AIS_ERR_BAD_OPERATION);
    if (!PyArg_ParseTuple(args, "sO", &dn, &attrList)) 
        return NULL;
    if (immom_saName(dn, &objectName) == NULL)
        return NULL;
    if (!PyList_Check(attrList))
        return immom_aisException(SA_AIS_ERR_INVALID_PARAM);

    len = PyList_Size(attrList);
    attrValues = (SaImmAttrModificationT_2**)memget(
        sizeof(SaImmAttrModificationT_2*) * (len+1));
    attrValues[len] = NULL;

    for (i = 0; i < len; i++) {
        PyObject* item = PyList_GetItem(attrList, i);
        char* attrName;
        char* attrType;
        PyObject* valueList;
        SaImmAttrModificationT_2* mv;

        if (!PyTuple_Check(item))
            return immom_aisException(SA_AIS_ERR_INVALID_PARAM);
        if (!PyArg_ParseTuple(item, "ssO", &attrName, &attrType, &valueList)) 
            return immom_return_null();
        if (!PyList_Check(valueList))
            return immom_aisException(SA_AIS_ERR_INVALID_PARAM);

        mv = (SaImmAttrModificationT_2*)memget(
            sizeof(SaImmAttrModificationT_2));
        if (immom_parseAttrValue(
                &(mv->modAttr), attrName, attrType, valueList) == NULL)
            return immom_return_null();
        mv->modType = SA_IMM_ATTR_VALUES_REPLACE;

        attrValues[i] = mv;
    }

    rc = saImmOmCcbObjectModify_2(
        ccbHandle, &objectName, (const SaImmAttrModificationT_2**)attrValues);
    if (rc != SA_AIS_OK) {
        return immom_aisException(rc);
    }
    return immom_return_None();
}
示例#3
0
static PyObject*
immom_saImmOmCcbObjectCreate(PyObject *self, PyObject *args)
{
    SaAisErrorT rc;
    char* className;
    char* parentStr;
    PyObject* attrList;
    SaNameT parentName;
    unsigned int len, i;
    SaImmAttrValuesT_2** attrValues;

    if (!haveCcb) 
        return immom_aisException(SA_AIS_ERR_BAD_OPERATION);
    if (!PyArg_ParseTuple(args, "ssO", &parentStr, &className, &attrList)) 
        return NULL;
    if (immom_saName(parentStr, &parentName) == NULL)
        return NULL;
    if (!PyList_Check(attrList))
        return immom_aisException(SA_AIS_ERR_INVALID_PARAM);

    len = PyList_Size(attrList);
    attrValues = (SaImmAttrValuesT_2**)memget(
        sizeof(SaImmAttrValuesT_2*) * (len+1));
    attrValues[len] = NULL;

    for (i = 0; i < len; i++) {
        PyObject* item = PyList_GetItem(attrList, i);
        char* attrName;
        char* attrType;
        PyObject* valueList;
        SaImmAttrValuesT_2* av;

        if (!PyTuple_Check(item))
            return immom_aisException(SA_AIS_ERR_INVALID_PARAM);
        if (!PyArg_ParseTuple(item, "ssO", &attrName, &attrType, &valueList)) 
            return immom_return_null();
        if (!PyList_Check(valueList))
            return immom_aisException(SA_AIS_ERR_INVALID_PARAM);

        av = (SaImmAttrValuesT_2*)memget(sizeof(SaImmAttrValuesT_2));

        if (immom_parseAttrValue(av, attrName, attrType, valueList) == NULL)
            return immom_return_null();

        attrValues[i] = av;
    }

    rc = saImmOmCcbObjectCreate_2(
        ccbHandle, className, &parentName, 
        (const SaImmAttrValuesT_2**)attrValues);
    if (rc != SA_AIS_OK) return immom_aisException(rc);
    return immom_return_None();
}
struct net_data *
net_data_create(const char *conf_file) {
	struct net_data *net_data;

	net_data = memget(sizeof (struct net_data));
	if (net_data == NULL)
		return (NULL);
	memset(net_data, 0, sizeof (struct net_data));

	if ((net_data->irs = irs_gen_acc("", conf_file)) == NULL) {
		memput(net_data, sizeof (struct net_data));
		return (NULL);
	}
#ifndef DO_PTHREADS
	(*net_data->irs->res_set)(net_data->irs, &_res, NULL);
#endif

	net_data->res = (*net_data->irs->res_get)(net_data->irs);
	if (net_data->res == NULL) {
		(*net_data->irs->close)(net_data->irs);
		memput(net_data, sizeof (struct net_data));
		return (NULL);
	}

	if ((net_data->res->options & RES_INIT) == 0U &&
	    res_ninit(net_data->res) == -1) {
		(*net_data->irs->close)(net_data->irs);
		memput(net_data, sizeof (struct net_data));
		return (NULL);
	}

	return (net_data);
}
示例#5
0
/* @ingroup mailbox
 *
 * Allocate a mailbox that allows up to the specified number of outstanding
 * messages.
 *
 * @param count
 *      Maximum number of messages allowed for the mailbox.
 *
 * @return
 *      The index of the newly allocated mailbox, or ::SYSERR if all mailboxes
 *      are already in use or other resources could not be allocated.
 */
syscall mailboxAlloc(uint count)
{
    static uint nextmbx = 0;
    uint i;
    struct mbox *mbxptr;
    int retval = SYSERR;

    /* wait until other threads are done editing the mailbox table */
    wait(mboxtabsem);

    /* run through all mailboxes until we find a free one */
    for (i = 0; i < NMAILBOX; i++)
    {
        nextmbx = (nextmbx + 1) % NMAILBOX;
        mbxptr = &mboxtab[nextmbx];

        /* when we find a free mailbox set that one up and return it */
        if (MAILBOX_FREE == mbxptr->state)
        {
            /* get memory space for the message queue */
            mbxptr->msgs = memget(sizeof(int) * count);

            /* check if memory was allocated correctly */
            if (SYSERR == (int)mbxptr->msgs)
            {
                pi_printf("error: fail to mailbox memory\r\n");
                break;
            }

            /* initialize mailbox details and semaphores */
            mbxptr->count = 0;
            mbxptr->start = 0;
            mbxptr->max = count;
            usb_sem_new(&mbxptr->sender, 1024);
            usb_sem_new(&mbxptr->receiver, 0);
            if ((SYSERR == (int)mbxptr->sender) ||
                    (SYSERR == (int)mbxptr->receiver))
            {
                memfree(mbxptr->msgs, sizeof(int) * (mbxptr->max));
                semfree(mbxptr->sender);
                semfree(mbxptr->receiver);
                pi_printf("error: fail to allocate mailbox\r\n");
                break;
            }

            /* mark this mailbox as being used */
            mbxptr->state = MAILBOX_ALLOC;

            /* return value is index of the allocated mailbox */
            retval = nextmbx;
            break;
        }
    }

    /* signal this thread is done editing the mbox tab */
    semsignal(mboxtabsem);

    /* return either SYSERR or the index of the allocated mailbox */
    return retval;
}
示例#6
0
log_channel
log_new_file_channel(unsigned int flags, int level,
		     const char *name, FILE *stream, unsigned int versions,
		     unsigned long max_size) {
	log_channel chan;

	chan = memget(sizeof (struct log_channel));
	if (chan == NULL) {
		errno = ENOMEM;
		return (NULL);
	}
	chan->type = log_file;
	chan->flags = flags;
	chan->level = level;
	if (name != NULL) {
		size_t len;

		len = strlen(name);
		/*
		 * Quantize length to a multiple of 256.  There's space for the
		 * NUL, since if len is a multiple of 256, the size chosen will
		 * be the next multiple.
		 */
		chan->out.file.name_size = ((len / 256) + 1) * 256;
		chan->out.file.name = memget(chan->out.file.name_size);
		if (chan->out.file.name == NULL) {
			memput(chan, sizeof (struct log_channel));
			errno = ENOMEM;
			return (NULL);
		}
		/* This is safe. */
		strcpy(chan->out.file.name, name);
	} else {
		chan->out.file.name_size = 0;
		chan->out.file.name = NULL;
	}
	chan->out.file.stream = stream;
	chan->out.file.versions = versions;
	chan->out.file.max_size = max_size;
	chan->out.file.owner = getuid();
	chan->out.file.group = getgid();
	chan->references = 0;
	return (chan);
}
future* future_alloc(int future_flags){ 

    if(future_flags == FUTURE_EXCLUSIVE){
        future *f = (future*)memget(sizeof(future));
        f->state = FUTURE_EMPTY;
        f->value = 0;
        f->flag = FUTURE_EXCLUSIVE;
        f->tid = -1;
        f->get_queue.head = NULL;
        f->get_queue.tail = NULL;
        f->set_queue.head = NULL;
        f->set_queue.tail = NULL;
        return f;
    }
    if(future_flags == FUTURE_SHARED){
        future *f = (future*)memget(sizeof(future));
        f->state = FUTURE_EMPTY;
        f->value = 0;
        f->flag = FUTURE_SHARED;
        f->tid = -1;
        f->get_queue.head = NULL;
        f->get_queue.tail = NULL;
        f->set_queue.head = NULL;
        f->set_queue.tail = NULL;
        return f;
    }
    if(future_flags == FUTURE_QUEUE){
        future *f = (future*)memget(sizeof(future));
        f->state = FUTURE_EMPTY;
        f->value = 0;
        f->flag = FUTURE_QUEUE;
        f->tid = -1;
        f->get_queue.head = NULL;
        f->get_queue.tail = NULL;
        f->set_queue.head = NULL;
        f->set_queue.tail = NULL;
        return f;
    }
    return NULL;
}
示例#8
0
int
ctl_bufget(struct ctl_buf *buf, ctl_logfunc logger) {
	static const char me[] = "ctl_bufget";

	REQUIRE(!allocated_p(*buf) && buf->used == 0U);
	buf->text = memget(MAX_LINELEN);
	if (!allocated_p(*buf)) {
		(*logger)(ctl_error, "%s: getmem: %s", me, strerror(errno));
		return (-1);
	}
	buf->used = 0;
	return (0);
}
示例#9
0
文件: db_tsig.c 项目: aosm/bind
struct tsig_record *
new_tsig(DST_KEY *key, u_char *sig, int siglen) {
	struct tsig_record *tsig;

	if (siglen > TSIG_SIG_SIZE)
		return(NULL);
	tsig = memget(sizeof(struct tsig_record));
	if (tsig == NULL)
		return(NULL);
	tsig->key = key;
	tsig->siglen = siglen;
	memcpy(tsig->sig, sig, siglen);
	return(tsig);
}
示例#10
0
log_channel
log_new_null_channel() {
	log_channel chan;

	chan = memget(sizeof (struct log_channel));
	if (chan == NULL) {
		errno = ENOMEM;
		return (NULL);
	}
	chan->type = log_null;
	chan->flags = LOG_CHANNEL_OFF;
	chan->level = log_info;
	chan->references = 0;
	return (chan);
}
示例#11
0
int
log_new_context(int num_categories, char **category_names, log_context *lc) {
	log_context nlc;

	nlc = memget(sizeof (struct log_context));
	if (nlc == NULL) {
		errno = ENOMEM;
		return (-1);
	}
	nlc->num_categories = num_categories;
	nlc->category_names = category_names;
	nlc->categories = memget(num_categories * sizeof (log_channel_list));
	if (nlc->categories == NULL) {
		memput(nlc, sizeof (struct log_context));
		errno = ENOMEM;
		return (-1);
	}
	memset(nlc->categories, '\0',
	       num_categories * sizeof (log_channel_list));
	nlc->flags = 0U;
	nlc->level = 0;
	*lc = nlc;
	return (0);
}
示例#12
0
log_channel
log_new_syslog_channel(unsigned int flags, int level, int facility) {
	log_channel chan;

	chan = memget(sizeof (struct log_channel));
	if (chan == NULL) {
		errno = ENOMEM;
		return (NULL);
	}
	chan->type = log_syslog;
	chan->flags = flags;
	chan->level = level;
	chan->out.facility = facility;
	chan->references = 0;
	return (chan);
}
示例#13
0
/**
 * @ingroup usbcore
 *
 * Dynamically allocates a struct usb_xfer_request, including a data buffer.
 *
 * @param bufsize
 *      Length of the data buffer for sending and/or receiving.
 *
 * @return
 *      A pointer to the resulting struct usb_xfer_request, or NULL if out of
 *      memory.
 */
struct usb_xfer_request *
usb_alloc_xfer_request(uint bufsize)
{
    struct usb_xfer_request *req;

    req = memget(sizeof(struct usb_xfer_request) + bufsize);
    if (req == (void*)SYSERR)
    {
    	pi_printf("error: fail to usb alloc xfer request\r\n");
        return NULL;
    }
    usb_init_xfer_request(req);
    req->sendbuf = (uint8_t*)(req + 1);
    req->size = bufsize;
    return req;
}
示例#14
0
int
log_add_channel(log_context lc, int category, log_channel chan) {
	log_channel_list lcl;

	if (lc == NULL || category < 0 || category >= lc->num_categories) {
		errno = EINVAL;
		return (-1);
	}

	lcl = memget(sizeof (struct log_channel_list));
	if (lcl == NULL) {
		errno = ENOMEM;
		return(-1);
	}
	lcl->channel = chan;
	lcl->next = lc->categories[category];
	lc->categories[category] = lcl;
	chan->references++;
	return (0);
}
示例#15
0
文件: nvram.c 项目: JamesLinus/xinu
/**
 * Change the variable name to value. If name does not exist, create it,
 * otherwise replace the old value with the new.
 * @param *name  name to modify
 * @param *value value to set name
 * @return OK on successful change, SYSERR on failure
 */
devcall nvramSet(char *name, char *value)
{
    struct nvram_tuple *tuple;
    ulong length, offset;

    if (OK != nvramInit())
    {
        return SYSERR;
    }

    length = sizeof(struct nvram_tuple) + strnlen(name, NVRAM_STRMAX)
        + 1 + strnlen(value, NVRAM_STRMAX);

    /* check if name is already defined somewhere */
    /* if so unset, so we can reset */
    if (nvramGet(name) != NULL)
    {
        nvramUnset(name);
    }

    /* get enough space for both to be rounded up with \0 stored */
    if (NULL == (tuple = (struct nvram_tuple *)memget(length)))
    {
        return SYSERR;
    }

    /* copy name into memory */
    offset = 0;
    strncpy(tuple->pair, name, strnlen(name, NVRAM_STRMAX));
    offset += strnlen(name, NVRAM_STRMAX);
    strncpy(tuple->pair + offset, "=", 1);
    offset += 1;
    strncpy(tuple->pair + offset, value, strnlen(value, NVRAM_STRMAX));
    offset += strnlen(value, NVRAM_STRMAX);
    strncpy(tuple->pair + offset, "\0", 1);

    /* store pointer to name in nvram_tuple struct */
    nvramInsert(tuple);

    return OK;
}
示例#16
0
文件: future_alloc.c 项目: osa1/osdev
future *future_alloc(int future_flags)
{
    future *f = (future*) memget(sizeof(future));
    if (f == (void*)SYSERR)
    {
        return NULL;
    }
    f->value = NULL;
    f->flag = future_flags;
    f->state = FUTURE_EMPTY;
    f->tid = -1;

    /* initially we're allowing threads to read and write `state` field */
    f->s = semcreate(1);

    /* initially queues are empty */
    f->set_queue = queinit();
    f->get_queue = queinit();

    return f;
}
示例#17
0
/**
 * Initialize free memory into page aligned regions.  Takes necessary
 * overhead of region table for the amount of memory on platform.
 * @param memory_start Base address of heap space.
 */
void memRegionInit(void *memory_start, uint memory_size)
{
    int i;
    int max_regions;
    uint table_size;

    /* Initialize free memory list */
    memory_size = truncpage((uint)memory_start + memory_size)
        - (uint)memory_start;
    memory_start = (void *)roundpage(memory_start);

    /* find how many regions we need */
    max_regions = (memory_size / PAGE_SIZE);
    table_size = max_regions * sizeof(struct memregion);

    /* get memory for the region table */
    regtab = (struct memregion *)memget(table_size);

    /* configure region table */
    regtab[0].prev = (struct memregion *)SYSERR;
    regtab[0].next = (struct memregion *)SYSERR;
    regtab[0].start = memory_start;
    regtab[0].length = memory_size;
    regtab[0].thread_id = 0;

    /* clear all other regions */
    for (i = 1; i < max_regions; i++)
    {
        memRegionClear(&(regtab[i]));
    }

    regalloclist = (struct memregion *)SYSERR;
    regfreelist = &(regtab[0]);

#ifdef DETAIL
    kprintf("Allocated %d bytes to user memory heap.\r\n", memory_size);
#endif
}
示例#18
0
/**
 * @ingroup shell
 *
 * Shell command fstest.
 * @param nargs  number of arguments in args array
 * @param args   array of arguments
 * @return OK for success, SYSERR for syntax error
 */
shellcmd xsh_fstest(int nargs, char *args[])
{
    int rval;
    int fd, i, j;
    char *buf1, *buf2;
    
    
    /* Output help, if '--help' argument was supplied */
    if (nargs == 2 && strncmp(args[1], "--help", 7) == 0)
    {
        printf("Usage: %s\n\n", args[0]);
        printf("Description:\n");
        printf("\tFilesystem Test\n");
        printf("Options:\n");
        printf("\t--help\tdisplay this help and exit\n");
        return OK;
    }

    /* Check for correct number of arguments */
    if (nargs > 1)
    {
        fprintf(stderr, "%s: too many arguments\n", args[0]);
        fprintf(stderr, "Try '%s --help' for more information\n",
                args[0]);
        return SYSERR;
    }
    if (nargs < 1)
    {
        fprintf(stderr, "%s: too few arguments\n", args[0]);
        fprintf(stderr, "Try '%s --help' for more information\n",
                args[0]);
        return SYSERR;
    }

#ifdef FS

    mkbsdev(0, 0, 0); /* device "0" and default blocksize (=0) and count */
    mkfs(0,DEFAULT_NUM_INODES); /* bsdev 0*/
    //testbitmask();
    
    buf1 = memget(SIZE*sizeof(char));
    buf2 = memget(SIZE*sizeof(char));
    
    fd = fcreat("a", O_CREAT);
       
    for(i=0; i<SIZE; i++)
    {
        j = i%(127-33);
        j = j+33;
        buf1[i] = (char) j;
    }
    
    rval = fwrite(fd,buf1,SIZE);
    if(rval == 0)
    {
        printf("\n\r File write failed");
    }
    fseek(fd,-rval); 
    
    rval = fread(fd, buf2, rval);
    buf2[rval] = '\0';
    
    if(rval == 0)
    {
        printf("\n\r File read failed");
    }
        
    printf("\n\rContent of file %s",buf2);
    
    rval = fclose(fd);
    if(rval != OK)
    {
        printf("\n\rReturn val for fclose : %d",rval);
    }
    memfree(buf1,SIZE);
    memfree(buf2,SIZE);
    
#else
    printf("No filesystem support\n");
#endif

    return OK;
}
示例#19
0
static int
scan_interfaces(int *have_v4, int *have_v6) {
	struct ifconf ifc;
	union {
		char _pad[256];		/*%< leave space for IPv6 addresses */
		struct ifreq ifreq;
	} u;
	struct in_addr in4;
	struct in6_addr in6;
	char *buf = NULL, *cp, *cplim;
	static unsigned int bufsiz = 4095;
	int s, n;
	size_t cpsize;

	/* Set to zero.  Used as loop terminators below. */
	*have_v4 = *have_v6 = 0;

#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR) && \
    !defined(IRIX_EMUL_IOCTL_SIOCGIFCONF) 
	/*
	 * Try to scan the interfaces using IPv6 ioctls().
	 */
	scan_interfaces6(have_v4, have_v6);
	if (*have_v4 != 0 && *have_v6 != 0)
		return (0);
#endif
#ifdef __linux
	scan_linux6(have_v6);
#endif

	/* Get interface list from system. */
	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
		goto err_ret;

	/*
	 * Grow buffer until large enough to contain all interface
	 * descriptions.
	 */
	for (;;) {
		buf = memget(bufsiz);
		if (buf == NULL)
			goto err_ret;
		ifc.ifc_len = bufsiz;
		ifc.ifc_buf = buf;
#ifdef IRIX_EMUL_IOCTL_SIOCGIFCONF
		/*
		 * This is a fix for IRIX OS in which the call to ioctl with
		 * the flag SIOCGIFCONF may not return an entry for all the
		 * interfaces like most flavors of Unix.
		 */
		if (emul_ioctl(&ifc) >= 0)
			break;
#else
		if ((n = ioctl(s, SIOCGIFCONF, (char *)&ifc)) != -1) {
			/*
			 * Some OS's just return what will fit rather
			 * than set EINVAL if the buffer is too small
			 * to fit all the interfaces in.  If 
			 * ifc.ifc_len is too near to the end of the
			 * buffer we will grow it just in case and
			 * retry.
			 */
			if (ifc.ifc_len + 2 * sizeof(u.ifreq) < bufsiz)
				break;
		}
#endif
		if ((n == -1) && errno != EINVAL)
			goto err_ret;

		if (bufsiz > 1000000)
			goto err_ret;

		memput(buf, bufsiz);
		bufsiz += 4096;
	}

	/* Parse system's interface list. */
	cplim = buf + ifc.ifc_len;    /*%< skip over if's with big ifr_addr's */
	for (cp = buf;
	     (*have_v4 == 0 || *have_v6 == 0) && cp < cplim;
	     cp += cpsize) {
		memcpy(&u.ifreq, cp, sizeof u.ifreq);
#ifdef HAVE_SA_LEN
#ifdef FIX_ZERO_SA_LEN
		if (u.ifreq.ifr_addr.sa_len == 0)
			u.ifreq.ifr_addr.sa_len = 16;
#endif
#ifdef HAVE_MINIMUM_IFREQ
		cpsize = sizeof u.ifreq;
		if (u.ifreq.ifr_addr.sa_len > sizeof (struct sockaddr))
			cpsize += (int)u.ifreq.ifr_addr.sa_len -
				(int)(sizeof (struct sockaddr));
#else
		cpsize = sizeof u.ifreq.ifr_name + u.ifreq.ifr_addr.sa_len;
#endif /* HAVE_MINIMUM_IFREQ */
		if (cpsize > sizeof u.ifreq && cpsize <= sizeof u)
			memcpy(&u.ifreq, cp, cpsize);
#elif defined SIOCGIFCONF_ADDR
		cpsize = sizeof u.ifreq;
#else
		cpsize = sizeof u.ifreq.ifr_name;
		/* XXX maybe this should be a hard error? */
		if (ioctl(s, SIOCGIFADDR, (char *)&u.ifreq) < 0)
			continue;
#endif
		switch (u.ifreq.ifr_addr.sa_family) {
		case AF_INET:
			if (*have_v4 == 0) {
				memcpy(&in4,
				       &((struct sockaddr_in *)
				       &u.ifreq.ifr_addr)->sin_addr,
				       sizeof in4);
				if (in4.s_addr == INADDR_ANY)
					break;
				n = ioctl(s, SIOCGIFFLAGS, (char *)&u.ifreq);
				if (n < 0)
					break;
				if ((u.ifreq.ifr_flags & IFF_UP) == 0)
					break;
				*have_v4 = 1;
			} 
			break;
		case AF_INET6:
			if (*have_v6 == 0) {
				memcpy(&in6,
				       &((struct sockaddr_in6 *)
				       &u.ifreq.ifr_addr)->sin6_addr,
				       sizeof in6);
				if (memcmp(&in6, &in6addr_any, sizeof in6) == 0)
					break;
				n = ioctl(s, SIOCGIFFLAGS, (char *)&u.ifreq);
				if (n < 0)
					break;
				if ((u.ifreq.ifr_flags & IFF_UP) == 0)
					break;
				*have_v6 = 1;
			}
			break;
		}
	}
	if (buf != NULL)
		memput(buf, bufsiz);
	close(s);
	/* printf("scan interface -> 4=%d 6=%d\n", *have_v4, *have_v6); */
	return (0);
 err_ret:
	if (buf != NULL)
		memput(buf, bufsiz);
	if (s != -1)
		close(s);
	/* printf("scan interface -> 4=%d 6=%d\n", *have_v4, *have_v6); */
	return (-1);
}
示例#20
0
static void
scan_interfaces6(int *have_v4, int *have_v6) {
	struct LIFCONF lifc;
	struct LIFREQ lifreq;
	struct in_addr in4;
	struct in6_addr in6;
	char *buf = NULL, *cp, *cplim;
	static unsigned int bufsiz = 4095;
	int s, cpsize, n;

	/* Get interface list from system. */
	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) == -1)
		goto cleanup;

	/*
	 * Grow buffer until large enough to contain all interface
	 * descriptions.
	 */
	for (;;) {
		buf = memget(bufsiz);
		if (buf == NULL)
			goto cleanup;
#ifdef SETFAMILYFLAGS
		lifc.lifc_family = AF_UNSPEC;	/*%< request all families */
		lifc.lifc_flags = 0;
#endif
		lifc.lifc_len = bufsiz;
		lifc.lifc_buf = buf;
		if ((n = ioctl(s, SIOCGLIFCONF, (char *)&lifc)) != -1) {
			/*
			 * Some OS's just return what will fit rather
			 * than set EINVAL if the buffer is too small
			 * to fit all the interfaces in.  If 
			 * lifc.lifc_len is too near to the end of the
			 * buffer we will grow it just in case and
			 * retry.
			 */
			if (lifc.lifc_len + 2 * sizeof(lifreq) < bufsiz)
				break;
		}
		if ((n == -1) && errno != EINVAL)
			goto cleanup;

		if (bufsiz > 1000000)
			goto cleanup;

		memput(buf, bufsiz);
		bufsiz += 4096;
	}

	/* Parse system's interface list. */
	cplim = buf + lifc.lifc_len;    /*%< skip over if's with big ifr_addr's */
	for (cp = buf;
	     (*have_v4 == 0 || *have_v6 == 0) && cp < cplim;
	     cp += cpsize) {
		memcpy(&lifreq, cp, sizeof lifreq);
#ifdef HAVE_SA_LEN
#ifdef FIX_ZERO_SA_LEN
		if (lifreq.lifr_addr.sa_len == 0)
			lifreq.lifr_addr.sa_len = 16;
#endif
#ifdef HAVE_MINIMUM_IFREQ
		cpsize = sizeof lifreq;
		if (lifreq.lifr_addr.sa_len > sizeof (struct sockaddr))
			cpsize += (int)lifreq.lifr_addr.sa_len -
				(int)(sizeof (struct sockaddr));
#else
		cpsize = sizeof lifreq.lifr_name + lifreq.lifr_addr.sa_len;
#endif /* HAVE_MINIMUM_IFREQ */
#elif defined SIOCGIFCONF_ADDR
		cpsize = sizeof lifreq;
#else
		cpsize = sizeof lifreq.lifr_name;
		/* XXX maybe this should be a hard error? */
		if (ioctl(s, SIOCGLIFADDR, (char *)&lifreq) < 0)
			continue;
#endif
		switch (lifreq.lifr_addr.ss_family) {
		case AF_INET:
			if (*have_v4 == 0) {
				memcpy(&in4,
				       &((struct sockaddr_in *)
				       &lifreq.lifr_addr)->sin_addr,
				       sizeof in4);
				if (in4.s_addr == INADDR_ANY)
					break;
				n = ioctl(s, SIOCGLIFFLAGS, (char *)&lifreq);
				if (n < 0)
					break;
				if ((lifreq.lifr_flags & IFF_UP) == 0)
					break;
				*have_v4 = 1;
			} 
			break;
		case AF_INET6:
			if (*have_v6 == 0) {
				memcpy(&in6,
				       &((struct sockaddr_in6 *)
				       &lifreq.lifr_addr)->sin6_addr, sizeof in6);
				if (memcmp(&in6, &in6addr_any, sizeof in6) == 0)
					break;
				n = ioctl(s, SIOCGLIFFLAGS, (char *)&lifreq);
				if (n < 0)
					break;
				if ((lifreq.lifr_flags & IFF_UP) == 0)
					break;
				*have_v6 = 1;
			}
			break;
		}
	}
	if (buf != NULL)
		memput(buf, bufsiz);
	close(s);
	/* printf("scan interface -> 4=%d 6=%d\n", *have_v4, *have_v6); */
	return;
 cleanup:
	if (buf != NULL)
		memput(buf, bufsiz);
	if (s != -1)
		close(s);
	/* printf("scan interface -> 4=%d 6=%d\n", *have_v4, *have_v6); */
	return;
}
示例#21
0
文件: nvram.c 项目: JamesLinus/xinu
/**
 * Commit nvram settings to memory. Due to the nature of nvram settings
 * writing to memory everytime would greatly reduce the life of flash, so
 * after making changes you must commit them to memory.
 * @return OK on success, SYSERR on failure
 */
devcall nvramCommit()
{
    struct dentry *devptr = NULL;
    struct flash *flash = NULL;
    struct nvram_tuple *tuple;
    struct flash_block block, test_block;
    uchar *buffer;
    ulong n, offset, pos;

    if (OK != nvramInit())
    {
        return SYSERR;
    }

#if FLASH
    devptr = (device *)&devtab[FLASH];
#endif
    if (NULL == devptr)
    {
        return SYSERR;
    }
    flash = &flashtab[devptr->minor];

    /* length is maintained length + 2 (null string buffers) */
    /* and + 3 & ~0x03 to force word alignment               */
    nvram_header->length = (nvram_header->length + 2 + 3) & ~0x03;

    /* prepare a buffer to store all settings in */
    buffer = (uchar *)memget(nvram_header->length);
    if (SYSERR == (int)buffer)
    {
        return SYSERR;
    }
    bzero(buffer, nvram_header->length);

    /* clear header crc */
    nvram_header->crc_ver_init &= ~NVRAM_CRC_MASK;

    /* copy header into block */
    memcpy(buffer, nvram_header, sizeof(struct nvram_header));
    offset = sizeof(struct nvram_header);

    /* start at offset in block and write byte for byte */
    for (n = 0; n < NVRAM_NHASH; n++)
    {
        tuple = nvram_tuples[n];
        if (NULL == tuple)
        {
            continue;
        }
        do
        {
            /* copy tuple into buffer */
            memcpy(buffer + offset, tuple->pair,
                   strnlen(tuple->pair, NVRAM_STRMAX));
            /* next position + 1 for null-terminiation */
            offset += strnlen(tuple->pair, NVRAM_STRMAX) + 1;
            /* optionally, free the tuple memory */
            //memfree((void *)tuple);
        }
        while ((tuple = tuple->next) != NULL);
    }

    /* calculate crc */
    nvram_header->crc_ver_init |= ((uchar)nvramCrc(buffer));

    /* write new crc into block */
    memcpy(buffer, nvram_header, sizeof(struct nvram_header));


    /* to maintain data integrity check if the last block is being used */
    block = logicalMap(flash, NVRAM_MIN_BLOCK);

    for (n = 0; n < MAX_LIVE_BLOCKS; n++)
    {
        pos = (flash->curr_block + n) % MAX_LIVE_BLOCKS;
        test_block = flash->erase_blocks[pos];
        if (test_block.start_pos == block.start_pos)
        {
            if (FLASH_BLOCK_FREE != block.state)
            {
                block = test_block;
                break;
            }
        }
    }

    if (MAX_LIVE_BLOCKS <= n)
    {
        /* it is not cached in main memory */
        /* evict a block */
        physicalWrite(flash, &(flash->erase_blocks[flash->curr_block]));
        block.state = FLASH_BLOCK_FREE;
        /* read in block with nvram settings */
        physicalRead(flash, &block);
    }

    /* write the settings */
    offset = block.size - NVRAM_SIZE;
    memcpy(block.buffer + offset, buffer, nvram_header->length);
    block.state = FLASH_BLOCK_DIRTY;
    /* force a sync */
#if FLASH
    control(FLASH, FLASH_SYNC, FLASH_BLOCK, (long)&block);
#endif
    /* finally, free the block and return */
    memfree((void *)buffer, nvram_header->length);

    return OK;
}
示例#22
0
static struct hostent *
copyandmerge(struct hostent *he1, struct hostent *he2, int af, int *error_num) {
	struct hostent *he = NULL;
	int addresses = 1;	/*%< NULL terminator */
	int names = 1;		/*%< NULL terminator */
	int len = 0;
	char **cpp, **npp;

	/*
	 * Work out array sizes;
	 */
	if (he1 != NULL) {
		cpp = he1->h_addr_list;
		while (*cpp != NULL) {
			addresses++;
			cpp++;
		}
		cpp = he1->h_aliases;
		while (*cpp != NULL) {
			names++;
			cpp++;
		}
	}

	if (he2 != NULL) {
		cpp = he2->h_addr_list;
		while (*cpp != NULL) {
			addresses++;
			cpp++;
		}
		if (he1 == NULL) {
			cpp = he2->h_aliases;
			while (*cpp != NULL) {
				names++;
				cpp++;
			}
		}
	}

	if (addresses == 1) {
		*error_num = NO_ADDRESS;
		return (NULL);
	}

	he = memget(sizeof *he);
	if (he == NULL)
		goto no_recovery;

	he->h_addr_list = memget(sizeof(char *) * (addresses));
	if (he->h_addr_list == NULL)
		goto cleanup0;
	memset(he->h_addr_list, 0, sizeof(char *) * (addresses));

	/* copy addresses */
	npp = he->h_addr_list;
	if (he1 != NULL) {
		cpp = he1->h_addr_list;
		while (*cpp != NULL) {
			*npp = memget((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
			if (*npp == NULL)
				goto cleanup1;
			/* convert to mapped if required */
			if (af == AF_INET6 && he1->h_addrtype == AF_INET) {
				memcpy(*npp, in6addr_mapped,
				       sizeof in6addr_mapped);
				memcpy(*npp + sizeof in6addr_mapped, *cpp,
				       INADDRSZ);
			} else {
				memcpy(*npp, *cpp,
				       (af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
			}
			cpp++;
			npp++;
		}
	}

	if (he2 != NULL) {
		cpp = he2->h_addr_list;
		while (*cpp != NULL) {
			*npp = memget((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
			if (*npp == NULL)
				goto cleanup1;
			/* convert to mapped if required */
			if (af == AF_INET6 && he2->h_addrtype == AF_INET) {
				memcpy(*npp, in6addr_mapped,
				       sizeof in6addr_mapped);
				memcpy(*npp + sizeof in6addr_mapped, *cpp,
				       INADDRSZ);
			} else {
				memcpy(*npp, *cpp,
				       (af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
			}
			cpp++;
			npp++;
		}
	}

	he->h_aliases = memget(sizeof(char *) * (names));
	if (he->h_aliases == NULL)
		goto cleanup1;
	memset(he->h_aliases, 0, sizeof(char *) * (names));

	/* copy aliases */
	npp = he->h_aliases;
	cpp = (he1 != NULL) ? he1->h_aliases : he2->h_aliases;
	while (*cpp != NULL) {
		len = strlen (*cpp) + 1;
		*npp = memget(len);
		if (*npp == NULL)
			goto cleanup2;
		strcpy(*npp, *cpp);
		npp++;
		cpp++;
	}

	/* copy hostname */
	he->h_name = memget(strlen((he1 != NULL) ?
			    he1->h_name : he2->h_name) + 1);
	if (he->h_name == NULL)
		goto cleanup2;
	strcpy(he->h_name, (he1 != NULL) ? he1->h_name : he2->h_name);

	/* set address type and length */
	he->h_addrtype = af;
	he->h_length = (af == AF_INET) ? INADDRSZ : IN6ADDRSZ;
	return(he);

 cleanup2:
	cpp = he->h_aliases;
	while (*cpp != NULL) {
		memput(*cpp, strlen(*cpp) + 1);
		cpp++;
	}
	memput(he->h_aliases, sizeof(char *) * (names));

 cleanup1:
	cpp = he->h_addr_list;
	while (*cpp != NULL) {
		memput(*cpp, (af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
		*cpp = NULL;
		cpp++;
	}
	memput(he->h_addr_list, sizeof(char *) * (addresses));

 cleanup0:
	memput(he, sizeof *he);

 no_recovery:
	*error_num = NO_RECOVERY;
	return (NULL);
}
示例#23
0
static int ethloopn_test(bool verbose, int dev)
{
    bool passed = TRUE;
    bool subpass;
    uint memsize;
    int i, value, len;
    struct etherGram *inpkt;
    struct etherGram *outpkt;
    char *payload;
    char str[80];
    int devminor;
    struct ethloop *pelp;
    struct netaddr addr;
    device *pdev;

    pdev = (device *)&devtab[dev];
    devminor = pdev->minor;
    pelp = &elooptab[devminor];

    sprintf(str, "%s Open", pdev->name);
    testPrint(verbose, str);
    failif((SYSERR == open(dev)), "");

    /* Allocate temporary buffers.  */
    memsize = sizeof(struct etherGram) + MAX_PAYLOAD - 1;
    inpkt = memget(memsize);
    outpkt = memget(memsize);

    payload = &(outpkt->payload[0]);

    control(dev, NET_GET_HWADDR, (int)&addr, NULL);
    memcpy(outpkt->dst, addr.addr, addr.len);
    memcpy(outpkt->src, addr.addr, addr.len);
    outpkt->type_len = hs2net(ETH_TYPE_ARP);

    /* generate payload content */
    for (i = 0; i < MAX_PAYLOAD; i++)
    {
        /* Cycle through 0x20 to 0x7d (range of 0x5e) */
        value = (i % 0x5e) + 0x20;
        payload[i] = value;
    }

    /* oversized packet (paylod 1502 bytes + 14 byte header) */
    sprintf(str, "%s 1516 byte packet", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 1516);
    failif((SYSERR != len), "");

    /* max packet (payload 1500 bytes + 14 byte header) */
    sprintf(str, "%s 1514 byte packet (write)", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 1514);
    failif((len != 1514), "");

    sprintf(str, "%s 1514 byte packet (read)", pelp->dev->name);
    testPrint(verbose, str);
    bzero(inpkt, memsize);
    len = read(dev, inpkt, 1514);
    failif((len != 1514) || (0 != memcmp(outpkt, inpkt, 1514)), "");

    /* 'normal' packet (payload 686 bytes + 14 byte header) */
    sprintf(str, "%s  700 byte packet (write)", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 700);
    failif((len != 700), "");

    sprintf(str, "%s  700 byte packet (read)", pelp->dev->name);
    testPrint(verbose, str);
    bzero(inpkt, memsize);
    len = read(dev, inpkt, 700);
    failif((len != 700) || (0 != memcmp(outpkt, inpkt, 700)), "");

    /* small packet (payload 16 bytes + 14 byte header) */
    sprintf(str, "%s   30 byte packet (write)", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 30);
    failif((len != 30), "");

    sprintf(str, "%s   30 byte packet (read)", pelp->dev->name);
    testPrint(verbose, str);
    bzero(inpkt, memsize);
    len = read(dev, inpkt, 30);
    failif((len != 30) || (0 != memcmp(outpkt, inpkt, 30)), "");

    /* micro packet (12 bytes) */
    sprintf(str, "%s   12 byte packet", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 12);
    failif((SYSERR != len), "");

    /* send 512 random sized packets */
    sprintf(str, "%s  512 random-sized packets", pelp->dev->name);
    testPrint(verbose, str);
    subpass = TRUE;

    for (i = 0; i < 512; i++)
    {
        len = 32 + (rand() % 1200);
        value = write(dev, outpkt, len);
        if (value != len)
        {
            subpass = FALSE;
        }

        bzero(inpkt, memsize);
        value = read(dev, inpkt, len);
        if ((value != len) || (0 != memcmp(outpkt, inpkt, len)))
        {
            subpass = FALSE;
        }
    }
    failif((TRUE != subpass), "");

    /* hold packet (payload 686 bytes + 14 byte header) */
    control(dev, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_HOLDNXT, NULL);
    sprintf(str, "%s  700 byte packet (write hold)", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 700);
    failif((len != 700), "");

    sprintf(str, "%s  700 byte packet (read hold)", pelp->dev->name);
    testPrint(verbose, str);
    bzero(inpkt, memsize);
    len = control(dev, ELOOP_CTRL_GETHOLD, (int)inpkt, 700);
    failif((0 != memcmp(outpkt, inpkt, 700)), "");

    /* drop packet (payload 686 bytes + 14 byte header) */
    control(dev, ELOOP_CTRL_SETFLAG, ELOOP_FLAG_DROPNXT, NULL);
    sprintf(str, "%s  700 byte packet (write drop)", pelp->dev->name);
    testPrint(verbose, str);
    len = write(dev, outpkt, 700);
    failif((len != 700), "");

    sprintf(str, "%s  700 byte packet (write)", pelp->dev->name);
    testPrint(verbose, str);
    outpkt->dst[0] += 1;
    len = write(dev, outpkt, 700);
    failif((len != 700), "");

    sprintf(str, "%s  700 byte packet (read)", pelp->dev->name);
    testPrint(verbose, str);
    bzero(inpkt, memsize);
    len = read(dev, inpkt, 700);
    failif((len != 700) || (0 != memcmp(outpkt, inpkt, 700)), "");

    /* Free temporary buffers. */
    memfree(outpkt, memsize);
    memfree(inpkt, memsize);

    sprintf(str, "%s Close", pdev->name);
    testPrint(verbose, str);
    failif((SYSERR == close(dev)), "");

    return passed;
}
示例#24
0
文件: nvram.c 项目: JamesLinus/xinu
/**
 * Initialize the nvram variable structures for editing
 * @return OK on success, SYSERR on failure
 */
devcall nvramInit(void)
{
    struct dentry *devptr = NULL;
    struct flash *flash = NULL;
    uint nvbase, offset, index, pair_len;
    uint nvram_length, size;
    char *pair;
    struct nvram_tuple *tuple;

    /* check if we already have initialized nvram */
    if (nvram_header != NULL && NVRAM_MAGIC == nvram_header->magic)
    {
        return OK;
    }

#if FLASH
    devptr = (device *)&devtab[FLASH];
#endif
    if (NULL == devptr)
    {
        return SYSERR;
    }

    flash = &flashtab[devptr->minor];

    /* zero out nvram_tuples pointers */
    for (index = 0; index < NVRAM_NHASH; index++)
    {
        nvram_tuples[index] = NULL;
    }

    /* Scan flash at for NVRAM magic number */
    nvbase = flash->base + flash->size - NVRAM_SIZE;
    while (nvbase > flash->base)
    {
        if (NVRAM_MAGIC == *((uint *)nvbase))
            break;
        nvbase -= NVRAM_SIZE;
    }
    offset = 0;

    /* find the head for data */
    nvram_header =
        (struct nvram_header *)memget(sizeof(struct nvram_header));
    memcpy(nvram_header, (void *)nvbase, sizeof(struct nvram_header));
    if (nvram_header->magic != NVRAM_MAGIC)
    {
        return SYSERR;
    }

    offset += sizeof(struct nvram_header);

    /* loop through nvram variables and add to array */
    nvram_length = nvram_header->length;

    nvram_header->length = sizeof(struct nvram_header);

    while (offset < nvram_length)
    {
        /* get the length of a string (name=value\0) */
        pair = (char *)(nvbase + offset);
        pair_len = strnlen(pair, nvram_length - offset);

        /* set offset to next string */
        offset += pair_len + 1;

        if (pair_len <= 0)
        {
            continue;
        }

        /* allocate memory to store tuple */
        size = sizeof(struct nvram_tuple) + pair_len;
        tuple = memget(size);

        /* store tuple */
        memcpy(tuple->pair, pair, pair_len);
        memcpy(tuple->pair + pair_len, "\0", 1);


        nvramInsert(tuple);
    }

    return OK;
}
示例#25
0
static PyObject*
immom_saImmOmAdminOperationInvoke(PyObject *self, PyObject *args)
{
    SaAisErrorT rc, oprc;
    PyObject* attrList;
    unsigned int len, i;
    char* dn;
    unsigned long long op;
    SaNameT objectName;
    SaImmAdminOperationParamsT_2** params;

    if (!haveAdminOwner) 
        return immom_aisException(SA_AIS_ERR_BAD_OPERATION);
    if (!PyArg_ParseTuple(args, "sLO", &dn, &op, &attrList))
        return NULL;
    if (immom_saName(dn, &objectName) == NULL)
        return NULL;
    if (!PyList_Check(attrList))
        return immom_aisException(SA_AIS_ERR_INVALID_PARAM);

    len = PyList_Size(attrList);
    params = (SaImmAdminOperationParamsT_2**)memget(
        sizeof(SaImmAdminOperationParamsT_2*) * (len+1));
    params[len] = NULL;

    for (i = 0; i < len; i++) {
        PyObject* item = PyList_GetItem(attrList, i);
        char* attrName;
        char* attrType;
        PyObject* valueList;
        SaImmAttrValuesT_2 av;
        SaImmAdminOperationParamsT_2* p;

        if (!PyTuple_Check(item))
            return immom_aisException(SA_AIS_ERR_INVALID_PARAM);
        if (!PyArg_ParseTuple(item, "ssO", &attrName, &attrType, &valueList)) 
            return immom_return_null();
        if (!PyList_Check(valueList))
            return immom_aisException(SA_AIS_ERR_INVALID_PARAM);

        if (immom_parseAttrValue(
                &av, attrName, attrType, valueList) == NULL)
            return immom_return_null();
        if (av.attrValuesNumber != 1)
            return immom_aisException(SA_AIS_ERR_INVALID_PARAM);

        p = (SaImmAdminOperationParamsT_2*)
            memget(sizeof(SaImmAdminOperationParamsT_2));
        p->paramName = av.attrName;
        p->paramType = av.attrValueType;
        p->paramBuffer = av.attrValues[0];
        params[i] = p;
    }

    rc = saImmOmAdminOperationInvoke_2(
        adminOwnerHandle, &objectName, 0ULL, op, 
        (SaImmAdminOperationParamsT_2 const**)params, &oprc, 
        SA_TIME_ONE_MINUTE);
    if (rc != SA_AIS_OK)
        return immom_aisException(rc);
    if (oprc != SA_AIS_OK)
        return immom_aisException(oprc);

    return immom_return_None();
}
示例#26
0
static void kexec_from_uart(int uartdev)
{
#ifdef _XINU_PLATFORM_ARM_RPI_
    irqmask im;
    device *uart;
    ulong size;
    void *kernel;
    uchar *p;
    ulong n;

    im = disable();

    uart = (device*)&devtab[uartdev];

    /* Tell raspbootcom to send the new kernel.  */
    kputc('\x03', uart);
    kputc('\x03', uart);
    kputc('\x03', uart);

    /* Receive size of the new kernel.  */
    for (;;)
    {
        size = (ulong)kgetc(uart);
        size |= (ulong)kgetc(uart) << 8;
        size |= (ulong)kgetc(uart) << 16;
        size |= (ulong)kgetc(uart) << 24;
        if (size <= 99999999 && size != 0)
        {
            break;
        }

        /* Tell raspbootcom to re-send the size.  */
        kputc('S', uart);
        kputc('E', uart);
    }

    /* Tell raspbootcom the size was successfully received.  */
    kputc('O', uart);
    kputc('K', uart);

    /* Allocate buffer for new kernel.  */
    kernel = memget(size);
    if (kernel == (void*)SYSERR)
    {
        return;
    }

    /* Load new kernel over the UART, placing it into the buffer.  */
    p = kernel;
    n = size;
    while (n--)
    {
        *p++ = kgetc(uart);
    }

    /* Execute the new kernel.  */
    kexec(kernel, size);

    /* The following code should never actually be reached.  */
    memfree(kernel, size);
    restore(im);
#else /* _XINU_PLATFORM_ARM_RPI_ */
    fprintf(stderr, "ERROR: kexec from UART not supported on this platform.\n");
#endif /* !_XINU_PLATFORM_ARM_RPI_ */
}
示例#27
0
文件: crcal.c 项目: tlapicka/crcal
/****************************************************
 *               Hlavní program.
 ****************************************************/
int main(void)
{
    double rnumber;             // číslo se kterým se má počítat
    double inumber;             // číslo se kterým se má počítat
    char enter[CMDLEN];         // vstup od uživatele

    setrad();
    /* Čte řádky až do konce souboru */
    while (readEnter(enter) != EOF) {
        if (sscanf(enter, "%lg,%lg", &rnumber, &inumber) == 2) {
            pushalg(rnumber, inumber);
        } else if (sscanf(enter, "%lgL%lg", &rnumber, &inumber) == 2) {
            pushexp(rnumber, inumber);
        } else if (sscanf(enter, ",%lg", &inumber) == 1) {
            pushalg(0, inumber);
        } else if (sscanf(enter, "%lg", &rnumber) == 1) {
            pushalg(rnumber, 0);
        } else if (strcmp(enter, "pi") == 0) {
            pushalg(M_PI, 0);
        } else if (strcmp(enter, "e") == 0) {
            pushalg(M_E, 0);
        } else if (strcmp(enter, "+") == 0) {
            make2wrap(makePlus, enter);
        } else if (strcmp(enter, "-") == 0) {
            make2wrap(makeMinus, enter);
        } else if (strcmp(enter, "*") == 0) {
            make2wrap(makeTimes, enter);
        } else if (strcmp(enter, "/") == 0) {
            make2wrap(makeDivid, enter);
        } else if (strcmp(enter, "**") == 0) {
            /* n-tá mocnina */
            make2wrap(cpow, enter);
        } else if (strcmp(enter, "^") == 0) {
            /* převrácená hodnota */
            make1wrap(makeFlipp, enter);
        } else if (strcmp(enter, "sqrt") == 0 || strcmp(enter, "t") == 0) {
            make1wrap(csqrt, enter);
        } else if (strcmp(enter, "exp") == 0 || strcmp(enter, "x") == 0) {
            /* exponenciální funkce */
            make1wrap(cexp, enter);
        } else if (strcmp(enter, "ln") == 0) {
            make1wrap(clog, enter);
        } else if (strcmp(enter, "log") == 0) {
            make1wrap(clog10, enter);
        } else if (strcmp(enter, "r") == 0) {
            /* přepne kalkulačku do radiánů */
            setrad();
        } else if (strcmp(enter, "d") == 0) {
            /* přepne kalkulačku do stupňů */
            setdeg();
        } else if (strcmp(enter, "la") == 0) {
            /* poslední číslo převede do algebraickém tvaru */
            last2alg();
            printStack();
        } else if (strcmp(enter, "le") == 0) {
            /* poslední číslo převede exponenciálním tvaru */
            last2exp();
            printStack();
        } else if (strcmp(enter, "aa") == 0) {
            /* všechny čísla převede do algebraickém tvaru */
            all2alg();
            printStack();
        } else if (strcmp(enter, "ae") == 0) {
            /* všechny čísla převede do exponenciálním tvaru */
            all2exp();
            printStack();
        } else if (strcmp(enter, "da") == 0) {
            setalg();
        } else if (strcmp(enter, "de") == 0) {
            setexp();
        } else if (strcmp(enter, "switch") == 0 || strcmp(enter, "s") == 0) {
            if (!lastSwitch())
                fprintf(stderr, " error > Nelze prohodit\n");
        } else if (strcmp(enter, "deg") == 0) {
            make1wrap(rad2deg, enter);
        } else if (strcmp(enter, "rad") == 0) {
            make1wrap(deg2rad, enter);
            /* Goniometrické funkce */
        } else if (strcmp(enter, "sin") == 0) {
            make1wrap(csin, enter);
        } else if (strcmp(enter, "cos") == 0) {
            make1wrap(ccos, enter);
        } else if (strcmp(enter, "tan") == 0 || strcmp(enter, "tg") == 0) {
            make1wrap(ctan, enter);
        } else if (strcmp(enter, "asin") == 0) {
            make1wrap(casin, enter);
        } else if (strcmp(enter, "acos") == 0) {
            make1wrap(cacos, enter);
        } else if (strcmp(enter, "atan") == 0 || strcmp(enter, "atg") == 0) {
            make1wrap(catan, enter);
        } else if (enter[0] == '>') {
            /* ukládá do proměnné */
            complex double number = pop();
            memput(number, enter, algform ? ALGFORM : EXPFORM);
            push(number, algform);
        } else if (enter[0] == '<') {
            /* vybírá obsah proměnné */
            enter[0] = '>';
            char form;
            complex double number = memget(enter, &form);
            push(number, form == ALGFORM ? true : false);
        } else if (strcmp(enter, "rem") == 0 || strcmp(enter, "m") == 0) {
            pop();
            printStack();
        } else if (strcmp(enter, "clr") == 0 || strcmp(enter, "c") == 0) {
            stackclear();       // vymaže celý zásobník
        } else if (strcmp(enter, "p") == 0 || strcmp(enter, "print") == 0) {    // jen tiskne zásobník
            printStack();
        } else if (strcmp(enter, "help") == 0 || strcmp(enter, "h") == 0) {
            printHelp();
        } else if (strcmp(enter, "quit") == 0 || strcmp(enter, "q") == 0) {
            break;
        } else {
            fprintf(stderr, " error > %s\n", enter);
            fprintf(stderr, " error > Neplatný vstup\n");
        }
    }
    stackclear();               // maže zásobník
    memclear();               // maže zásobník
    return 0;
}
示例#28
0
static PyObject *
immom_saImmOmClassCreate(PyObject *self, PyObject *args)
{
    char* className;
    char* categoryStr;
    SaImmClassCategoryT classCategory;
    PyObject* alist;
    unsigned int len, i;
    SaImmAttrDefinitionT_2** attrDefinitions;
    SaAisErrorT rc;

    if (!PyArg_ParseTuple(args, "ssO", &className, &categoryStr, &alist)) 
        return NULL;
    if (!PyList_Check(alist))
        return immom_aisException(SA_AIS_ERR_INVALID_PARAM);
    if (strcmp(categoryStr, "CONFIG") == 0) {
        classCategory = SA_IMM_CLASS_CONFIG;
    } else if (strcmp(categoryStr, "RUNTIME") == 0) {
        classCategory = SA_IMM_CLASS_RUNTIME;
    } else {
        return immom_aisException(SA_AIS_ERR_INVALID_PARAM);
    }

    len = PyList_Size(alist);
    attrDefinitions = (SaImmAttrDefinitionT_2**)
        memget(sizeof(SaImmAttrDefinitionT_2*) * (len+1));
    attrDefinitions[len] = NULL;

    for (i = 0; i < len; i++) {
        PyObject* item = PyList_GetItem(alist, i);
        char* attrName;
        char* attrType;
        unsigned PY_LONG_LONG flags;
	PyObject* def;
        SaImmAttrDefinitionT_2* ad;

        if (!PyTuple_Check(item))
            return immom_aisException(SA_AIS_ERR_INVALID_PARAM);
        if (!PyArg_ParseTuple(item, "ssKO", &attrName, &attrType, &flags,&def))
            return immom_return_null();
	if (!PyList_Check(def))
	    return immom_aisException(SA_AIS_ERR_INVALID_PARAM);
        ad = (SaImmAttrDefinitionT_2*)memget(sizeof(SaImmAttrDefinitionT_2));
        ad->attrName = attrName;
        ad->attrValueType = immom_parseTypeStr(attrType);
        if ((int)ad->attrValueType == 0)
            return immom_aisException(SA_AIS_ERR_INVALID_PARAM);
        ad->attrFlags = flags;
	ad->attrDefaultValue = NULL;
	if (PyList_Size(def) > 0) {
		SaImmAttrValuesT_2* attr = memget(sizeof(SaImmAttrValuesT_2));
		if (immom_parseAttrValue(attr, attrName, attrType, def)== NULL)
			immom_return_null();
		ad->attrDefaultValue = attr->attrValues[0];
	}
        attrDefinitions[i] = ad;
    }

    rc = saImmOmClassCreate_2(
        immOmHandle, className, classCategory, 
        (const SaImmAttrDefinitionT_2**)attrDefinitions);
    if (rc != SA_AIS_OK) return immom_aisException(rc);
    return immom_return_None();
}
示例#29
0
static SaImmAttrValuesT_2*
immom_parseAttrValue(
    SaImmAttrValuesT_2* attr,   /* OUT-parameter */
    char* name, char const* typestr, PyObject* valueList)
{
    unsigned int i;
    attr->attrName = name;
    attr->attrValueType = immom_parseTypeStr(typestr);
    if ((unsigned int)attr->attrValueType == 0)
        return NULL;
    attr->attrValuesNumber = PyList_Size(valueList);
    attr->attrValues = (SaImmAttrValueT*)memget(
        sizeof(SaImmAttrValueT) * (attr->attrValuesNumber+1));
    attr->attrValues[attr->attrValuesNumber] = NULL;
    for (i = 0; i < attr->attrValuesNumber; i++) {
        PyObject* item = PyList_GetItem(valueList, i);
        switch (attr->attrValueType) {
        case SA_IMM_ATTR_SAINT32T: {
            SaInt32T* vp = (SaInt32T*)memget(sizeof(SaInt32T));
            *vp = PyInt_AsLong(item);
            if (PyErr_Occurred()) return immom_return_null();
            attr->attrValues[i] = vp;
            break;
        }
        case SA_IMM_ATTR_SAUINT32T: {
            SaUint32T* vp = (SaUint32T*)memget(sizeof(SaUint32T));
            *vp = PyInt_AsUnsignedLongMask(item);
            if (PyErr_Occurred()) return immom_return_null();
            attr->attrValues[i] = vp;
            break;
        }
        case SA_IMM_ATTR_SATIMET:
        case SA_IMM_ATTR_SAINT64T: {
            SaInt64T* vp = (SaInt64T*)memget(sizeof(SaInt64T));
            *vp = PyLong_AsLongLong(item);
            if (PyErr_Occurred()) return immom_return_null();
            attr->attrValues[i] = vp;
            break;
        }
        case SA_IMM_ATTR_SAUINT64T: {
            SaUint64T* vp = (SaUint64T*)memget(sizeof(SaUint64T));
	    /* PyLong_AsUnsignedLongLong seems to be broken...
               *vp = PyLong_AsUnsignedLongLong(item);*/
            *vp = PyLong_AsLongLong(item);
            if (PyErr_Occurred()) return immom_return_null();
            attr->attrValues[i] = vp;
            break;
        }
        case SA_IMM_ATTR_SANAMET: {
            SaNameT* vp = (SaNameT*)memget(sizeof(SaNameT));
            char* str = PyString_AsString(item);
            if (str == NULL) return immom_return_null();
            if (immom_saName(str, vp) == NULL) return NULL;
            attr->attrValues[i] = vp;
            break;
        }
        case SA_IMM_ATTR_SASTRINGT: {
            SaStringT* vp = (SaStringT*)memget(sizeof(SaStringT));
            char* str = PyString_AsString(item);
            if (str == NULL) return immom_return_null();
            *vp = str;
            attr->attrValues[i] = vp;
            break;
        }
        case SA_IMM_ATTR_SAFLOATT:
        case SA_IMM_ATTR_SADOUBLET:
        case SA_IMM_ATTR_SAANYT:
        default:
            return immom_aisException(SA_AIS_ERR_NOT_SUPPORTED);
        }
    }
    return attr;
}