summaryrefslogtreecommitdiffstats
path: root/xlators/bindings/python/src/python.c
blob: 739ef7329008e0e704306fab4f8edd80dbf73916 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
   Copyright (c) 2007 Chris AtLee <chris@atlee.ca>
   This file is part of GlusterFS.

   GlusterFS is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation; either version 3 of the License,
   or (at your option) any later version.

   GlusterFS is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.
*/

#include <Python.h>

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include "glusterfs.h"
#include "xlator.h"
#include "logging.h"
#include "defaults.h"

typedef struct
{
    char        *scriptname;
    PyObject    *pXlator;
    PyObject    *pScriptModule;
    PyObject    *pGlusterModule;
    PyThreadState *pInterp;

    PyObject    *pFrameType, *pVectorType, *pFdType;
} python_private_t;

int32_t
python_writev (call_frame_t *frame,
              xlator_t *this,
              fd_t *fd,
              struct iovec *vector,
              int32_t count, 
              off_t offset)
{
  python_private_t *priv = (python_private_t *)this->private;
  gf_log("python", GF_LOG_DEBUG, "In writev");
  if (PyObject_HasAttrString(priv->pXlator, "writev"))
  {

      PyObject *retval = PyObject_CallMethod(priv->pXlator, "writev",
              "O O O i l",
              PyObject_CallMethod(priv->pFrameType, "from_address", "O&", PyLong_FromVoidPtr, frame),
              PyObject_CallMethod(priv->pFdType, "from_address", "O&", PyLong_FromVoidPtr, fd),
              PyObject_CallMethod(priv->pVectorType, "from_address", "O&", PyLong_FromVoidPtr, vector),
              count,
              offset);
      if (PyErr_Occurred())
      {
          PyErr_Print();
      }
      Py_XDECREF(retval);
  }
  else
  {
      return default_writev(frame, this, fd, vector, count, offset);
  }
  return 0;
}

struct xlator_fops fops = {
    .writev       = python_writev
};

struct xlator_mops mops = {
};

static PyObject *
AnonModule_FromFile (const char* fname)
{
    // Get the builtins
    PyThreadState* pThread = PyThreadState_Get();
    PyObject *pBuiltins = pThread->interp->builtins;

    if (PyErr_Occurred())
    {
        PyErr_Print();
        return NULL;
    }

    // Create a new dictionary for running code in
    PyObject *pModuleDict = PyDict_New();
    PyDict_SetItemString(pModuleDict, "__builtins__", pBuiltins);
    Py_INCREF(pBuiltins);

    // Run the file in the new context
    FILE* fp = fopen(fname, "r");
    PyRun_File(fp, fname, Py_file_input, pModuleDict, pModuleDict);
    fclose(fp);
    if (PyErr_Occurred())
    {
        PyErr_Print();
        Py_DECREF(pModuleDict);
        Py_DECREF(pBuiltins);
        return NULL;
    }

    // Create an object to hold the new context
    PyRun_String("class ModuleWrapper(object):\n\tpass\n", Py_single_input, pModuleDict, pModuleDict);
    if (PyErr_Occurred())
    {
        PyErr_Print();
        Py_DECREF(pModuleDict);
        Py_DECREF(pBuiltins);
        return NULL;
    }
    PyObject *pModule = PyRun_String("ModuleWrapper()", Py_eval_input, pModuleDict, pModuleDict);
    if (PyErr_Occurred())
    {
        PyErr_Print();
        Py_DECREF(pModuleDict);
        Py_DECREF(pBuiltins);
        Py_XDECREF(pModule);
        return NULL;
    }

    // Set the new context's dictionary to the one we used to run the code
    // inside
    PyObject_SetAttrString(pModule, "__dict__", pModuleDict);
    if (PyErr_Occurred())
    {
        PyErr_Print();
        Py_DECREF(pModuleDict);
        Py_DECREF(pBuiltins);
        Py_DECREF(pModule);
        return NULL;
    }

    return pModule;
}

int32_t
init (xlator_t *this)
{
  // This is ok to call more than once per process
  Py_InitializeEx(0);

  if (!this->children) {
    gf_log ("python", GF_LOG_ERROR, 
            "FATAL: python should have exactly one child");
    return -1;
  }

  python_private_t *priv = CALLOC (sizeof (python_private_t), 1);
  ERR_ABORT (priv);

  data_t *scriptname = dict_get (this->options, "scriptname");
  if (scriptname) {
      priv->scriptname = data_to_str(scriptname);
  } else {
      gf_log("python", GF_LOG_ERROR,
              "FATAL: python requires the scriptname parameter");
      return -1;
  }

  priv->pInterp = Py_NewInterpreter();
    
  // Adjust python's path
  PyObject *syspath = PySys_GetObject("path");
  PyObject *path = PyString_FromString(GLUSTER_PYTHON_PATH);
  PyList_Append(syspath, path);
  Py_DECREF(path);

  gf_log("python", GF_LOG_DEBUG,
          "Loading gluster module");

  priv->pGlusterModule = PyImport_ImportModule("gluster");
  if (PyErr_Occurred())
  {
      PyErr_Print();
      return -1;
  }

  priv->pFrameType = PyObject_GetAttrString(priv->pGlusterModule, "call_frame_t");
  priv->pFdType = PyObject_GetAttrString(priv->pGlusterModule, "fd_t");
  priv->pVectorType = PyObject_GetAttrString(priv->pGlusterModule, "iovec");

  gf_log("python", GF_LOG_DEBUG, "Loading script...%s", priv->scriptname);
  
  priv->pScriptModule = AnonModule_FromFile(priv->scriptname);
  if (!priv->pScriptModule || PyErr_Occurred())
  {
      gf_log("python", GF_LOG_ERROR, "Error loading %s", priv->scriptname);
      PyErr_Print();
      return -1;
  }

  if (!PyObject_HasAttrString(priv->pScriptModule, "xlator"))
  {
      gf_log("python", GF_LOG_ERROR, "%s does not have a xlator attribute", priv->scriptname);
      return -1;
  }
  gf_log("python", GF_LOG_DEBUG, "Instantiating translator");
  priv->pXlator = PyObject_CallMethod(priv->pScriptModule, "xlator", "O&",
          PyLong_FromVoidPtr, this);
  if (PyErr_Occurred() || !priv->pXlator)
  {
      PyErr_Print();
      return -1;
  }

  this->private = priv;

  gf_log ("python", GF_LOG_DEBUG, "python xlator loaded");
  return 0;
}

void 
fini (xlator_t *this)
{
  python_private_t *priv = (python_private_t*)(this->private);
  Py_DECREF(priv->pXlator);
  Py_DECREF(priv->pScriptModule);
  Py_DECREF(priv->pGlusterModule);
  Py_DECREF(priv->pFrameType);
  Py_DECREF(priv->pFdType);
  Py_DECREF(priv->pVectorType);
  Py_EndInterpreter(priv->pInterp);
  return;
}