Пример #1
0
wcstring event_get_desc(const event_t *e)
{

    CHECK(e, 0);

    wcstring result;
    switch (e->type)
    {

        case EVENT_SIGNAL:
            result = format_string(_(L"signal handler for %ls (%ls)"), sig2wcs(e->param1.signal), signal_get_desc(e->param1.signal));
            break;

        case EVENT_VARIABLE:
            result = format_string(_(L"handler for variable '%ls'"), e->str_param1.c_str());
            break;

        case EVENT_EXIT:
            if (e->param1.pid > 0)
            {
                result = format_string(_(L"exit handler for process %d"), e->param1.pid);
            }
            else
            {
                job_t *j = job_get_from_pid(-e->param1.pid);
                if (j)
                    result = format_string(_(L"exit handler for job %d, '%ls'"), j->job_id, j->command_wcstr());
                else
                    result = format_string(_(L"exit handler for job with process group %d"), -e->param1.pid);
            }

            break;

        case EVENT_JOB_ID:
        {
            job_t *j = job_get(e->param1.job_id);
            if (j)
                result = format_string(_(L"exit handler for job %d, '%ls'"), j->job_id, j->command_wcstr());
            else
                result = format_string(_(L"exit handler for job with job id %d"), e->param1.job_id);

            break;
        }

        case EVENT_GENERIC:
            result = format_string(_(L"handler for generic event '%ls'"), e->str_param1.c_str());
            break;

        default:
            result = format_string(_(L"Unknown event type"));
            break;

    }

    return result;
}
Пример #2
0
/* Get a job using its ID and acquire its AioContext */
static Job *find_job(const char *id, AioContext **aio_context, Error **errp)
{
    Job *job;

    *aio_context = NULL;

    job = job_get(id);
    if (!job) {
        error_setg(errp, "Job not found");
        return NULL;
    }

    *aio_context = job->aio_context;
    aio_context_acquire(*aio_context);

    return job;
}
Пример #3
0
void*
worker_thread_routine(void)
{
    int fd;
    job *job_to_do;

    while(1)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        job_to_do = job_get();
        fd = job_to_do->fd;
        free(job_to_do);
        client_process(fd);
        pthread_mutex_unlock(&mutex);

        printf("thd finished\n");
    }
    return NULL;
}
Пример #4
0
/*
    Give a more condensed description of \c event compared to \c event_get_desc.
    It includes what function will fire if the \c event is an event handler.
 */
static wcstring event_desc_compact(const event_t &event)
{
    wcstring res;
    wchar_t const *temp;
    int sig;
    switch (event.type)
    {
        case EVENT_ANY:
            res = L"EVENT_ANY";
            break;
        case EVENT_VARIABLE:
            if (event.str_param1.c_str())
            {
                res = format_string(L"EVENT_VARIABLE($%ls)", event.str_param1.c_str());
            }
            else
            {
                res = L"EVENT_VARIABLE([any])";
            }
            break;
        case EVENT_SIGNAL:
            sig = event.param1.signal;
            if (sig == EVENT_ANY_SIGNAL)
            {
                temp = L"[all signals]";
            }
            else if (sig == 0)
            {
                temp = L"not set";
            }
            else
            {
                temp = sig2wcs(sig);
            }
            res = format_string(L"EVENT_SIGNAL(%ls)", temp);
            break;
        case EVENT_EXIT:
            if (event.param1.pid == EVENT_ANY_PID)
            {
                res = wcstring(L"EVENT_EXIT([all child processes])");
            }
            else if (event.param1.pid > 0)
            {
                res = format_string(L"EVENT_EXIT(pid %d)", event.param1.pid);
            }
            else
            {
                job_t *j = job_get_from_pid(-event.param1.pid);
                if (j)
                    res = format_string(L"EVENT_EXIT(jobid %d: \"%ls\")", j->job_id, j->command_wcstr());
                else
                    res = format_string(L"EVENT_EXIT(pgid %d)", -event.param1.pid);
            }
            break;
        case EVENT_JOB_ID:
        {
            job_t *j = job_get(event.param1.job_id);
            if (j)
                res = format_string(L"EVENT_JOB_ID(job %d: \"%ls\")", j->job_id, j->command_wcstr());
            else
                res = format_string(L"EVENT_JOB_ID(jobid %d)", event.param1.job_id);
            break;
        }
        case EVENT_GENERIC:
            res = format_string(L"EVENT_GENERIC(%ls)", event.str_param1.c_str());
            break;
        default:
            res = format_string(L"unknown/illegal event(%x)", event.type);
    }
    if (event.function_name.size())
    {
        return format_string(L"%ls: \"%ls\"", res.c_str(), event.function_name.c_str());
    }
    else
    {
        return res;
    }
}
Пример #5
0
static int find_process(const wchar_t *proc,
                        expand_flags_t flags,
                        std::vector<completion_t> &out)
{
    int found = 0;

    if (!(flags & EXPAND_SKIP_JOBS))
    {
        ASSERT_IS_MAIN_THREAD();
        const job_t *j;

        if (iswnumeric(proc) || (wcslen(proc)==0))
        {
            /*
              This is a numeric job string, like '%2'
            */

            if (flags & ACCEPT_INCOMPLETE)
            {
                job_iterator_t jobs;
                while ((j = jobs.next()))
                {
                    wchar_t jid[16];
                    if (j->command_is_empty())
                        continue;

                    swprintf(jid, 16, L"%d", j->job_id);

                    if (wcsncmp(proc, jid, wcslen(proc))==0)
                    {
                        wcstring desc_buff = format_string(COMPLETE_JOB_DESC_VAL, j->command_wcstr());
                        append_completion(out,
                                          jid+wcslen(proc),
                                          desc_buff,
                                          0);
                    }
                }

            }
            else
            {

                int jid;
                wchar_t *end;

                errno = 0;
                jid = fish_wcstoi(proc, &end, 10);
                if (jid > 0 && !errno && !*end)
                {
                    j = job_get(jid);
                    if ((j != 0) && (j->command_wcstr() != 0))
                    {
                        {
                            append_completion(out, to_string<long>(j->pgid));
                            found = 1;
                        }
                    }
                }
            }
        }
        if (found)
            return 1;

        job_iterator_t jobs;
        while ((j = jobs.next()))
        {

            if (j->command_is_empty())
                continue;

            size_t offset;
            if (match_pid(j->command(), proc, flags, &offset))
            {
                if (flags & ACCEPT_INCOMPLETE)
                {
                    append_completion(out,
                                      j->command_wcstr() + offset + wcslen(proc),
                                      COMPLETE_JOB_DESC,
                                      0);
                }
                else
                {
                    append_completion(out, to_string<long>(j->pgid));
                    found = 1;
                }
            }
        }

        if (found)
        {
            return 1;
        }

        jobs.reset();
        while ((j = jobs.next()))
        {
            process_t *p;
            if (j->command_is_empty())
                continue;
            for (p=j->first_process; p; p=p->next)
            {
                if (p->actual_cmd.empty())
                    continue;

                size_t offset;
                if (match_pid(p->actual_cmd, proc, flags, &offset))
                {
                    if (flags & ACCEPT_INCOMPLETE)
                    {
                        append_completion(out,
                                          wcstring(p->actual_cmd, offset + wcslen(proc)),
                                          COMPLETE_CHILD_PROCESS_DESC,
                                          0);
                    }
                    else
                    {
                        append_completion(out,
                                          to_string<long>(p->pid),
                                          L"",
                                          0);
                        found = 1;
                    }
                }
            }
        }

        if (found)
        {
            return 1;
        }
    }

    /* Iterate over all processes */
    wcstring process_name;
    pid_t process_pid;
    process_iterator_t iterator;
    while (iterator.next_process(&process_name, &process_pid))
    {
        size_t offset;
        if (match_pid(process_name, proc, flags, &offset))
        {
            if (flags & ACCEPT_INCOMPLETE)
            {
                append_completion(out,
                                  process_name.c_str() + offset + wcslen(proc),
                                  COMPLETE_PROCESS_DESC,
                                  0);
            }
            else
            {
                append_completion(out, to_string<long>(process_pid));
            }
        }
    }

    return 1;
}