File: popen_plus.cpp

package info (click to toggle)
cg3 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,684 kB
  • sloc: cpp: 26,476; xml: 6,139; perl: 1,398; lisp: 1,091; ansic: 178; sh: 47; python: 26; makefile: 14
file content (190 lines) | stat: -rw-r--r-- 3,991 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 ** Author: Hamid Alipour http://codingrecipes.com http://twitter.com/code_head
 ** SQLite style license:
 **
 ** 2001 September 15
 **
 ** The author disclaims copyright to this source code.  In place of
 ** a legal notice, here is a blessing:
 **
 **    May you do good and not evil.
 **    May you find forgiveness for yourself and forgive others.
 **    May you share freely, never taking more than you give.
 **/

#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <paths.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include "popen_plus.hpp"

namespace CG3_PopenPlus {

popen_plus_process *popen_plus(const char *command)
{
    int inpipe[2];
    int outpipe[2];
    char *argv[4];
    popen_plus_process *process = (popen_plus_process*)malloc(sizeof(popen_plus_process));

    if (!process)
        goto error_out;

    if (pipe(inpipe) != 0)
        goto clean_process_out;

    if (pipe(outpipe) != 0)
        goto clean_inpipe_out;

    process->read_fp = fdopen(outpipe[READ], "rb");
    if (!process->read_fp)
        goto clean_outpipe_out;

    process->write_fp = fdopen(inpipe[WRITE], "wb");
    if (!process->write_fp)
        goto clean_read_fp_out;

    if (pthread_mutex_init(&process->mutex, NULL) != 0)
        goto clean_write_fp_out;

    process->pid = fork();
    if (process->pid == -1)
        goto clean_mutex_out;

    if (process->pid == 0) {
        close(outpipe[READ]);
        close(inpipe[WRITE]);

        if (inpipe[READ] != STDIN_FILENO) {
            dup2(inpipe[READ], STDIN_FILENO);
            close(inpipe[READ]);
        }

        if (outpipe[WRITE] != STDOUT_FILENO) {
            dup2(outpipe[WRITE], STDOUT_FILENO);
            close(outpipe[WRITE]);
        }

        char _sh[] = "sh";
        char _c[] = "-c";

        argv[0] = _sh;
        argv[1] = _c;
        argv[2] = (char *) command;
        argv[3] = NULL;

        execv(_PATH_BSHELL, argv);
        exit(127);
    }

    close(outpipe[WRITE]);
    close(inpipe[READ]);

    return process;

clean_mutex_out:
    pthread_mutex_destroy(&process->mutex);

clean_write_fp_out:
    fclose(process->write_fp);

clean_read_fp_out:
    fclose(process->read_fp);

clean_outpipe_out:
    close(outpipe[READ]);
    close(outpipe[WRITE]);

clean_inpipe_out:
    close(inpipe[READ]);
    close(inpipe[WRITE]);

clean_process_out:
    free(process);

error_out:
    return NULL;
}

int popen_plus_close(popen_plus_process *process)
{
    int pstat = 0;
    pid_t pid = 0;

    /**
     * If someone else destrys this mutex, then this call will fail and we know
     * that another thread already cleaned up the process so we can safely return
     * and since we are destroying this mutex bellow then we don't need to unlock
     * it...
     */
    if (pthread_mutex_lock(&process->mutex) != 0)
        return 0;

    if (process->pid != -1) {
        do {
            pid = waitpid(process->pid, &pstat, 0);
        } while (pid == -1 && errno == EINTR);
    }

    if (process->read_fp)
        fclose(process->read_fp);

    if (process->write_fp)
        fclose(process->write_fp);

    pthread_mutex_destroy(&process->mutex);

    free(process);

    return (pid == -1 ? -1 : pstat);
}

int popen_plus_kill(popen_plus_process *process)
{
    char command[64];

    sprintf(command, "kill -9 %d", process->pid);
    system(command);

    return 0;
}

int popen_plus_kill_by_id(int process_id)
{
    char command[64];

    sprintf(command, "kill -9 %d", process_id);
    system(command);

    return 0;
}

int popen_plus_terminate(popen_plus_process *process)
{
    char command[64];

    sprintf(command, "kill -TERM %d", process->pid);
    system(command);

    return 0;
}

int popen_plus_terminate_with_id(int process_id)
{
    char command[64];

    sprintf(command, "kill -TERM %d", process_id);
    system(command);

    return 0;
}

}