Debian Codegraph @

src/share.c

raw
1 #include "pycurl.h"
2 #include "docstrings.h"
3
4 # define EASY_WEAKREFS_LOCK(share) PyThread_acquire_lock((share)->easy_weakrefs_lock, 1)
5 # define EASY_WEAKREFS_UNLOCK(share) PyThread_release_lock((share)->easy_weakrefs_lock)
6 /*************************************************************************
7 // static utility functions
8 **************************************************************************/
9
10 PYCURL_INTERNAL int
11 share_register_easy(CurlShareObject *share, CurlObject *easy)
12 {
13 PyObject *wr;
14
15 assert(share != NULL);
16 assert(easy != NULL);
17
18 wr = PyWeakref_NewRef((PyObject *)easy, NULL);
19 if (wr == NULL) {
20 return -1;
21 }
22
23 EASY_WEAKREFS_LOCK(share);
24
25 if (share->share_handle == NULL || share->easy_weakrefs == NULL) {
26 PyErr_SetString(PyExc_RuntimeError, "CurlShare is closed");
27 goto error;
28 }
29
30 if (PySet_Add(share->easy_weakrefs, wr) < 0) {
31 goto error;
32 }
33
34 EASY_WEAKREFS_UNLOCK(share);
35 Py_DECREF(wr);
36 return 0;
37
38 error:
39 EASY_WEAKREFS_UNLOCK(share);
40 Py_DECREF(wr);
41 return -1;
42 }
43
44
45 PYCURL_INTERNAL void
46 share_unregister_easy(CurlShareObject *share, CurlObject *easy)
47 {
48 PyObject *wr;
49
50 assert(share != NULL);
51 assert(easy != NULL);
52
53 wr = PyWeakref_NewRef((PyObject *)easy, NULL);
54 if (wr == NULL) {
55 PyErr_Clear();
56 return;
57 }
58
59 EASY_WEAKREFS_LOCK(share);
60
61 if (share->easy_weakrefs == NULL) {
62 EASY_WEAKREFS_UNLOCK(share);
63 Py_DECREF(wr);
64 return;
65 }
66
67 if (PySet_Discard(share->easy_weakrefs, wr) < 0) {
68 PyErr_Clear();
69 }
70
71 EASY_WEAKREFS_UNLOCK(share);
72 Py_DECREF(wr);
73 }
74
75
76 /* assert some CurlShareObject invariants */
77 static void
78 assert_share_state(const CurlShareObject *self)
79 {
80 assert(self != NULL);
81 assert(PyObject_IsInstance((PyObject *) self, (PyObject *) p_CurlShare_Type) == 1);
82 assert(self->lock != NULL);
83 }
84
85
86 static int
87 check_share_state(const CurlShareObject *self, int flags, const char *name)
88 {
89 assert_share_state(self);
90 return 0;
91 }
92
93
94 /* constructor */
95 PYCURL_INTERNAL CurlShareObject *
96 do_share_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
97 {
98 int res;
99 CurlShareObject *self;
100 const curl_lock_function lock_cb = share_lock_callback;
101 const curl_unlock_function unlock_cb = share_unlock_callback;
102 int *ptr;
103 static char *kwlist[] = {"detach_on_close", NULL};
104 int detach_on_close = 1;
105 if (subtype == p_CurlShare_Type && !PyArg_ParseTupleAndKeywords(args, kwds, "|$p", kwlist, &detach_on_close)) {
106 return NULL;
107 }
108
109 /* Allocate python curl-share object */
110 self = (CurlShareObject *) subtype->tp_alloc(subtype, 0);
111 if (!self) {
112 return NULL;
113 }
114
115 /* tp_alloc is expected to return zeroed memory */
116 for (ptr = (int *) &self->dict;
117 ptr < (int *) (((char *) self) + sizeof(CurlShareObject));
118 ++ptr) {
119 assert(*ptr == 0);
120 }
121
122 self->lock = share_lock_new();
123 if (self->lock == NULL) {
124 Py_DECREF(self);
125 return NULL;
126 }
127 self->easy_weakrefs_lock = PyThread_allocate_lock();
128 if (self->easy_weakrefs_lock == NULL) {
129 Py_DECREF(self);
130 PyErr_NoMemory();
131 return NULL;
132 }
133
134 self->easy_weakrefs = PySet_New(NULL);
135 if (self->easy_weakrefs == NULL) {
136 PyThread_free_lock(self->easy_weakrefs_lock);
137 self->easy_weakrefs_lock = NULL;
138 Py_DECREF(self);
139 return NULL;
140 }
141
142 /* Allocate libcurl share handle */
143 self->share_handle = curl_share_init();
144 if (self->share_handle == NULL) {
145 Py_DECREF(self);
146 PyErr_SetString(ErrorObject, "initializing curl-share failed");
147 return NULL;
148 }
149
150 /* Set locking functions and data */
151 res = curl_share_setopt(self->share_handle, CURLSHOPT_LOCKFUNC, lock_cb);
152 assert(res == CURLE_OK);
153 res = curl_share_setopt(self->share_handle, CURLSHOPT_USERDATA, self);
154 assert(res == CURLE_OK);
155 res = curl_share_setopt(self->share_handle, CURLSHOPT_UNLOCKFUNC, unlock_cb);
156 assert(res == CURLE_OK);
157
158 self->detach_on_close = detach_on_close ? 1 : 0;
159 return self;
160 }
161
162
163 PYCURL_INTERNAL int
164 do_share_traverse(CurlShareObject *self, visitproc visit, void *arg)
165 {
166 int err;
167 #undef VISIT
168 #define VISIT(v) if ((v) != NULL && ((err = visit(v, arg)) != 0)) return err
169
170 VISIT(self->dict);
171 VISIT(self->easy_weakrefs);
172
173 return 0;
174 #undef VISIT
175 }
176
177 static void
178 util_share_xdecref(CurlShareObject *self)
179 {
180 Py_CLEAR(self->dict);
181 /*
182 * The lock can be NULL for partially-initialized objects or during
183 * early destruction paths; guard against acquiring an uninitialized lock.
184 */
185 if (self->easy_weakrefs_lock) EASY_WEAKREFS_LOCK(self);
186 Py_CLEAR(self->easy_weakrefs);
187 if (self->easy_weakrefs_lock) EASY_WEAKREFS_UNLOCK(self);
188 }
189
190
191 /* Drop references that may have created reference cycles. */
192 PYCURL_INTERNAL int
193 do_share_clear(CurlShareObject *self)
194 {
195 util_share_xdecref(self);
196 return 0;
197 }
198
199
200 static void
201 util_share_close(CurlShareObject *self){
202 if (self->share_handle != NULL) {
203 CURLSH *share_handle = self->share_handle;
204 self->share_handle = NULL;
205 curl_share_cleanup(share_handle);
206 }
207 }
208
209
210 PYCURL_INTERNAL void
211 do_share_dealloc(CurlShareObject *self)
212 {
213 PyObject_GC_UnTrack(self);
214 Py_TRASHCAN_BEGIN(self, do_share_dealloc);
215
216 util_share_xdecref(self);
217 util_share_close(self);
218
219 if (self->lock) {
220 share_lock_destroy(self->lock);
221 }
222 if (self->easy_weakrefs_lock) {
223 PyThread_free_lock(self->easy_weakrefs_lock);
224 self->easy_weakrefs_lock = NULL;
225 }
226
227 if (self->weakreflist != NULL) {
228 PyObject_ClearWeakRefs((PyObject *) self);
229 }
230
231 CurlShare_Type.tp_free(self);
232 Py_TRASHCAN_END
233 }
234
235 static int
236 share_cleanup_and_count_live_easies(CurlShareObject *self)
237 {
238 int has_live = 0;
239
240 EASY_WEAKREFS_LOCK(self);
241
242 if (self->easy_weakrefs && PySet_Check(self->easy_weakrefs)) {
243 PyObject *it = PyObject_GetIter(self->easy_weakrefs);
244 PyObject *to_remove = PyList_New(0);
245
246 if (it && to_remove) {
247 PyObject *wr;
248 PyObject *obj = NULL;
249
250 while ((wr = PyIter_Next(it))) {
251 #if PY_VERSION_HEX >= 0x030D0000 /* Python 3.13+ */
252 int rc = PyWeakref_GetRef(wr, &obj);
253 // return -1 on error, 0 on dead object
254 // in either case, mark as removable and
255 // move to the next reference
256 if (rc < 0) {
257 Py_DECREF(wr);
258 Py_DECREF(it);
259 Py_DECREF(to_remove);
260 EASY_WEAKREFS_UNLOCK(self);
261 return -1;
262 } else if (rc == 0 || obj == NULL) {
263 PyList_Append(to_remove, wr);
264 Py_DECREF(wr);
265 continue;
266 }
267
268 #else
269 // will borrowed reference, None if object dead
270 obj = PyWeakref_GetObject(wr);
271 if (obj != Py_None) {
272 // If not None, real object, INCREF and carry on
273 Py_INCREF(obj);
274 } else {
275 // otherwise mark for removal and move to the next ref
276 PyList_Append(to_remove, wr);
277 Py_DECREF(wr);
278 continue;
279 }
280 #endif
281 CurlObject *easy = (CurlObject *)obj;
282
283 if (easy && easy->share == self) {
284 if (self->detach_on_close) {
285 curl_easy_setopt(easy->handle, CURLOPT_SHARE, NULL);
286 easy->share = NULL;
287
288 PyList_Append(to_remove, wr);
289 } else {
290 has_live += 1;
291 }
292 } else {
293 PyList_Append(to_remove, wr);
294 }
295
296 Py_DECREF(wr);
297 Py_DECREF(obj);
298 }
299
300 Py_ssize_t i, n = PyList_GET_SIZE(to_remove);
301 for (i = 0; i < n; i++) {
302 PyObject *wr2 = PyList_GET_ITEM(to_remove, i);
303 PySet_Discard(self->easy_weakrefs, wr2);
304 }
305
306 Py_DECREF(it);
307 Py_DECREF(to_remove);
308
309 if (PyErr_Occurred()) {
310 PyErr_Clear();
311 }
312 } else {
313 Py_XDECREF(it);
314 Py_XDECREF(to_remove);
315 PyErr_Clear();
316 }
317 }
318
319 EASY_WEAKREFS_UNLOCK(self);
320 return has_live;
321 }
322
323
324 static PyObject *
325 do_share_close(CurlShareObject *self, PyObject *Py_UNUSED(ignored))
326 {
327 int nlive;
328
329 if (check_share_state(self, 2, "close") != 0) {
330 return NULL;
331 }
332
333 nlive = share_cleanup_and_count_live_easies(self);
334 if (nlive > 0) {
335 PyErr_Format(
336 ErrorObject,
337 "cannot close CurlShare: still in use by %d active Curl object%s",
338 nlive,
339 (nlive == 1 ? "" : "s")
340 );
341 return NULL;
342 } else if (nlive < 0) {
343 // Error in share_cleanup_and_count_live_easies, propagate up
344 return NULL;
345 }
346
347 util_share_close(self);
348 Py_RETURN_NONE;
349 }
350
351
352 static PyObject *do_share_closed(CurlShareObject *self, PyObject *Py_UNUSED(ignored))
353 {
354 if (self->share_handle == NULL) {
355 Py_RETURN_TRUE;
356 } else {
357 Py_RETURN_FALSE;
358 }
359 }
360
361
362 /* setopt, unsetopt*/
363 /* --------------- unsetopt/setopt/getinfo --------------- */
364
365 static PyObject *
366 do_share_setopt(CurlShareObject *self, PyObject *args)
367 {
368 int option;
369 PyObject *obj;
370
371 if (!PyArg_ParseTuple(args, "iO:setopt", &option, &obj))
372 return NULL;
373 if (check_share_state(self, 1 | 2, "sharesetopt") != 0)
374 return NULL;
375
376 /* early checks of option value */
377 if (option <= 0)
378 goto error;
379 if (option >= (int)CURLOPTTYPE_OFF_T + OPTIONS_SIZE)
380 goto error;
381 if (option % 10000 >= OPTIONS_SIZE)
382 goto error;
383
384 #if 0 /* XXX - should we ??? */
385 /* Handle the case of None */
386 if (obj == Py_None) {
387 return util_curl_unsetopt(self, option);
388 }
389 #endif
390
391 /* Handle the case of integer arguments */
392 if (PyLong_Check(obj)) {
393 long d = PyLong_AsLong(obj);
394 if (d == -1 && PyErr_Occurred()) {
395 return NULL;
396 }
397 switch(d) {
398 case CURL_LOCK_DATA_COOKIE:
399 case CURL_LOCK_DATA_DNS:
400 case CURL_LOCK_DATA_SSL_SESSION:
401 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 57, 0)
402 case CURL_LOCK_DATA_CONNECT:
403 #endif
404 #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 61, 0)
405 case CURL_LOCK_DATA_PSL:
406 #endif
407 break;
408 default:
409 goto error;
410 }
411 switch(option) {
412 case CURLSHOPT_SHARE:
413 case CURLSHOPT_UNSHARE:
414 curl_share_setopt(self->share_handle, option, d);
415 break;
416 default:
417 PyErr_SetString(PyExc_TypeError, "integers are not supported for this option");
418 return NULL;
419 }
420 Py_RETURN_NONE;
421 }
422 /* Failed to match any of the function signatures -- return error */
423 error:
424 PyErr_SetString(PyExc_TypeError, "invalid arguments to setopt");
425 return NULL;
426 }
427
428
429 static PyObject *do_share_getstate(CurlShareObject *self, PyObject *Py_UNUSED(ignored))
430 {
431 PyErr_SetString(PyExc_TypeError, "CurlShare objects do not support serialization");
432 return NULL;
433 }
434
435
436 static PyObject *do_share_setstate(CurlShareObject *self, PyObject *args)
437 {
438 PyErr_SetString(PyExc_TypeError, "CurlShare objects do not support deserialization");
439 return NULL;
440 }
441
442 static PyObject *do_share_enter(CurlShareObject *self, PyObject *Py_UNUSED(ignored))
443 {
444 Py_INCREF(self);
445 return (PyObject *)self;
446 }
447
448
449 /*************************************************************************
450 // type definitions
451 **************************************************************************/
452
453 /* --------------- methods --------------- */
454
455 PYCURL_INTERNAL PyMethodDef curlshareobject_methods[] = {
456 {"close", (PyCFunction)do_share_close, METH_NOARGS, share_close_doc},
457 {"closed", (PyCFunction)do_share_closed, METH_NOARGS, share_closed_doc},
458 {"setopt", (PyCFunction)do_share_setopt, METH_VARARGS, share_setopt_doc},
459 {"__getstate__", (PyCFunction)do_share_getstate, METH_NOARGS, NULL},
460 {"__setstate__", (PyCFunction)do_share_setstate, METH_VARARGS, NULL},
461 {"__enter__", (PyCFunction)do_share_enter, METH_NOARGS, NULL},
462 {"__exit__", (PyCFunction)do_share_close, METH_VARARGS, NULL},
463 {NULL, NULL, 0, 0}
464 };
465
466
467 /* --------------- setattr/getattr --------------- */
468
469
470 PYCURL_INTERNAL PyObject *
471 do_share_getattro(PyObject *o, PyObject *n)
472 {
473 PyObject *v;
474 assert_share_state((CurlShareObject *)o);
475 v = PyObject_GenericGetAttr(o, n);
476 if( !v && PyErr_ExceptionMatches(PyExc_AttributeError) )
477 {
478 PyErr_Clear();
479 v = my_getattro(o, n, ((CurlShareObject *)o)->dict,
480 curlshareobject_constants, curlshareobject_methods);
481 }
482 return v;
483 }
484
485 PYCURL_INTERNAL int
486 do_share_setattro(PyObject *o, PyObject *n, PyObject *v)
487 {
488 assert_share_state((CurlShareObject *)o);
489 return my_setattro(&((CurlShareObject *)o)->dict, n, v);
490 }
491
492 PYCURL_INTERNAL PyTypeObject CurlShare_Type = {
493 PyVarObject_HEAD_INIT(NULL, 0)
494 "pycurl.CurlShare", /* tp_name */
495 sizeof(CurlShareObject), /* tp_basicsize */
496 0, /* tp_itemsize */
497 (destructor)do_share_dealloc, /* tp_dealloc */
498 0, /* tp_print */
499 0, /* tp_getattr */
500 0, /* tp_setattr */
501 0, /* tp_reserved */
502 0, /* tp_repr */
503 0, /* tp_as_number */
504 0, /* tp_as_sequence */
505 0, /* tp_as_mapping */
506 0, /* tp_hash */
507 0, /* tp_call */
508 0, /* tp_str */
509 (getattrofunc)do_share_getattro, /* tp_getattro */
510 (setattrofunc)do_share_setattro, /* tp_setattro */
511 0, /* tp_as_buffer */
512 PYCURL_TYPE_FLAGS, /* tp_flags */
513 share_doc, /* tp_doc */
514 (traverseproc)do_share_traverse, /* tp_traverse */
515 (inquiry)do_share_clear, /* tp_clear */
516 0, /* tp_richcompare */
517 offsetof(CurlShareObject, weakreflist), /* tp_weaklistoffset */
518 0, /* tp_iter */
519 0, /* tp_iternext */
520 curlshareobject_methods, /* tp_methods */
521 0, /* tp_members */
522 0, /* tp_getset */
523 0, /* tp_base */
524 0, /* tp_dict */
525 0, /* tp_descr_get */
526 0, /* tp_descr_set */
527 0, /* tp_dictoffset */
528 0, /* tp_init */
529 PyType_GenericAlloc, /* tp_alloc */
530 (newfunc)do_share_new, /* tp_new */
531 PyObject_GC_Del, /* tp_free */
532 };
533
534 /* vi:ts=4:et:nowrap
535 */