]> git.ipfire.org Git - pakfire.git/commitdiff
execute: Add data pointer to logging callback
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Mar 2021 16:28:18 +0000 (16:28 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Mar 2021 16:28:18 +0000 (16:28 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/libpakfire/execute.c
src/libpakfire/include/pakfire/execute.h
src/libpakfire/parser.c
src/libpakfire/step.c
tests/libpakfire/execute.c

index 39b040970b62c604f00220c26d78967e32b5a4f9..48ebedb8555ac775c3287af2f59d8a49ea1abea9 100644 (file)
@@ -371,7 +371,8 @@ static PyObject* Pakfire_make_path(PakfireObject* self, PyObject* args) {
 
 static PyObject* Pakfire_execute_logging_callback = NULL;
 
-static int __Pakfire_execute_logging_callback(Pakfire pakfire, int priority, const char* data) {
+static int __Pakfire_execute_logging_callback(Pakfire pakfire, void* data,
+               int priority, const char* line) {
        int r = 0;
 
        // Do nothing if callback isn't set
@@ -379,7 +380,7 @@ static int __Pakfire_execute_logging_callback(Pakfire pakfire, int priority, con
                return 0;
 
        // Create tuple with arguments for the callback function
-       PyObject* args = Py_BuildValue("(is)", priority, data);
+       PyObject* args = Py_BuildValue("(is)", priority, line);
        if (!args)
                return 1;
 
@@ -510,7 +511,7 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject*
 
        // Execute command
        int r = pakfire_execute(self->pakfire, argv, envp, flags,
-               (logging_callback) ? __Pakfire_execute_logging_callback : NULL);
+               (logging_callback) ? __Pakfire_execute_logging_callback : NULL, NULL);
 
        // Cleanup
        for (unsigned int i = 0; envp[i]; i++)
@@ -621,7 +622,7 @@ static PyObject* Pakfire_execute_script(PakfireObject* self, PyObject* args, PyO
 
        // Execute script
        int r = pakfire_execute_script(self->pakfire, script, strlen(script), envp, flags,
-               (logging_callback) ? __Pakfire_execute_logging_callback : NULL);
+               (logging_callback) ? __Pakfire_execute_logging_callback : NULL, NULL);
 
        // Cleanup
        for (unsigned int i = 0; envp[i]; i++)
index 0cf4f2ee2271d6abdd54d29c756b167d3165f3d5..e141cd440282f9642f8c74bc77efddd6936a265d 100644 (file)
@@ -67,7 +67,8 @@ static int pakfire_execute_buffer_is_full(const struct pakfire_execute_buffer* b
        If not newline character is found, it will try to read more data until it finds one.
 */
 static int pakfire_execute_logger_proxy(Pakfire pakfire, int fd,
-               pakfire_execute_logging_callback logging_callback, int priority, struct pakfire_execute_buffer* buffer) {
+               pakfire_execute_logging_callback logging_callback, void* data, int priority,
+               struct pakfire_execute_buffer* buffer) {
        char line[BUFFER_SIZE + 1];
 
        // Fill up buffer from fd
@@ -119,7 +120,7 @@ static int pakfire_execute_logger_proxy(Pakfire pakfire, int fd,
                line[length] = '\0';
 
                // Log the line
-               int r = logging_callback(pakfire, priority, line);
+               int r = logging_callback(pakfire, data, priority, line);
                if (r)
                        return r;
 
@@ -132,7 +133,7 @@ static int pakfire_execute_logger_proxy(Pakfire pakfire, int fd,
 }
 
 static int pakfire_execute_logger(Pakfire pakfire, pakfire_execute_logging_callback logging_callback,
-               pid_t pid, int stdout, int stderr, int* status) {
+               void* data, pid_t pid, int stdout, int stderr, int* status) {
        int epollfd = -1;
        struct epoll_event ev;
        struct epoll_event events[EPOLL_MAX_EVENTS];
@@ -214,7 +215,7 @@ static int pakfire_execute_logger(Pakfire pakfire, pakfire_execute_logging_callb
                        }
 
                        // Send everything to the logger
-                       r = pakfire_execute_logger_proxy(pakfire, fd, logging_callback, priority, buffer);
+                       r = pakfire_execute_logger_proxy(pakfire, fd, logging_callback, data, priority, buffer);
                        if (r)
                                goto OUT;
                }
@@ -227,7 +228,7 @@ OUT:
        return r;
 }
 
-static int default_logging_callback(Pakfire pakfire, int priority, const char* line) {
+static int default_logging_callback(Pakfire pakfire, void* data, int priority, const char* line) {
        switch (priority) {
                case LOG_INFO:
                        INFO(pakfire, "%s", line);
@@ -319,7 +320,7 @@ static int pakfire_execute_fork(void* data) {
 }
 
 PAKFIRE_EXPORT int pakfire_execute(Pakfire pakfire, const char* argv[], char* envp[],
-               int flags, pakfire_execute_logging_callback logging_callback) {
+               int flags, pakfire_execute_logging_callback logging_callback, void* data) {
        struct pakfire_execute env = {
                .pakfire = pakfire,
                .argv = argv,
@@ -390,7 +391,7 @@ PAKFIRE_EXPORT int pakfire_execute(Pakfire pakfire, const char* argv[], char* en
                if (env.stderr[1])
                        close(env.stderr[1]);
 
-               if (pakfire_execute_logger(pakfire, logging_callback, pid, env.stdout[0], env.stderr[0], &status)) {
+               if (pakfire_execute_logger(pakfire, logging_callback, data, pid, env.stdout[0], env.stderr[0], &status)) {
                        ERROR(pakfire, "Log reading aborted: %s\n", strerror(errno));
                }
        }
@@ -419,16 +420,16 @@ PAKFIRE_EXPORT int pakfire_execute(Pakfire pakfire, const char* argv[], char* en
 }
 
 PAKFIRE_EXPORT int pakfire_execute_command(Pakfire pakfire, const char* command, char* envp[],
-               int flags, pakfire_execute_logging_callback logging_callback) {
+               int flags, pakfire_execute_logging_callback logging_callback, void* data) {
        const char* argv[2] = {
                command, NULL,
        };
 
-       return pakfire_execute(pakfire, argv, envp, flags, logging_callback);
+       return pakfire_execute(pakfire, argv, envp, flags, logging_callback, data);
 }
 
-PAKFIRE_EXPORT int pakfire_execute_script(Pakfire pakfire, const char* data, const size_t size,
-               char* envp[], int flags, pakfire_execute_logging_callback logging_callback) {
+PAKFIRE_EXPORT int pakfire_execute_script(Pakfire pakfire, const char* script, const size_t size,
+               char* envp[], int flags, pakfire_execute_logging_callback logging_callback, void* data) {
        const char* root = pakfire_get_path(pakfire);
 
        // Write the scriptlet to disk
@@ -452,7 +453,7 @@ PAKFIRE_EXPORT int pakfire_execute_script(Pakfire pakfire, const char* data, con
        DEBUG(pakfire, "Writing script to %s\n", path);
 
        // Write data
-       ssize_t bytes_written = write(fd, data, size);
+       ssize_t bytes_written = write(fd, script, size);
        if (bytes_written < (ssize_t)size) {
                ERROR(pakfire, "Could not write script to file %s: %s\n",
                        path, strerror(errno));
@@ -486,7 +487,7 @@ PAKFIRE_EXPORT int pakfire_execute_script(Pakfire pakfire, const char* data, con
                command = pakfire_path_relpath(root, path);
 
        // Run the script
-       r = pakfire_execute_command(pakfire, command, envp, flags, logging_callback);
+       r = pakfire_execute_command(pakfire, command, envp, flags, logging_callback, data);
        if (r) {
                DEBUG(pakfire, "Script return code: %d\n", r);
        }
index e1d534c4111f26a710f4e0d3c75d264fdb05c31c..1c4b79c1cdf4da97f1c9df11f7b9eb62de9ef74d 100644 (file)
 
 #include <pakfire/types.h>
 
-typedef int (*pakfire_execute_logging_callback)(Pakfire pakfire, int priority, const char* data);
+typedef int (*pakfire_execute_logging_callback)(Pakfire pakfire, void* data,
+       int priority, const char* line);
 
 int pakfire_execute(Pakfire pakfire, const char* argv[], char* envp[],
-       int flags, pakfire_execute_logging_callback logging_callback);
+       int flags, pakfire_execute_logging_callback logging_callback, void* data);
 int pakfire_execute_command(Pakfire pakfire, const char* command, char* envp[],
-       int flags, pakfire_execute_logging_callback logging_callback);
-int pakfire_execute_script(Pakfire pakfire, const char* data, const size_t size,
-       char* envp[], int flags, pakfire_execute_logging_callback logging_callback);
+       int flags, pakfire_execute_logging_callback logging_callback, void* data);
+int pakfire_execute_script(Pakfire pakfire, const char* script, const size_t size,
+       char* envp[], int flags, pakfire_execute_logging_callback logging_callback, void* data);
 
 enum {
        PAKFIRE_EXECUTE_NONE                    = 0,
index 0c4e0bc762f98c980605c0ef01662c0855f509d2..01e699e3906972459fbcdaf06dac14f76fb984b9 100644 (file)
@@ -423,7 +423,7 @@ static int pakfire_parser_expand_commands(PakfireParser parser, char** buffer) {
                const char* output = "XXX";
 
                // Execute the command inside the Pakfire environment
-               r = pakfire_execute(parser->pakfire, argv, NULL, 0, NULL);
+               r = pakfire_execute(parser->pakfire, argv, NULL, 0, NULL, NULL);
                if (r) {
                        // Just log this and continue
                        DEBUG(parser->pakfire, "Command '%s' failed with return code %d\n", command, r);
index 4faaad9f143d97d5b39251692e293363309defa3..28b8224535cdf74f33c0fb7da5bb5e6a4b9d9140 100644 (file)
@@ -234,7 +234,7 @@ static int pakfire_script_check_shell(struct pakfire_scriptlet* scriptlet) {
 }
 
 static int pakfire_step_run_shell_script(PakfireStep step, struct pakfire_scriptlet* scriptlet) {
-       return pakfire_execute_script(step->pakfire, scriptlet->data, scriptlet->size, NULL, 0, NULL);
+       return pakfire_execute_script(step->pakfire, scriptlet->data, scriptlet->size, NULL, 0, NULL, NULL);
 }
 
 static int pakfire_step_run_script(PakfireStep step,
@@ -273,7 +273,7 @@ static int pakfire_run_ldconfig(PakfireStep step) {
        const char* path = pakfire_get_path(step->pakfire);
 
        if (pakfire_access(step->pakfire, path, LDCONFIG, X_OK) == 0) {
-               r = pakfire_execute_command(step->pakfire, LDCONFIG, NULL, 0, NULL);
+               r = pakfire_execute_command(step->pakfire, LDCONFIG, NULL, 0, NULL, NULL);
 
                DEBUG(step->pakfire, "ldconfig returned %d\n", r);
        }
index 330add11adc4d8ae48e95862545476f79599931e..2775983bd3cfb770ced3d6181085f3d88568ff4e 100644 (file)
@@ -30,7 +30,7 @@ static const char* cmd[2] = {
 };
 
 static int test_does_not_exist(const struct test* t) {
-       int r = pakfire_execute(t->pakfire, cmd, NULL, 0, NULL);
+       int r = pakfire_execute(t->pakfire, cmd, NULL, 0, NULL, NULL);
        ASSERT(r != 0);
 
        return EXIT_SUCCESS;