Coverage Report

Created: 2023-09-25 06:17

/src/znc/include/znc/ExecSock.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2004-2023 ZNC, see the NOTICE file for details.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#ifndef ZNC_EXECSOCK_H
18
#define ZNC_EXECSOCK_H
19
20
#include <znc/zncconfig.h>
21
#include <znc/Socket.h>
22
#include <signal.h>
23
24
//! @author imaginos@imaginos.net
25
class CExecSock : public CZNCSock {
26
  public:
27
0
    CExecSock() : CZNCSock(0), m_iPid(-1) {}
28
29
0
    int Execute(const CString& sExec) {
30
0
        int iReadFD, iWriteFD;
31
0
        m_iPid = popen2(iReadFD, iWriteFD, sExec);
32
0
        if (m_iPid != -1) {
33
0
            ConnectFD(iReadFD, iWriteFD, "0.0.0.0:0");
34
0
        }
35
0
        return (m_iPid);
36
0
    }
37
0
    void Kill(int iSignal) {
38
0
        kill(m_iPid, iSignal);
39
0
        Close();
40
0
    }
41
0
    virtual ~CExecSock() {
42
0
        close2(m_iPid, GetRSock(), GetWSock());
43
0
        SetRSock(-1);
44
0
        SetWSock(-1);
45
0
    }
46
47
    int popen2(int& iReadFD, int& iWriteFD, const CString& sCommand);
48
    void close2(int iPid, int iReadFD, int iWriteFD);
49
50
  private:
51
    int m_iPid;
52
};
53
54
#endif  // !ZNC_EXECSOCK_H