aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/pybind11/docs/upgrade.rst
blob: 87bcebee2c410f9b4ae623a71a9bd10eedf65137 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
Upgrade guide
#############

This is a companion guide to the :doc:`changelog`. While the changelog briefly
lists all of the new features, improvements and bug fixes, this upgrade guide
focuses only the subset which directly impacts your experience when upgrading
to a new version. But it goes into more detail. This includes things like
deprecated APIs and their replacements, build system changes, general code
modernization and other useful information.

.. _upgrade-guide-2.6:

v2.6
====

Usage of the ``PYBIND11_OVERLOAD*`` macros and ``get_overload`` function should
be replaced by ``PYBIND11_OVERRIDE*`` and ``get_override``. In the future, the
old macros may be deprecated and removed.

``py::module`` has been renamed ``py::module_``, but a backward compatible
typedef has been included. This change was to avoid a language change in C++20
that requires unqualified ``module`` not be placed at the start of a logical
line. Qualified usage is unaffected and the typedef will remain unless the
C++ language rules change again.

The public constructors of ``py::module_`` have been deprecated. Use
``PYBIND11_MODULE`` or ``module_::create_extension_module`` instead.

An error is now thrown when ``__init__`` is forgotten on subclasses. This was
incorrect before, but was not checked. Add a call to ``__init__`` if it is
missing.

A ``py::type_error`` is now thrown when casting to a subclass (like
``py::bytes`` from ``py::object``) if the conversion is not valid. Make a valid
conversion instead.

The undocumented ``h.get_type()`` method has been deprecated and replaced by
``py::type::of(h)``.

Enums now have a ``__str__`` method pre-defined; if you want to override it,
the simplest fix is to add the new ``py::prepend()`` tag when defining
``"__str__"``.

If ``__eq__`` defined but not ``__hash__``, ``__hash__`` is now set to
``None``, as in normal CPython. You should add ``__hash__`` if you intended the
class to be hashable, possibly using the new ``py::hash`` shortcut.

The constructors for ``py::array`` now always take signed integers for size,
for consistency. This may lead to compiler warnings on some systems. Cast to
``py::ssize_t`` instead of ``std::size_t``.

The ``tools/clang`` submodule and ``tools/mkdoc.py`` have been moved to a
standalone package, `pybind11-mkdoc`_. If you were using those tools, please
use them via a pip install from the new location.

The ``pybind11`` package on PyPI no longer fills the wheel "headers" slot - if
you were using the headers from this slot, they are available by requesting the
``global`` extra, that is, ``pip install "pybind11[global]"``. (Most users will
be unaffected, as the ``pybind11/include`` location is reported by ``python -m
pybind11 --includes`` and ``pybind11.get_include()`` is still correct and has
not changed since 2.5).

.. _pybind11-mkdoc: https://github.com/pybind/pybind11-mkdoc

CMake support:
--------------

The minimum required version of CMake is now 3.4.  Several details of the CMake
support have been deprecated; warnings will be shown if you need to change
something. The changes are:

* ``PYBIND11_CPP_STANDARD=<platform-flag>`` is deprecated, please use
  ``CMAKE_CXX_STANDARD=<number>`` instead, or any other valid CMake CXX or CUDA
  standard selection method, like ``target_compile_features``.

* If you do not request a standard, pybind11 targets will compile with the
  compiler default, but not less than C++11, instead of forcing C++14 always.
  If you depend on the old behavior, please use ``set(CMAKE_CXX_STANDARD 14 CACHE STRING "")``
  instead.

* Direct ``pybind11::module`` usage should always be accompanied by at least
  ``set(CMAKE_CXX_VISIBILITY_PRESET hidden)`` or similar - it used to try to
  manually force this compiler flag (but not correctly on all compilers or with
  CUDA).

* ``pybind11_add_module``'s ``SYSTEM`` argument is deprecated and does nothing;
  linking now behaves like other imported libraries consistently in both
  config and submodule mode, and behaves like a ``SYSTEM`` library by
  default.

* If ``PYTHON_EXECUTABLE`` is not set, virtual environments (``venv``,
  ``virtualenv``, and ``conda``) are prioritized over the standard search
  (similar to the new FindPython mode).

In addition, the following changes may be of interest:

* ``CMAKE_INTERPROCEDURAL_OPTIMIZATION`` will be respected by
  ``pybind11_add_module`` if set instead of linking to ``pybind11::lto`` or
  ``pybind11::thin_lto``.

* Using ``find_package(Python COMPONENTS Interpreter Development)`` before
  pybind11 will cause pybind11 to use the new Python mechanisms instead of its
  own custom search, based on a patched version of classic ``FindPythonInterp``
  / ``FindPythonLibs``. In the future, this may become the default. A recent
  (3.15+ or 3.18.2+) version of CMake is recommended.



v2.5
====

The Python package now includes the headers as data in the package itself, as
well as in the "headers" wheel slot. ``pybind11 --includes`` and
``pybind11.get_include()`` report the new location, which is always correct
regardless of how pybind11 was installed, making the old ``user=`` argument
meaningless. If you are not using the function to get the location already, you
are encouraged to switch to the package location.


v2.2
====

Deprecation of the ``PYBIND11_PLUGIN`` macro
--------------------------------------------

``PYBIND11_MODULE`` is now the preferred way to create module entry points.
The old macro emits a compile-time deprecation warning.

.. code-block:: cpp

    // old
    PYBIND11_PLUGIN(example) {
        py::module m("example", "documentation string");

        m.def("add", [](int a, int b) { return a + b; });

        return m.ptr();
    }

    // new
    PYBIND11_MODULE(example, m) {
        m.doc() = "documentation string"; // optional

        m.def("add", [](int a, int b) { return a + b; });
    }


New API for defining custom constructors and pickling functions
---------------------------------------------------------------

The old placement-new custom constructors have been deprecated. The new approach
uses ``py::init()`` and factory functions to greatly improve type safety.

Placement-new can be called accidentally with an incompatible type (without any
compiler errors or warnings), or it can initialize the same object multiple times
if not careful with the Python-side ``__init__`` calls. The new-style custom
constructors prevent such mistakes. See :ref:`custom_constructors` for details.

.. code-block:: cpp

    // old -- deprecated (runtime warning shown only in debug mode)
    py::class<Foo>(m, "Foo")
        .def("__init__", [](Foo &self, ...) {
            new (&self) Foo(...); // uses placement-new
        });

    // new
    py::class<Foo>(m, "Foo")
        .def(py::init([](...) { // Note: no `self` argument
            return new Foo(...); // return by raw pointer
            // or: return std::make_unique<Foo>(...); // return by holder
            // or: return Foo(...); // return by value (move constructor)
        }));

Mirroring the custom constructor changes, ``py::pickle()`` is now the preferred
way to get and set object state. See :ref:`pickling` for details.

.. code-block:: cpp

    // old -- deprecated (runtime warning shown only in debug mode)
    py::class<Foo>(m, "Foo")
        ...
        .def("__getstate__", [](const Foo &self) {
            return py::make_tuple(self.value1(), self.value2(), ...);
        })
        .def("__setstate__", [](Foo &self, py::tuple t) {
            new (&self) Foo(t[0].cast<std::string>(), ...);
        });

    // new
    py::class<Foo>(m, "Foo")
        ...
        .def(py::pickle(
            [](const Foo &self) { // __getstate__
                return py::make_tuple(f.value1(), f.value2(), ...); // unchanged
            },
            [](py::tuple t) { // __setstate__, note: no `self` argument
                return new Foo(t[0].cast<std::string>(), ...);
                // or: return std::make_unique<Foo>(...); // return by holder
                // or: return Foo(...); // return by value (move constructor)
            }
        ));

For both the constructors and pickling, warnings are shown at module
initialization time (on import, not when the functions are called).
They're only visible when compiled in debug mode. Sample warning:

.. code-block:: none

    pybind11-bound class 'mymodule.Foo' is using an old-style placement-new '__init__'
    which has been deprecated. See the upgrade guide in pybind11's docs.


Stricter enforcement of hidden symbol visibility for pybind11 modules
---------------------------------------------------------------------

pybind11 now tries to actively enforce hidden symbol visibility for modules.
If you're using either one of pybind11's :doc:`CMake or Python build systems
<compiling>` (the two example repositories) and you haven't been exporting any
symbols, there's nothing to be concerned about. All the changes have been done
transparently in the background. If you were building manually or relied on
specific default visibility, read on.

Setting default symbol visibility to *hidden* has always been recommended for
pybind11 (see :ref:`faq:symhidden`). On Linux and macOS, hidden symbol
visibility (in conjunction with the ``strip`` utility) yields much smaller
module binaries. `CPython's extension docs`_ also recommend hiding symbols
by default, with the goal of avoiding symbol name clashes between modules.
Starting with v2.2, pybind11 enforces this more strictly: (1) by declaring
all symbols inside the ``pybind11`` namespace as hidden and (2) by including
the ``-fvisibility=hidden`` flag on Linux and macOS (only for extension
modules, not for embedding the interpreter).

.. _CPython's extension docs: https://docs.python.org/3/extending/extending.html#providing-a-c-api-for-an-extension-module

The namespace-scope hidden visibility is done automatically in pybind11's
headers and it's generally transparent to users. It ensures that:

* Modules compiled with different pybind11 versions don't clash with each other.

* Some new features, like ``py::module_local`` bindings, can work as intended.

The ``-fvisibility=hidden`` flag applies the same visibility to user bindings
outside of the ``pybind11`` namespace. It's now set automatic by pybind11's
CMake and Python build systems, but this needs to be done manually by users
of other build systems. Adding this flag:

* Minimizes the chances of symbol conflicts between modules. E.g. if two
  unrelated modules were statically linked to different (ABI-incompatible)
  versions of the same third-party library, a symbol clash would be likely
  (and would end with unpredictable results).

* Produces smaller binaries on Linux and macOS, as pointed out previously.

Within pybind11's CMake build system, ``pybind11_add_module`` has always been
setting the ``-fvisibility=hidden`` flag in release mode. From now on, it's
being applied unconditionally, even in debug mode and it can no longer be opted
out of with the ``NO_EXTRAS`` option. The ``pybind11::module`` target now also
adds this flag to it's interface. The ``pybind11::embed`` target is unchanged.

The most significant change here is for the ``pybind11::module`` target. If you
were previously relying on default visibility, i.e. if your Python module was
doubling as a shared library with dependents, you'll need to either export
symbols manually (recommended for cross-platform libraries) or factor out the
shared library (and have the Python module link to it like the other
dependents). As a temporary workaround, you can also restore default visibility
using the CMake code below, but this is not recommended in the long run:

.. code-block:: cmake

    target_link_libraries(mymodule PRIVATE pybind11::module)

    add_library(restore_default_visibility INTERFACE)
    target_compile_options(restore_default_visibility INTERFACE -fvisibility=default)
    target_link_libraries(mymodule PRIVATE restore_default_visibility)


Local STL container bindings
----------------------------

Previous pybind11 versions could only bind types globally -- all pybind11
modules, even unrelated ones, would have access to the same exported types.
However, this would also result in a conflict if two modules exported the
same C++ type, which is especially problematic for very common types, e.g.
``std::vector<int>``. :ref:`module_local` were added to resolve this (see
that section for a complete usage guide).

``py::class_`` still defaults to global bindings (because these types are
usually unique across modules), however in order to avoid clashes of opaque
types, ``py::bind_vector`` and ``py::bind_map`` will now bind STL containers
as ``py::module_local`` if their elements are: builtins (``int``, ``float``,
etc.), not bound using ``py::class_``, or bound as ``py::module_local``. For
example, this change allows multiple modules to bind ``std::vector<int>``
without causing conflicts. See :ref:`stl_bind` for more details.

When upgrading to this version, if you have multiple modules which depend on
a single global binding of an STL container, note that all modules can still
accept foreign  ``py::module_local`` types in the direction of Python-to-C++.
The locality only affects the C++-to-Python direction. If this is needed in
multiple modules, you'll need to either:

* Add a copy of the same STL binding to all of the modules which need it.

* Restore the global status of that single binding by marking it
  ``py::module_local(false)``.

The latter is an easy workaround, but in the long run it would be best to
localize all common type bindings in order to avoid conflicts with
third-party modules.


Negative strides for Python buffer objects and numpy arrays
-----------------------------------------------------------

Support for negative strides required changing the integer type from unsigned
to signed in the interfaces of ``py::buffer_info`` and ``py::array``. If you
have compiler warnings enabled, you may notice some new conversion warnings
after upgrading. These can be resolved using ``static_cast``.


Deprecation of some ``py::object`` APIs
---------------------------------------

To compare ``py::object`` instances by pointer, you should now use
``obj1.is(obj2)`` which is equivalent to ``obj1 is obj2`` in Python.
Previously, pybind11 used ``operator==`` for this (``obj1 == obj2``), but
that could be confusing and is now deprecated (so that it can eventually
be replaced with proper rich object comparison in a future release).

For classes which inherit from ``py::object``, ``borrowed`` and ``stolen``
were previously available as protected constructor tags. Now the types
should be used directly instead: ``borrowed_t{}`` and ``stolen_t{}``
(`#771 <https://github.com/pybind/pybind11/pull/771>`_).


Stricter compile-time error checking
------------------------------------

Some error checks have been moved from run time to compile time. Notably,
automatic conversion of ``std::shared_ptr<T>`` is not possible when ``T`` is
not directly registered with ``py::class_<T>`` (e.g. ``std::shared_ptr<int>``
or ``std::shared_ptr<std::vector<T>>`` are not automatically convertible).
Attempting to bind a function with such arguments now results in a compile-time
error instead of waiting to fail at run time.

``py::init<...>()`` constructor definitions are also stricter and now prevent
bindings which could cause unexpected behavior:

.. code-block:: cpp

    struct Example {
        Example(int &);
    };

    py::class_<Example>(m, "Example")
        .def(py::init<int &>()); // OK, exact match
        // .def(py::init<int>()); // compile-time error, mismatch

A non-``const`` lvalue reference is not allowed to bind to an rvalue. However,
note that a constructor taking ``const T &`` can still be registered using
``py::init<T>()`` because a ``const`` lvalue reference can bind to an rvalue.

v2.1
====

Minimum compiler versions are enforced at compile time
------------------------------------------------------

The minimums also apply to v2.0 but the check is now explicit and a compile-time
error is raised if the compiler does not meet the requirements:

* GCC >= 4.8
* clang >= 3.3 (appleclang >= 5.0)
* MSVC >= 2015u3
* Intel C++ >= 15.0


The ``py::metaclass`` attribute is not required for static properties
---------------------------------------------------------------------

Binding classes with static properties is now possible by default. The
zero-parameter version of ``py::metaclass()`` is deprecated. However, a new
one-parameter ``py::metaclass(python_type)`` version was added for rare
cases when a custom metaclass is needed to override pybind11's default.

.. code-block:: cpp

    // old -- emits a deprecation warning
    py::class_<Foo>(m, "Foo", py::metaclass())
        .def_property_readonly_static("foo", ...);

    // new -- static properties work without the attribute
    py::class_<Foo>(m, "Foo")
        .def_property_readonly_static("foo", ...);

    // new -- advanced feature, override pybind11's default metaclass
    py::class_<Bar>(m, "Bar", py::metaclass(custom_python_type))
        ...


v2.0
====

Breaking changes in ``py::class_``
----------------------------------

These changes were necessary to make type definitions in pybind11
future-proof, to support PyPy via its ``cpyext`` mechanism (`#527
<https://github.com/pybind/pybind11/pull/527>`_), and to improve efficiency
(`rev. 86d825 <https://github.com/pybind/pybind11/commit/86d825>`_).

1. Declarations of types that provide access via the buffer protocol must
   now include the ``py::buffer_protocol()`` annotation as an argument to
   the ``py::class_`` constructor.

   .. code-block:: cpp

       py::class_<Matrix>("Matrix", py::buffer_protocol())
           .def(py::init<...>())
           .def_buffer(...);

2. Classes which include static properties (e.g. ``def_readwrite_static()``)
   must now include the ``py::metaclass()`` attribute. Note: this requirement
   has since been removed in v2.1. If you're upgrading from 1.x, it's
   recommended to skip directly to v2.1 or newer.

3. This version of pybind11 uses a redesigned mechanism for instantiating
   trampoline classes that are used to override virtual methods from within
   Python. This led to the following user-visible syntax change:

   .. code-block:: cpp

       // old v1.x syntax
       py::class_<TrampolineClass>("MyClass")
           .alias<MyClass>()
           ...

       // new v2.x syntax
       py::class_<MyClass, TrampolineClass>("MyClass")
           ...

   Importantly, both the original and the trampoline class are now specified
   as arguments to the ``py::class_`` template, and the ``alias<..>()`` call
   is gone. The new scheme has zero overhead in cases when Python doesn't
   override any functions of the underlying C++ class.
   `rev. 86d825 <https://github.com/pybind/pybind11/commit/86d825>`_.

   The class type must be the first template argument given to ``py::class_``
   while the trampoline can be mixed in arbitrary order with other arguments
   (see the following section).


Deprecation of the ``py::base<T>()`` attribute
----------------------------------------------

``py::base<T>()`` was deprecated in favor of specifying ``T`` as a template
argument to ``py::class_``. This new syntax also supports multiple inheritance.
Note that, while the type being exported must be the first argument in the
``py::class_<Class, ...>`` template, the order of the following types (bases,
holder and/or trampoline) is not important.

.. code-block:: cpp

    // old v1.x
    py::class_<Derived>("Derived", py::base<Base>());

    // new v2.x
    py::class_<Derived, Base>("Derived");

    // new -- multiple inheritance
    py::class_<Derived, Base1, Base2>("Derived");

    // new -- apart from `Derived` the argument order can be arbitrary
    py::class_<Derived, Base1, Holder, Base2, Trampoline>("Derived");


Out-of-the-box support for ``std::shared_ptr``
----------------------------------------------

The relevant type caster is now built in, so it's no longer necessary to
include a declaration of the form:

.. code-block:: cpp

    PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>)

Continuing to do so won’t cause an error or even a deprecation warning,
but it's completely redundant.


Deprecation of a few ``py::object`` APIs
----------------------------------------

All of the old-style calls emit deprecation warnings.

+---------------------------------------+---------------------------------------------+
|  Old syntax                           |  New syntax                                 |
+=======================================+=============================================+
| ``obj.call(args...)``                 | ``obj(args...)``                            |
+---------------------------------------+---------------------------------------------+
| ``obj.str()``                         | ``py::str(obj)``                            |
+---------------------------------------+---------------------------------------------+
| ``auto l = py::list(obj); l.check()`` | ``py::isinstance<py::list>(obj)``           |
+---------------------------------------+---------------------------------------------+
| ``py::object(ptr, true)``             | ``py::reinterpret_borrow<py::object>(ptr)`` |
+---------------------------------------+---------------------------------------------+
| ``py::object(ptr, false)``            | ``py::reinterpret_steal<py::object>(ptr)``  |
+---------------------------------------+---------------------------------------------+
| ``if (obj.attr("foo"))``              | ``if (py::hasattr(obj, "foo"))``            |
+---------------------------------------+---------------------------------------------+
| ``if (obj["bar"])``                   | ``if (obj.contains("bar"))``                |
+---------------------------------------+---------------------------------------------+
d corruption: connection id %i: err %s: %s", conn ? (int)conn->id : -1, strerror(saved_errno), str); #ifdef TESTING /* Allow them to attach debugger. */ sleep(30); #endif syslog(LOG_DAEMON, "xenstored corruption: connection id %i: err %s: %s", conn ? (int)conn->id : -1, strerror(saved_errno), str); _exit(2); } static bool write_message(struct connection *conn) { int ret; struct buffered_data *out = conn->out; if (out->inhdr) { if (verbose) xprintf("Writing msg %i out to %p\n", out->hdr.msg.type, conn); ret = conn->write(conn, out->hdr.raw + out->used, sizeof(out->hdr) - out->used); if (ret < 0) return false; out->used += ret; if (out->used < sizeof(out->hdr)) return true; out->inhdr = false; out->used = 0; /* Second write might block if non-zero. */ if (out->hdr.msg.len) return true; } if (verbose) xprintf("Writing data len %i out to %p\n", out->hdr.msg.len, conn); ret = conn->write(conn, out->buffer + out->used, out->hdr.msg.len - out->used); if (ret < 0) return false; out->used += ret; if (out->used != out->hdr.msg.len) return true; conn->out = NULL; /* If this was an event, we wait for ack, otherwise we're done. */ if (!is_watch_event(conn, out)) talloc_free(out); queue_next_event(conn); return true; } static int destroy_conn(void *_conn) { struct connection *conn = _conn; /* Flush outgoing if possible, but don't block. */ if (!conn->domain) { fd_set set; struct timeval none; FD_ZERO(&set); FD_SET(conn->fd, &set); none.tv_sec = none.tv_usec = 0; while (conn->out && select(conn->fd+1, NULL, &set, NULL, &none) == 1) if (!write_message(conn)) break; close(conn->fd); } list_del(&conn->list); return 0; } static int initialize_set(fd_set *inset, fd_set *outset, int sock, int ro_sock, int event_fd) { struct connection *i; int max; FD_ZERO(inset); FD_ZERO(outset); FD_SET(sock, inset); max = sock; FD_SET(ro_sock, inset); if (ro_sock > max) max = ro_sock; FD_SET(event_fd, inset); if (event_fd > max) max = event_fd; list_for_each_entry(i, &connections, list) { if (i->domain) continue; if (!i->blocked) FD_SET(i->fd, inset); if (i->out) FD_SET(i->fd, outset); if (i->fd > max) max = i->fd; } return max; } /* Read everything from a talloc_open'ed fd. */ static void *read_all(int *fd, unsigned int *size) { unsigned int max = 4; int ret; void *buffer = talloc_size(fd, max); *size = 0; while ((ret = read(*fd, buffer + *size, max - *size)) > 0) { *size += ret; if (*size == max) buffer = talloc_realloc_size(fd, buffer, max *= 2); } if (ret < 0) return NULL; return buffer; } static int destroy_fd(void *_fd) { int *fd = _fd; close(*fd); return 0; } /* Return a pointer to an fd, self-closing and attached to this pathname. */ static int *talloc_open(const char *pathname, int flags, int mode) { int *fd; fd = talloc(pathname, int); *fd = open(pathname, flags, mode); if (*fd < 0) { int saved_errno = errno; talloc_free(fd); errno = saved_errno; return NULL; } talloc_set_destructor(fd, destroy_fd); return fd; } /* Is child a subnode of parent, or equal? */ bool is_child(const char *child, const char *parent) { unsigned int len = strlen(parent); /* / should really be "" for this algorithm to work, but that's a * usability nightmare. */ if (streq(parent, "/")) return true; if (strncmp(child, parent, len) != 0) return false; return child[len] == '/' || child[len] == '\0'; } /* Answer never ends in /. */ char *node_dir_outside_transaction(const char *node) { if (streq(node, "/")) return talloc_strdup(node, xs_daemon_store()); return talloc_asprintf(node, "%s%s", xs_daemon_store(), node); } static char *node_dir(struct transaction *trans, const char *node) { if (!trans || !within_transaction(trans, node)) return node_dir_outside_transaction(node); return node_dir_inside_transaction(trans, node); } static char *node_datafile(struct transaction *trans, const char *node) { return talloc_asprintf(node, "%s/.data", node_dir(trans, node)); } static char *node_permfile(struct transaction *trans, const char *node) { return talloc_asprintf(node, "%s/.perms", node_dir(trans, node)); } struct buffered_data *new_buffer(void *ctx) { struct buffered_data *data; data = talloc(ctx, struct buffered_data); data->inhdr = true; data->used = 0; data->buffer = NULL; return data; } /* Return length of string (including nul) at this offset. */ unsigned int get_string(const struct buffered_data *data, unsigned int offset) { const char *nul; if (offset >= data->used) return 0; nul = memchr(data->buffer + offset, 0, data->used - offset); if (!nul) return 0; return nul - (data->buffer + offset) + 1; } /* Break input into vectors, return the number, fill in up to num of them. */ unsigned int get_strings(struct buffered_data *data, char *vec[], unsigned int num) { unsigned int off, i, len; off = i = 0; while ((len = get_string(data, off)) != 0) { if (i < num) vec[i] = data->buffer + off; i++; off += len; } return i; } /* Returns "false", meaning "connection is not blocked". */ bool send_reply(struct connection *conn, enum xsd_sockmsg_type type, const void *data, unsigned int len) { struct buffered_data *bdata; /* When data gets freed, we want list entry is destroyed (so * list entry is a child). */ bdata = new_buffer(conn); bdata->buffer = talloc_array(bdata, char, len); bdata->hdr.msg.type = type; bdata->hdr.msg.len = len; memcpy(bdata->buffer, data, len); /* There might be an event going out now. Queue behind it. */ if (conn->out) { assert(conn->out->hdr.msg.type == XS_WATCH_EVENT); assert(!conn->waiting_reply); conn->waiting_reply = bdata; } else conn->out = bdata; return false; } /* Some routines (write, mkdir, etc) just need a non-error return */ bool send_ack(struct connection *conn, enum xsd_sockmsg_type type) { return send_reply(conn, type, "OK", sizeof("OK")); } bool send_error(struct connection *conn, int error) { unsigned int i; for (i = 0; error != xsd_errors[i].errnum; i++) if (i == ARRAY_SIZE(xsd_errors) - 1) corrupt(conn, "Unknown error %i (%s)", error, strerror(error)); return send_reply(conn, XS_ERROR, xsd_errors[i].errstring, strlen(xsd_errors[i].errstring) + 1); } static bool valid_chars(const char *node) { /* Nodes can have lots of crap. */ return (strspn(node, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789-/_@") == strlen(node)); } static bool is_valid_nodename(const char *node) { /* Must start in /. */ if (!strstarts(node, "/")) return false; /* Cannot end in / (unless it's just "/"). */ if (strends(node, "/") && !streq(node, "/")) return false; /* No double //. */ if (strstr(node, "//")) return false; return valid_chars(node); } /* We expect one arg in the input: return NULL otherwise. */ static const char *onearg(struct buffered_data *in) { if (get_string(in, 0) != in->used) return NULL; return in->buffer; } /* If it fails, returns NULL and sets errno. */ static struct xs_permissions *get_perms(struct transaction *transaction, const char *node, unsigned int *num) { unsigned int size; char *strings; struct xs_permissions *ret; int *fd; fd = talloc_open(node_permfile(transaction, node), O_RDONLY, 0); if (!fd) return NULL; strings = read_all(fd, &size); if (!strings) return NULL; *num = xs_count_strings(strings, size); ret = talloc_array(node, struct xs_permissions, *num); if (!xs_strings_to_perms(ret, *num, strings)) corrupt(NULL, "Permissions corrupt for %s", node); return ret; } static char *perms_to_strings(const char *node, struct xs_permissions *perms, unsigned int num, unsigned int *len) { unsigned int i; char *strings = NULL; char buffer[MAX_STRLEN(domid_t) + 1]; for (*len = 0, i = 0; i < num; i++) { if (!xs_perm_to_string(&perms[i], buffer)) return NULL; strings = talloc_realloc(node, strings, char, *len + strlen(buffer) + 1); strcpy(strings + *len, buffer); *len += strlen(buffer) + 1; } return strings; } /* Destroy this, and its children, and its children's children. */ int destroy_path(void *path) { DIR *dir; struct dirent *dirent; dir = opendir(path); if (!dir) { if (unlink(path) == 0 || errno == ENOENT) return 0; corrupt(NULL, "Destroying path %s", path); } while ((dirent = readdir(dir)) != NULL) { char fullpath[strlen(path) + 1 + strlen(dirent->d_name) + 1]; sprintf(fullpath, "%s/%s", (char *)path, dirent->d_name); if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")) destroy_path(fullpath); } closedir(dir); if (rmdir(path) != 0) corrupt(NULL, "Destroying directory %s", path); return 0; } /* Create a self-destructing temporary file */ static char *tempfile(const char *path, void *contents, unsigned int len) { int *fd; char *tmppath = talloc_asprintf(path, "%s.tmp", path); fd = talloc_open(tmppath, O_WRONLY|O_CREAT|O_EXCL, 0640); if (!fd) return NULL; talloc_set_destructor(tmppath, destroy_path); if (!xs_write_all(*fd, contents, len)) return NULL; return tmppath; } /* We assume rename() doesn't fail on moves in same dir. */ static void commit_tempfile(const char *path) { char realname[strlen(path) + 1]; unsigned int len = strrchr(path, '.') - path; memcpy(realname, path, len); realname[len] = '\0'; if (rename(path, realname) != 0) corrupt(NULL, "Committing %s", realname); talloc_set_destructor(path, NULL); } static bool set_perms(struct transaction *transaction, const char *node, struct xs_permissions *perms, unsigned int num) { unsigned int len; char *permpath, *strings; strings = perms_to_strings(node, perms, num, &len); if (!strings) return false; /* Create then move. */ permpath = tempfile(node_permfile(transaction, node), strings, len); if (!permpath) return false; commit_tempfile(permpath); return true; } static char *get_parent(const char *node) { char *slash = strrchr(node + 1, '/'); if (!slash) return talloc_strdup(node, "/"); return talloc_asprintf(node, "%.*s", slash - node, node); } static enum xs_perm_type perm_for_id(domid_t id, struct xs_permissions *perms, unsigned int num) { unsigned int i; /* Owners and tools get it all... */ if (!id || perms[0].id == id) return XS_PERM_READ|XS_PERM_WRITE|XS_PERM_CREATE|XS_PERM_OWNER; for (i = 1; i < num; i++) if (perms[i].id == id) return perms[i].perms; return perms[0].perms; } /* We have a weird permissions system. You can allow someone into a * specific node without allowing it in the parents. If it's going to * fail, however, we don't want the errno to indicate any information * about the node. */ static int check_with_parents(struct connection *conn, const char *node, int errnum) { struct xs_permissions *perms; unsigned int num; /* We always tell them about memory failures. */ if (errnum == ENOMEM) return errnum; do { node = get_parent(node); perms = get_perms(conn->transaction, node, &num); if (perms) break; } while (!streq(node, "/")); /* No permission at root? We're in trouble. */ if (!perms) corrupt(conn, "No permissions file at root"); if (!(perm_for_id(conn->id, perms, num) & XS_PERM_READ)) return EACCES; return errnum; } bool check_node_perms(struct connection *conn, const char *node, enum xs_perm_type perm) { struct xs_permissions *perms; unsigned int num; if (!node) { errno = EINVAL; return false; } if (!node || !is_valid_nodename(node)) { errno = EINVAL; return false; } if (!conn->can_write && (perm & XS_PERM_WRITE)) { errno = EROFS; return false; } perms = get_perms(conn->transaction, node, &num); /* No permissions. If we want to create it and * it doesn't exist, check parent directory. */ if (!perms && errno == ENOENT && (perm & XS_PERM_CREATE)) { char *parent = get_parent(node); if (!parent) return false; perms = get_perms(conn->transaction, parent, &num); } if (!perms) { errno = check_with_parents(conn, node, errno); return false; } if (perm_for_id(conn->id, perms, num) & perm) return true; errno = check_with_parents(conn, node, EACCES); return false; } static bool send_directory(struct connection *conn, const char *node) { char *path, *reply = talloc_strdup(node, ""); unsigned int reply_len = 0; DIR *dir; struct dirent *dirent; if (!check_node_perms(conn, node, XS_PERM_READ)) return send_error(conn, errno); path = node_dir(conn->transaction, node); dir = opendir(path); if (!dir) return send_error(conn, errno); while ((dirent = readdir(dir)) != NULL) { int len = strlen(dirent->d_name) + 1; if (!valid_chars(dirent->d_name)) continue; reply = talloc_realloc(path, reply, char, reply_len + len); strcpy(reply + reply_len, dirent->d_name); reply_len += len; } closedir(dir); return send_reply(conn, XS_DIRECTORY, reply, reply_len); } static bool do_read(struct connection *conn, const char *node) { char *value; unsigned int size; int *fd; if (!check_node_perms(conn, node, XS_PERM_READ)) return send_error(conn, errno); fd = talloc_open(node_datafile(conn->transaction, node), O_RDONLY, 0); if (!fd) { /* Data file doesn't exist? We call that a directory */ if (errno == ENOENT) errno = EISDIR; return send_error(conn, errno); } value = read_all(fd, &size); if (!value) return send_error(conn, errno); return send_reply(conn, XS_READ, value, size); } /* Create a new directory. Optionally put data in it (if data != NULL) */ static bool new_directory(struct connection *conn, const char *node, void *data, unsigned int datalen) { struct xs_permissions perms; char *permstr; unsigned int len; int *fd; char *dir = node_dir(conn->transaction, node); if (mkdir(dir, 0750) != 0) return false; /* Set destructor so we clean up if neccesary. */ talloc_set_destructor(dir, destroy_path); /* Default permisisons: we own it, noone else has permission. */ perms.id = conn->id; perms.perms = XS_PERM_NONE; permstr = perms_to_strings(dir, &perms, 1, &len); fd = talloc_open(node_permfile(conn->transaction, node), O_WRONLY|O_CREAT|O_EXCL, 0640); if (!fd || !xs_write_all(*fd, permstr, len)) return false; if (data) { char *datapath = node_datafile(conn->transaction, node); fd = talloc_open(datapath, O_WRONLY|O_CREAT|O_EXCL, 0640); if (!fd || !xs_write_all(*fd, data, datalen)) return false; } /* Finished! */ talloc_set_destructor(dir, NULL); return true; } /* path, flags, data... */ static bool do_write(struct connection *conn, struct buffered_data *in) { unsigned int offset, datalen; char *vec[2]; char *node, *tmppath; enum xs_perm_type mode; struct stat st; /* Extra "strings" can be created by binary data. */ if (get_strings(in, vec, ARRAY_SIZE(vec)) < ARRAY_SIZE(vec)) return send_error(conn, EINVAL); node = vec[0]; if (!within_transaction(conn->transaction, node)) return send_error(conn, EROFS); if (transaction_block(conn, node)) return true; offset = strlen(vec[0]) + strlen(vec[1]) + 2; datalen = in->used - offset; if (streq(vec[1], XS_WRITE_NONE)) mode = XS_PERM_WRITE; else if (streq(vec[1], XS_WRITE_CREATE)) mode = XS_PERM_WRITE|XS_PERM_CREATE; else if (streq(vec[1], XS_WRITE_CREATE_EXCL)) mode = XS_PERM_WRITE|XS_PERM_CREATE; else return send_error(conn, EINVAL); if (!check_node_perms(conn, node, mode)) return send_error(conn, errno); if (lstat(node_dir(conn->transaction, node), &st) != 0) { /* Does not exist... */ if (errno != ENOENT) return send_error(conn, errno); /* Not going to create it? */ if (!(mode & XS_PERM_CREATE)) return send_error(conn, ENOENT); if (!new_directory(conn, node, in->buffer + offset, datalen)) return send_error(conn, errno); } else { /* Exists... */ if (streq(vec[1], XS_WRITE_CREATE_EXCL)) return send_error(conn, EEXIST); tmppath = tempfile(node_datafile(conn->transaction, node), in->buffer + offset, datalen); if (!tmppath) return send_error(conn, errno); commit_tempfile(tmppath); } add_change_node(conn->transaction, node); send_ack(conn, XS_WRITE); fire_watches(conn->transaction, node); return false; } static bool do_mkdir(struct connection *conn, const char *node) { if (!check_node_perms(conn, node, XS_PERM_WRITE|XS_PERM_CREATE)) return send_error(conn, errno); if (!within_transaction(conn->transaction, node)) return send_error(conn, EROFS); if (transaction_block(conn, node)) return true; if (!new_directory(conn, node, NULL, 0)) return send_error(conn, errno); add_change_node(conn->transaction, node); send_ack(conn, XS_MKDIR); fire_watches(conn->transaction, node); return false; } static bool do_rm(struct connection *conn, const char *node) { char *tmppath, *path; if (!check_node_perms(conn, node, XS_PERM_WRITE)) return send_error(conn, errno); if (!within_transaction(conn->transaction, node)) return send_error(conn, EROFS); if (transaction_block(conn, node)) return true; if (streq(node, "/")) return send_error(conn, EINVAL); /* We move the directory to temporary name, destructor cleans up. */ path = node_dir(conn->transaction, node); tmppath = talloc_asprintf(node, "%s.tmp", path); talloc_set_destructor(tmppath, destroy_path); if (rename(path, tmppath) != 0) return send_error(conn, errno); add_change_node(conn->transaction, node); send_ack(conn, XS_RM); fire_watches(conn->transaction, node); return false; } static bool do_get_perms(struct connection *conn, const char *node) { struct xs_permissions *perms; char *strings; unsigned int len, num; if (!check_node_perms(conn, node, XS_PERM_READ)) return send_error(conn, errno); perms = get_perms(conn->transaction, node, &num); if (!perms) return send_error(conn, errno); strings = perms_to_strings(node, perms, num, &len); if (!strings) return send_error(conn, errno); return send_reply(conn, XS_GET_PERMS, strings, len); } static bool do_set_perms(struct connection *conn, struct buffered_data *in) { unsigned int num; char *node; struct xs_permissions *perms; num = xs_count_strings(in->buffer, in->used); if (num < 2) return send_error(conn, EINVAL); /* First arg is node name. */ node = in->buffer; in->buffer += strlen(in->buffer) + 1; num--; if (!within_transaction(conn->transaction, node)) return send_error(conn, EROFS); if (transaction_block(conn, node)) return true; /* We must own node to do this (tools can do this too). */ if (!check_node_perms(conn, node, XS_PERM_WRITE|XS_PERM_OWNER)) return send_error(conn, errno); perms = talloc_array(node, struct xs_permissions, num); if (!xs_strings_to_perms(perms, num, in->buffer)) return send_error(conn, errno); if (!set_perms(conn->transaction, node, perms, num)) return send_error(conn, errno); add_change_node(conn->transaction, node); send_ack(conn, XS_SET_PERMS); fire_watches(conn->transaction, node); return false; } /* Process "in" for conn: "in" will vanish after this conversation, so * we can talloc off it for temporary variables. May free "conn". * Returns true if can't complete due to block. */ static bool process_message(struct connection *conn, struct buffered_data *in) { switch (in->hdr.msg.type) { case XS_DIRECTORY: return send_directory(conn, onearg(in)); case XS_READ: return do_read(conn, onearg(in)); case XS_WRITE: return do_write(conn, in); case XS_MKDIR: return do_mkdir(conn, onearg(in)); case XS_RM: return do_rm(conn, onearg(in)); case XS_GET_PERMS: return do_get_perms(conn, onearg(in)); case XS_SET_PERMS: return do_set_perms(conn, in); case XS_SHUTDOWN: /* FIXME: Implement gentle shutdown too. */ /* Only tools can do this. */ if (conn->id != 0) return send_error(conn, EACCES); if (!conn->can_write) return send_error(conn, EROFS); send_ack(conn, XS_SHUTDOWN); /* Everything hangs off auto-free context, freed at exit. */ exit(0); #ifdef TESTING case XS_DEBUG: { /* For testing, we allow them to set id. */ if (streq(in->buffer, "setid")) { conn->id = atoi(in->buffer + get_string(in, 0)); send_ack(conn, XS_DEBUG); } else if (streq(in->buffer, "failtest")) { if (get_string(in, 0) < in->used) srandom(atoi(in->buffer + get_string(in, 0))); send_ack(conn, XS_DEBUG); failtest = true; } return false; } #endif /* TESTING */ case XS_WATCH: return do_watch(conn, in); case XS_WATCH_ACK: return do_watch_ack(conn); case XS_UNWATCH: return do_unwatch(conn, onearg(in)); case XS_TRANSACTION_START: return do_transaction_start(conn, onearg(in)); case XS_TRANSACTION_END: return do_transaction_end(conn, onearg(in)); case XS_INTRODUCE: return do_introduce(conn, in); case XS_RELEASE: return do_release(conn, onearg(in)); case XS_GETDOMAINPATH: return do_get_domain_path(conn, onearg(in)); case XS_WATCH_EVENT: default: eprintf("Client unknown operation %i", in->hdr.msg.type); send_error(conn, ENOSYS); return false; } } static int out_of_mem(void *data) { longjmp(*(jmp_buf *)data, 1); } static void consider_message(struct connection *conn) { struct buffered_data *in = NULL; enum xsd_sockmsg_type type = conn->in->hdr.msg.type; jmp_buf talloc_fail; /* For simplicity, we kill the connection on OOM. */ talloc_set_fail_handler(out_of_mem, &talloc_fail); if (setjmp(talloc_fail)) { talloc_free(conn); goto end; } if (verbose) xprintf("Got message %i len %i from %p\n", type, conn->in->hdr.msg.len, conn); /* We might get a command while waiting for an ack: this means * the other end discarded it: we will re-transmit. */ if (type != XS_WATCH_ACK) reset_watch_event(conn); /* Careful: process_message may free connection. We detach * "in" beforehand and allocate the new buffer to avoid * touching conn after process_message. */ in = talloc_steal(talloc_autofree_context(), conn->in); conn->in = new_buffer(conn); if (process_message(conn, in)) { /* Blocked by transaction: queue for re-xmit. */ talloc_free(conn->in); conn->in = in; in = NULL; } end: talloc_free(in); talloc_set_fail_handler(NULL, NULL); if (talloc_total_blocks(NULL) != talloc_total_blocks(talloc_autofree_context()) + 1) talloc_report_full(NULL, stderr); } /* Errors in reading or allocating here mean we get out of sync, so we * drop the whole client connection. */ void handle_input(struct connection *conn) { int bytes; struct buffered_data *in; assert(!conn->blocked); in = conn->in; /* Not finished header yet? */ if (in->inhdr) { bytes = conn->read(conn, in->hdr.raw + in->used, sizeof(in->hdr) - in->used); if (bytes <= 0) goto bad_client; in->used += bytes; if (in->used != sizeof(in->hdr)) return; if (in->hdr.msg.len > PATH_MAX) { syslog(LOG_DAEMON, "Client tried to feed us %i", in->hdr.msg.len); goto bad_client; } in->buffer = talloc_array(in, char, in->hdr.msg.len); if (!in->buffer) goto bad_client; in->used = 0; in->inhdr = false; return; } bytes = conn->read(conn, in->buffer + in->used, in->hdr.msg.len - in->used); if (bytes < 0) goto bad_client; in->used += bytes; if (in->used != in->hdr.msg.len) return; consider_message(conn); return; bad_client: /* Kill it. */ talloc_free(conn); } void handle_output(struct connection *conn) { if (!write_message(conn)) talloc_free(conn); } /* If a transaction has ended, see if we can unblock any connections. */ static void unblock_connections(void) { struct connection *i, *tmp; list_for_each_entry_safe(i, tmp, &connections, list) { if (!i->blocked) continue; if (!transaction_covering_node(i->blocked)) { talloc_free(i->blocked); i->blocked = NULL; consider_message(i); } } /* To balance bias, move first entry to end. */ if (!list_empty(&connections)) { i = list_top(&connections, struct connection, list); list_del(&i->list); list_add_tail(&i->list, &connections); } } struct connection *new_connection(connwritefn_t *write, connreadfn_t *read) { struct connection *new; jmp_buf talloc_fail; new = talloc(talloc_autofree_context(), struct connection); if (!new) return NULL; new->blocked = false; new->out = new->waiting_reply = NULL; new->event = NULL; new->fd = -1; new->id = 0; new->domain = NULL; new->transaction = NULL; new->write = write; new->read = read; new->can_write = true; talloc_set_fail_handler(out_of_mem, &talloc_fail); if (setjmp(talloc_fail)) { talloc_free(new); return NULL; } new->in = new_buffer(new); talloc_set_fail_handler(NULL, NULL); list_add_tail(&new->list, &connections); talloc_set_destructor(new, destroy_conn); return new; } static int writefd(struct connection *conn, const void *data, unsigned int len) { return write(conn->fd, data, len); } static int readfd(struct connection *conn, void *data, unsigned int len) { return read(conn->fd, data, len); } static void accept_connection(int sock, bool canwrite) { int fd; struct connection *conn; fd = accept(sock, NULL, NULL); if (fd < 0) return; conn = new_connection(writefd, readfd); if (conn) { conn->fd = fd; conn->can_write = canwrite; } else close(fd); } /* Calc timespan from now to absolute time. */ static void time_relative_to_now(struct timeval *tv) { struct timeval now; gettimeofday(&now, NULL); if (timercmp(&now, tv, >)) timerclear(tv); else { tv->tv_sec -= now.tv_sec; if (now.tv_usec > tv->tv_usec) { tv->tv_sec--; tv->tv_usec += 1000000; } tv->tv_usec -= now.tv_usec; } } static struct option options[] = { { "no-fork", 0, NULL, 'N' }, { "verbose", 0, NULL, 'V' }, { "output-pid", 0, NULL, 'P' }, { NULL, 0, NULL, 0 } }; int main(int argc, char *argv[]) { int opt, *sock, *ro_sock, event_fd, max, tmpout; struct sockaddr_un addr; fd_set inset, outset; bool dofork = true; bool outputpid = false; while ((opt = getopt_long(argc, argv, "DV", options, NULL)) != -1) { switch (opt) { case 'N': dofork = false; break; case 'V': verbose = true; break; case 'P': outputpid = true; break; } } if (optind != argc) barf("%s: No arguments desired", argv[0]); talloc_enable_leak_report_full(); /* Create sockets for them to listen to. */ sock = talloc(talloc_autofree_context(), int); *sock = socket(PF_UNIX, SOCK_STREAM, 0); if (*sock < 0) barf_perror("Could not create socket"); ro_sock = talloc(talloc_autofree_context(), int); *ro_sock = socket(PF_UNIX, SOCK_STREAM, 0); if (*ro_sock < 0) barf_perror("Could not create socket"); talloc_set_destructor(sock, destroy_fd); talloc_set_destructor(ro_sock, destroy_fd); /* Don't kill us with SIGPIPE. */ signal(SIGPIPE, SIG_IGN); /* FIXME: Be more sophisticated, don't mug running daemon. */ unlink(xs_daemon_socket()); unlink(xs_daemon_socket_ro()); addr.sun_family = AF_UNIX; strcpy(addr.sun_path, xs_daemon_socket()); if (bind(*sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) barf_perror("Could not bind socket to %s", xs_daemon_socket()); strcpy(addr.sun_path, xs_daemon_socket_ro()); if (bind(*ro_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) barf_perror("Could not bind socket to %s", xs_daemon_socket_ro()); if (chmod(xs_daemon_socket(), 0600) != 0 || chmod(xs_daemon_socket_ro(), 0660) != 0) barf_perror("Could not chmod sockets"); if (listen(*sock, 1) != 0 || listen(*ro_sock, 1) != 0) barf_perror("Could not listen on sockets"); /* If we're the first, create .perms file for root. */ if (mkdir(xs_daemon_store(), 0750) == 0) { struct xs_permissions perms; char *root = talloc_strdup(talloc_autofree_context(), "/"); perms.id = 0; perms.perms = XS_PERM_READ; if (!set_perms(NULL, root, &perms, 1)) barf_perror("Could not create permissions in root"); talloc_free(root); mkdir(xs_daemon_transactions(), 0750); } else if (errno != EEXIST) barf_perror("Could not create root %s", xs_daemon_store()); /* Listen to hypervisor. */ event_fd = domain_init(); /* Debugging: daemonize() closes standard fds, so dup here. */ tmpout = dup(STDOUT_FILENO); if (dofork) { openlog("xenstored", 0, LOG_DAEMON); daemonize(); } if (outputpid) { char buffer[20]; sprintf(buffer, "%i\n", getpid()); write(tmpout, buffer, strlen(buffer)); } close(tmpout); #ifdef TESTING signal(SIGUSR1, stop_failtest); #endif /* Get ready to listen to the tools. */ max = initialize_set(&inset, &outset, *sock, *ro_sock, event_fd); /* Main loop. */ for (;;) { struct connection *i; struct timeval *tvp = NULL, tv; timerclear(&tv); shortest_transaction_timeout(&tv); if (timerisset(&tv)) { time_relative_to_now(&tv); tvp = &tv; } if (select(max+1, &inset, &outset, NULL, tvp) < 0) { if (errno == EINTR) continue; barf_perror("Select failed"); } if (FD_ISSET(*sock, &inset)) accept_connection(*sock, true); if (FD_ISSET(*ro_sock, &inset)) accept_connection(*ro_sock, false); if (FD_ISSET(event_fd, &inset)) handle_event(event_fd); list_for_each_entry(i, &connections, list) { if (i->domain) continue; /* Operations can delete themselves or others * (xs_release): list is not safe after input, * so break. */ if (FD_ISSET(i->fd, &inset)) { handle_input(i); break; } if (FD_ISSET(i->fd, &outset)) { handle_output(i); break; } } if (tvp) check_transaction_timeout(); /* If transactions ended, we might be able to do more work. */ unblock_connections(); max = initialize_set(&inset, &outset, *sock,*ro_sock,event_fd); } }