social.dark-alexandr.net

sss friendica
есть у кого нибудь код запускающий процесс с io пайпасм для stdout/stderr/stdin с использованием например posix_spawn, или чего то другого стандартного из posix или на крайняк gnu extensions (лучше конечно posix), нужно запускать процесс и получать его вывод на системе без /bin/sh (в системе вобще нет какого либо sh, это embedded)

нужно сделать аналог такого кода

static int run_cmd_ret(const char* cmd, char** out)
{
    FILE [i]f = popen(cmd, "r");
    int is_debug = 0;
    char buf[block_size], *out_buf;
    size_t out_size = 0;
#ifdef DEBUG
    is_debug = 1;
    printf("executing: \"%s\"\n", cmd);
#endif
    if (!f)
    {
        dbg_print("popen failed\n");
        return -1;
    }
    if (out || is_debug)
        out_buf = (char[/i])malloc(block_size);
    while (!feof(f) && !ferror(f))
    {
        int len = fread(buf, 1, block_size, f);
        if (len && (out || is_debug))
        {
            out_buf = realloc(out_buf, out_size + len + 1);
            if (!out_buf)
            {
                dbg_print("realloc failed\n");
                return -1;
            }
            memcpy(out_buf + out_size, buf, len);
            out_size += len;
        }
    }
    if (out || is_debug)
        out_buf[out_size] = 0;
    if (out)
        *out = out_buf;
    if (is_debug)
    {
        dbg_print(out_buf);
        if (out_buf[out_size - 1] != '\n')
            dbg_print("\n");
        if (!out)
            free(out_buf);
    }
    return 0;
}

static int run_cmd(const char* cmd)
{
    return run_cmd_ret(cmd, 0);
}

но popen требует наличия /bin/sh в системе.
sss hubzilla (AP)
https://unix.stackexchange.com/questions/252901/get-output-of-posix-spawn - надо попробовать.
<br />#include &lt;stdio.h&gt;<br />#include &lt;stdlib.h&gt;<br />#include &lt;sys/stat.h&gt;<br />#include &lt;fcntl.h&gt;<br />#include &lt;spawn.h&gt;<br /><br />int main(int argc, char* argv[], char *env[])<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;int ret;<br />&nbsp;&nbsp;&nbsp;&nbsp;pid_t child_pid;<br />&nbsp;&nbsp;&nbsp;&nbsp;posix_spawn_file_actions_t child_fd_actions;<br />&nbsp;&nbsp;&nbsp;&nbsp;if (ret = posix_spawn_file_actions_init (&child_fd_actions))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror (&quot;posix_spawn_file_actions_init&quot;), exit(ret);<br />&nbsp;&nbsp;&nbsp;&nbsp;if (ret = posix_spawn_file_actions_addopen (&amp;child_fd_actions, 1, &quot;/tmp/foo-log&quot;, <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;O_WRONLY | O_CREAT | O_TRUNC, 0644))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror (&quot;posix_spawn_file_actions_addopen&quot;), exit(ret);<br />&nbsp;&nbsp;&nbsp;&nbsp;if (ret = posix_spawn_file_actions_adddup2 (&amp;child_fd_actions, 1, 2))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror (&quot;posix_spawn_file_actions_adddup2&quot;), exit(ret);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if (ret = posix_spawnp (&amp;child_pid, &quot;date&quot;, &amp;child_fd_actions, NULL, argv, env))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror (&quot;posix_spawn&quot;), exit(ret);<br />}<br />


What does it do?

The third parameter of posix_spwan is a pointer of type posix_spawn_file_actions_t (one you have given as NULL). posix_spawn will open, close or duplicate file descriptors inherited from the calling process as specified by the posix_spawn_file_actions_t object.
So we start with a posix_spawn_file_actions_t object (chiild_fd_actions), and initialize it with posix_spawn_file_actions_init().
Now, the posix_spawn_file_actions_{addopen,addclose,addup2} functions can be used to open, close or duplicate file descriptors (after the open(3), close(3) and dup2(3) functions) respectively.
So we posix_spawn_file_actions_addopen a file at /tmp/foo-log to file descriptor 1 (aka stdout).
Then we posix_spawn_file_actions_adddup2 fd 2 (aka stderr) to fd 1.
Note that nothing has been opened or duped yet. The last two functions simply changed the child_fd_actions object to note that these actions are to be taken.
And finally we use posix_spawn with the child_fd_actions object.
Iron Bug friendica (via ActivityPub)
чота код распидорасило от HTML'а.
sss friendica
@Iron Bug @sss это авторепост с хабзилы, что то они как то между собой не дружат, разбираться лень, и так работы много.
оригинал тут https://hubzilla.dark-alexandr.net/display/b64.aHR0cHM6Ly9odWJ6aWxsYS5kYXJrLWFsZXhhbmRyLm5ldC9pdGVtLzA3Yjg1Y2NjLWRmNTQtNDEwOC1iM2U3LTExYzhjYjNhYzA4MA
Iron Bug friendica (via ActivityPub)
ну, там его немного меньше распидорасило 😀 хотя выравнивание съехало и всё в кучу. но в перепосте выглядит страшнее.
sss hubzilla (AP)
хрен с ним