/*
 *===========================================================================
 *                    ipcom_proc_copyenv
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
IP_GLOBAL void
ipcom_proc_copyenv(Ipcom_proc *proc_c)
{
    Ipcom_proc         *proc_p;
    Ip_err              retval;

    proc_p = ipcom_proc_self();
    ip_assert(proc_p != IP_NULL);
    ip_assert(proc_c != IP_NULL);

    if (proc_p->env_tree == IP_NULL)
        /* Parent process has no environment variables */
        return;

    /* Create child environment semaphore and tree */
    retval = ipcom_once(&proc_c->env_once, ipcom_env_init, proc_c);
    if (retval != IPCOM_SUCCESS)
    {
        IP_PANIC();
        return;
    }

    ipcom_mutex_lock(proc_p->env_mutex);

    ipcom_hash_for_each(proc_p->env_tree,
                        (Ipcom_hash_foreach_cb_func) ipcom_env_clone,
                        proc_c->env_tree);

    ipcom_mutex_unlock(proc_p->env_mutex);
}
/*
 *===========================================================================
 *                    ipcom_getenv
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
IP_PUBLIC char *
ipcom_getenv(const char *name)
{
#if IPCOM_USE_ENV == IPCOM_ENV_IPCOM
    Ip_err           retval;
    Ipcom_proc      *proc;
    Ipcom_env_entry *env;
    char            *value = IP_NULL;

    proc = ipcom_proc_self();
    ip_assert(proc != IP_NULL);

    retval = ipcom_once(&proc->env_once, ipcom_env_init, proc);
    if (retval != IPCOM_SUCCESS)
    {
        IP_PANIC();
        return IP_NULL;
    }

    ipcom_mutex_lock(proc->env_mutex);

    env = ipcom_hash_get(proc->env_tree, name);
    if (env != IP_NULL)
        value = (char *)env->value;

    ipcom_mutex_unlock(proc->env_mutex);

    return value;

#elif IPCOM_USE_ENV == IPCOM_ENV_NATIVE && (defined(IP_PORT_OSE) || defined(IP_PORT_OSE5))
    char *env;
    static char buf[256];

    env = get_env(current_process(), name);
    if (env != IP_NULL)
    {
        ipcom_strncpy(buf, env, sizeof(buf));
        free_buf((union SIGNAL **)&env);
        return (char *)buf;
    }
    return IP_NULL;

#elif IPCOM_USE_ENV == IPCOM_ENV_NATIVE
    return getenv(name);

#elif defined(IPCOM_USE_SYSVAR) && IPCOM_VR_MAX == 1
    static char buf[256];
    Ip_size_t  buf_size = sizeof(buf);

    return ipcom_sysvar_get(name, buf, &buf_size);

#else
    return IP_NULL;

#endif /* #if IPCOM_USE_ENV == IPCOM_ENV_IPCOM */
}
/*
 *===========================================================================
 *                    ipcom_unsetenv
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
IP_PUBLIC int
ipcom_unsetenv(const char *name)
{
#if IPCOM_USE_ENV == IPCOM_ENV_IPCOM
    Ip_err           retval;
    Ipcom_proc      *proc;
    Ipcom_env_entry *env;

    proc = ipcom_proc_self();
    ip_assert(proc != IP_NULL);

    retval = ipcom_once(&proc->env_once, ipcom_env_init, proc);
    if (retval != IPCOM_SUCCESS)
    {
        IP_PANIC();
        return -1;
    }

    ipcom_mutex_lock(proc->env_mutex);

    env = ipcom_hash_get(proc->env_tree, name);
    if (env != IP_NULL)
        ipcom_env_delete(env, proc->env_tree);

    ipcom_mutex_unlock(proc->env_mutex);

    return 0;

#elif IPCOM_USE_ENV == IPCOM_ENV_NATIVE && (defined(IP_PORT_OSE) || defined(IP_PORT_OSE5))
    set_env(current_process(), name, NULL);
    return 0;

#elif IPCOM_USE_ENV == IPCOM_ENV_NATIVE && defined(IP_PORT_VXWORKS)
    {
        char buf[256];
        int  ret;
        ipcom_snprintf(buf, sizeof(buf), "%s=", name);
        ret = (int)putenv(buf);
        return ret == 0 ? 0 : -1;
    }

#elif IPCOM_USE_ENV == IPCOM_ENV_NATIVE
    return unsetenv(name);

#elif defined(IPCOM_USE_SYSVAR) && IPCOM_VR_MAX == 1
    (void)ipcom_sysvar_unset(name);
    return 0;

#else
    return 0;

#endif /* #if IPCOM_USE_ENV == IPCOM_ENV_IPCOM */
}
/*
 *===========================================================================
 *                    ipcom_clearenv
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
IP_GLOBAL void
ipcom_proc_clearenv(Ipcom_proc *proc)
{
    ip_assert(proc != IP_NULL);

    ipcom_mutex_lock(proc->env_mutex);

    /* Remove all environment variable entries */
    ipcom_hash_for_each(proc->env_tree,
                        (Ipcom_hash_foreach_cb_func) ipcom_env_delete,
                        proc->env_tree);

    ipcom_mutex_unlock(proc->env_mutex);
}
Пример #5
0
/*
 *===========================================================================
 *                       ipppp_work_schedule
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
IP_STATIC int
ipppp_work_schedule(struct Ipppp_action_work *work)
{
    if (ipcom_once(&ipppp_work_once,
                   ipppp_work_init,
                   IP_NULL)
        != IPCOM_SUCCESS)
    {
        IP_PANIC();
        return -1;
    }


    ipcom_mutex_lock(ipppp_work_lock);
    ipcom_list_insert_last(&ipppp_work_queue, &work->list);
    ipcom_mutex_unlock(ipppp_work_lock);

    ipcom_sem_post(ipppp_work_sem);
    return 0;
}
Пример #6
0
/*
 *===========================================================================
 *                       ipppp_work
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
IP_STATIC
IPCOM_PROCESS(ipppp_work)
{
    ipcom_proc_init();

    for (;;)
    {
        struct Ipppp_action_work *work;

        ipcom_sem_wait(ipppp_work_sem);

        ipcom_mutex_lock(ipppp_work_lock);
        work = IPCOM_LIST_FIRST(&ipppp_work_queue);
        ipcom_list_remove(&work->list);
        ipcom_mutex_unlock(ipppp_work_lock);

        ipppp_example_action_work(work);

        ipcom_free(work);
    }

    /* ipcom_proc_exit(); */
}
/*
 *===========================================================================
 *                    ipcom_once
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 */
IP_PUBLIC Ip_err
ipcom_once(Ipcom_once_t *once, Ip_err (*init)(void *param), void *param)
{
    Ip_err           ret = IPCOM_SUCCESS;
    int              old_state;
    Ipcom_wait_queue wq;

    if (once->state == IPCOM_ONCE_STATE_DONE)
        return IPCOM_SUCCESS;

    /* Enter the once monitor */
    ip_assert(ipcom_once_mutex != IPCOM_MUTEX_INVALID);
    ipcom_mutex_lock(ipcom_once_mutex);

    /* Check state */
    switch (old_state = once->state)
    {
    case IPCOM_ONCE_STATE_UNINITIALIZED:
        /* First call, will do the init */
        ipcom_wait_queue_init(&wq);
        once->wq    = &wq;
        once->state = IPCOM_ONCE_STATE_RUNNING;
        break;
    case IPCOM_ONCE_STATE_RUNNING:
        /* Sleep until the init() function has been run */
        ret = ipcom_wait_queue_add_current_proc(once->wq,
                                                IPC_TIMEOUT_INFINITE,
                                                ipcom_mutex_unlock,
                                                ipcom_mutex_lock,
                                                ipcom_once_mutex);
        break;
    case IPCOM_ONCE_STATE_DONE:
        break;
    default:
        IP_PANIC();
        break;
    }

    /* Exit the once monitor */
    ipcom_mutex_unlock(ipcom_once_mutex);

    if (old_state == IPCOM_ONCE_STATE_UNINITIALIZED)
    {
        /* Do init call */
        ret = init(param);

        /* Enter the once monitor again */
        ipcom_mutex_lock(ipcom_once_mutex);

        /* Update the state to done */
        once->state = IPCOM_ONCE_STATE_DONE;

        /* All pending processes are unlinked from the wait-queue by
           this call, so it is safe to set once->wq to IP_NULL */
        ipcom_wait_queue_wakeup_all_procs(once->wq);
        once->wq = IP_NULL;

        /* Exit the once monitor */
        ipcom_mutex_unlock(ipcom_once_mutex);
    }

    return ret;
}
/*
 *===========================================================================
 *                    ipcom_setenv
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
IP_PUBLIC int
ipcom_setenv(const char *name, const char *value, int rewrite)
{
#if IPCOM_USE_ENV == IPCOM_ENV_IPCOM
    Ip_err           retval;
    Ipcom_proc      *proc;
    Ipcom_env_entry *env;

    proc = ipcom_proc_self();
    ip_assert(proc != IP_NULL);

    retval = ipcom_once(&proc->env_once, ipcom_env_init, proc);
    if (retval != IPCOM_SUCCESS)
    {
        IP_PANIC();
        return -1;
    }

    ipcom_mutex_lock(proc->env_mutex);

    /* Check for duplicate. */
    env = ipcom_hash_get(proc->env_tree, name);
    if (env != IP_NULL)
    {
        if (rewrite == 0)
            goto leave;
        ipcom_env_delete(env, proc->env_tree);
    }

    /* Create environment variable. */
    env = ipcom_env_create(name, value);
    if (env == IP_NULL)
    {
        ipcom_mutex_unlock(proc->env_mutex);
        return -1;
    }
    (void)ipcom_hash_add(proc->env_tree, env);

 leave:
    ipcom_mutex_unlock(proc->env_mutex);
    return 0;

#elif IPCOM_USE_ENV == IPCOM_ENV_NATIVE && (defined(IP_PORT_OSE) || defined(IP_PORT_OSE5))
    (void)rewrite;

    set_env(current_process(), name, value);
    return 0;

#elif IPCOM_USE_ENV == IPCOM_ENV_NATIVE && defined(IP_PORT_VXWORKS)
    char buf[256];
    int  ret;

    (void)rewrite;

    ipcom_snprintf(buf, sizeof(buf), "%s=%s", name, value);
    ret = (int)putenv(buf);
    return ret == 0 ? 0 : -1;

#elif IPCOM_USE_ENV == IPCOM_ENV_NATIVE
    return setenv(name, value, rewrite);

#elif defined(IPCOM_USE_SYSVAR) && IPCOM_VR_MAX == 1
    (void)ipcom_sysvar_set(name, value, rewrite ? IPCOM_SYSVAR_FLAG_OVERWRITE : 0);
    return 0;

#else
    (void)name;
    (void)value;
    (void)rewrite;
    return -1;

#endif /* #if IPCOM_USE_ENV == IPCOM_ENV_IPCOM */
}