aixand-lib-local-runtime 0.1.0b0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aixand_lib_local_runtime-0.1.0b0.dist-info/METADATA +68 -0
- aixand_lib_local_runtime-0.1.0b0.dist-info/RECORD +12 -0
- aixand_lib_local_runtime-0.1.0b0.dist-info/WHEEL +5 -0
- aixand_lib_local_runtime-0.1.0b0.dist-info/licenses/LICENSE +202 -0
- aixand_lib_local_runtime-0.1.0b0.dist-info/top_level.txt +1 -0
- aixand_local_runtime/__init__.py +25 -0
- aixand_local_runtime/_macos_process.py +67 -0
- aixand_local_runtime/_windows_process.py +31 -0
- aixand_local_runtime/ipc.py +159 -0
- aixand_local_runtime/processes.py +34 -0
- aixand_local_runtime/signals.py +22 -0
- aixand_local_runtime/websocket.py +69 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: aixand-lib-local-runtime
|
|
3
|
+
Version: 0.1.0b0
|
|
4
|
+
Summary: Local IPC, WebSockets, process identity, and shutdown utilities
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Requires-Python: >=3.11
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: aixand-lib-filesystem<0.2,>=0.1.0b0
|
|
10
|
+
Provides-Extra: websocket
|
|
11
|
+
Requires-Dist: websockets<18,>=15; extra == "websocket"
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# Local runtime utilities
|
|
15
|
+
|
|
16
|
+
Independently installable Python 3.11+ package: `aixand-lib-local-runtime`, import
|
|
17
|
+
`aixand_local_runtime`. It depends on `aixand-lib-filesystem`, with no application service or external repository requirements. The optional `[websocket]` extra
|
|
18
|
+
installs `websockets>=15,<18`; core IPC uses only the standard library and OS APIs.
|
|
19
|
+
|
|
20
|
+
- `ipc`: synchronous and asynchronous local connections and servers, endpoint
|
|
21
|
+
discovery, and Windows endpoint authentication.
|
|
22
|
+
- `websocket`: local WebSocket client/server adapters.
|
|
23
|
+
- `processes`: opaque process-birth identities for checking the same PID on the
|
|
24
|
+
same host; exited/unidentifiable processes return `None`.
|
|
25
|
+
- `signals`: shutdown callbacks for the current asyncio loop.
|
|
26
|
+
|
|
27
|
+
The package root exports the IPC, process identity, and signal APIs. Filesystem
|
|
28
|
+
operations are imported directly from `aixand_filesystem`; they are not duplicated
|
|
29
|
+
or re-exported here. Product configuration and provider behavior belong to callers.
|
|
30
|
+
|
|
31
|
+
POSIX uses Unix sockets. Windows uses loopback and protected endpoint descriptors
|
|
32
|
+
with the `aixand.local/1` schema and a fresh per-endpoint capability.
|
|
33
|
+
Applications own endpoint placement and lifecycle policy.
|
|
34
|
+
|
|
35
|
+
From this directory:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
python -m pip install -e ../filesystem -e '.[websocket]'
|
|
39
|
+
python -m unittest discover -s tests -v
|
|
40
|
+
python -m pip wheel --no-deps .
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Native Windows and macOS behavior requires verification on those platforms.
|
|
44
|
+
|
|
45
|
+
## Local developer commands
|
|
46
|
+
|
|
47
|
+
Inside the aixand-libraries source checkout, run from this directory with
|
|
48
|
+
Python 3.11+ and uv:
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
./dev.sh develop --local
|
|
52
|
+
./dev.sh check
|
|
53
|
+
./dev.sh test
|
|
54
|
+
./dev.sh build
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Use `.\dev.ps1` on Windows. Each package owns its development environment and
|
|
58
|
+
helper. Commands run within this package. See the
|
|
59
|
+
[development guide](docs/development.md) for wheel-based setup without sibling
|
|
60
|
+
sources, lock updates, and build outputs.
|
|
61
|
+
|
|
62
|
+
## Documentation and support
|
|
63
|
+
|
|
64
|
+
See the [documentation index](docs/README.md) for usage, API reference, and runnable
|
|
65
|
+
starter examples. Report ordinary problems through the integrating product's
|
|
66
|
+
support channel or issue tracker. External contributions are not currently
|
|
67
|
+
accepted due to limited maintainer capacity; see [reporting guidance](../docs/reporting.md).
|
|
68
|
+
Report vulnerabilities [privately](../SECURITY.md).
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
aixand_lib_local_runtime-0.1.0b0.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
2
|
+
aixand_local_runtime/__init__.py,sha256=QfllUcMQZZO30N1knpoaps7c6KwYsn55_W-Lyn1GRqw,498
|
|
3
|
+
aixand_local_runtime/_macos_process.py,sha256=_oLTP4NW1R2VWD2p233Jx5IimS2MjaCBM8D_WbqFddo,1824
|
|
4
|
+
aixand_local_runtime/_windows_process.py,sha256=PXetIfkRJwlXaVzngyorP47y4NFC-D328QUpM1ym6_k,1026
|
|
5
|
+
aixand_local_runtime/ipc.py,sha256=-yRwUTYEFaDDpdIZyV363hhe68Y7fzAcnDWZNJQmc_0,4585
|
|
6
|
+
aixand_local_runtime/processes.py,sha256=of3edPpluf4xRMffQtzxZ7S8ljBnYEho1561yYTT1U8,984
|
|
7
|
+
aixand_local_runtime/signals.py,sha256=QC6G5vAvzYKcNGQOwtBNkH6-loaeQOKOi1rNReQJvR8,551
|
|
8
|
+
aixand_local_runtime/websocket.py,sha256=Fuo7kCGYXaHAIEeHPNOTbqSmfAMN8bN0KJL4_hsEwa0,2066
|
|
9
|
+
aixand_lib_local_runtime-0.1.0b0.dist-info/METADATA,sha256=eN5tu8I03hBpeg9778nErQxyHOE-VgDXpwpabZK1mdk,2723
|
|
10
|
+
aixand_lib_local_runtime-0.1.0b0.dist-info/WHEEL,sha256=16fc_9Rp854SLcdhaDEjr0u65p0e3FhZxihNgcnCE98,91
|
|
11
|
+
aixand_lib_local_runtime-0.1.0b0.dist-info/top_level.txt,sha256=hj2gEdHVhpDMNRQVRp_R_sC98cOweQ4PiI-esYjYVxQ,21
|
|
12
|
+
aixand_lib_local_runtime-0.1.0b0.dist-info/RECORD,,
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
aixand_local_runtime
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Local communication and process lifecycle utilities."""
|
|
2
|
+
|
|
3
|
+
from .ipc import (
|
|
4
|
+
WINDOWS,
|
|
5
|
+
LocalServer,
|
|
6
|
+
connect_local,
|
|
7
|
+
endpoint,
|
|
8
|
+
endpoint_exists,
|
|
9
|
+
open_local_connection,
|
|
10
|
+
start_local_server,
|
|
11
|
+
)
|
|
12
|
+
from .processes import process_start
|
|
13
|
+
from .signals import install_signals
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"WINDOWS",
|
|
17
|
+
"LocalServer",
|
|
18
|
+
"connect_local",
|
|
19
|
+
"endpoint",
|
|
20
|
+
"endpoint_exists",
|
|
21
|
+
"open_local_connection",
|
|
22
|
+
"start_local_server",
|
|
23
|
+
"process_start",
|
|
24
|
+
"install_signals",
|
|
25
|
+
]
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""macOS process-birth identities using the native libproc interface."""
|
|
2
|
+
|
|
3
|
+
import ctypes
|
|
4
|
+
from functools import cache
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class _ProcessInfo(ctypes.Structure):
|
|
8
|
+
# Public proc_bsdinfo ABI, including the microsecond process start time:
|
|
9
|
+
# https://github.com/apple-oss-distributions/xnu/blob/main/bsd/sys/proc_info.h
|
|
10
|
+
_fields_ = (
|
|
11
|
+
[
|
|
12
|
+
(name, ctypes.c_uint32)
|
|
13
|
+
for name in (
|
|
14
|
+
"flags",
|
|
15
|
+
"status",
|
|
16
|
+
"exit_status",
|
|
17
|
+
"pid",
|
|
18
|
+
"parent_pid",
|
|
19
|
+
"uid",
|
|
20
|
+
"gid",
|
|
21
|
+
"real_uid",
|
|
22
|
+
"real_gid",
|
|
23
|
+
"saved_uid",
|
|
24
|
+
"saved_gid",
|
|
25
|
+
"reserved",
|
|
26
|
+
)
|
|
27
|
+
]
|
|
28
|
+
+ [
|
|
29
|
+
("command", ctypes.c_char * 16),
|
|
30
|
+
("name", ctypes.c_char * 32),
|
|
31
|
+
]
|
|
32
|
+
+ [
|
|
33
|
+
(name, ctypes.c_uint32)
|
|
34
|
+
for name in ("files", "group", "job_count", "terminal", "terminal_group")
|
|
35
|
+
]
|
|
36
|
+
+ [
|
|
37
|
+
("nice", ctypes.c_int32),
|
|
38
|
+
("start_seconds", ctypes.c_uint64),
|
|
39
|
+
("start_microseconds", ctypes.c_uint64),
|
|
40
|
+
]
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@cache
|
|
45
|
+
def _query():
|
|
46
|
+
library = ctypes.CDLL("/usr/lib/libproc.dylib", use_errno=True)
|
|
47
|
+
query = library.proc_pidinfo
|
|
48
|
+
query.argtypes = [
|
|
49
|
+
ctypes.c_int,
|
|
50
|
+
ctypes.c_int,
|
|
51
|
+
ctypes.c_uint64,
|
|
52
|
+
ctypes.c_void_p,
|
|
53
|
+
ctypes.c_int,
|
|
54
|
+
]
|
|
55
|
+
query.restype = ctypes.c_int
|
|
56
|
+
return query
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def process_start(pid):
|
|
60
|
+
info = _ProcessInfo()
|
|
61
|
+
try:
|
|
62
|
+
size = _query()(pid, 3, 0, ctypes.byref(info), ctypes.sizeof(info))
|
|
63
|
+
except OSError:
|
|
64
|
+
return None
|
|
65
|
+
if size != ctypes.sizeof(info) or info.pid != pid or info.status == 5:
|
|
66
|
+
return None
|
|
67
|
+
return f"{info.start_seconds}:{info.start_microseconds}"
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""Windows process-birth identities; no filesystem policy."""
|
|
2
|
+
|
|
3
|
+
import ctypes as c
|
|
4
|
+
from ctypes import wintypes as w
|
|
5
|
+
|
|
6
|
+
kernel = c.WinDLL("kernel32", use_last_error=True)
|
|
7
|
+
kernel.OpenProcess.argtypes = [w.DWORD, w.BOOL, w.DWORD]
|
|
8
|
+
kernel.OpenProcess.restype = w.HANDLE
|
|
9
|
+
kernel.CloseHandle.argtypes = [w.HANDLE]
|
|
10
|
+
kernel.GetProcessTimes.argtypes = [w.HANDLE, *([c.POINTER(w.FILETIME)] * 4)]
|
|
11
|
+
kernel.WaitForSingleObject.argtypes = [w.HANDLE, w.DWORD]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _check(result):
|
|
15
|
+
if not result:
|
|
16
|
+
raise c.WinError(c.get_last_error())
|
|
17
|
+
return result
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def process_start(pid):
|
|
21
|
+
handle = kernel.OpenProcess(0x100000 | 0x1000, False, int(pid))
|
|
22
|
+
if not handle:
|
|
23
|
+
return None
|
|
24
|
+
try:
|
|
25
|
+
if kernel.WaitForSingleObject(handle, 0) != 258:
|
|
26
|
+
return None
|
|
27
|
+
values = [w.FILETIME() for _ in range(4)]
|
|
28
|
+
_check(kernel.GetProcessTimes(handle, *[c.byref(value) for value in values]))
|
|
29
|
+
return str(values[0].dwLowDateTime | values[0].dwHighDateTime << 32)
|
|
30
|
+
finally:
|
|
31
|
+
kernel.CloseHandle(handle)
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"""Local-only IPC: Unix sockets or authenticated Windows loopback.
|
|
2
|
+
|
|
3
|
+
Endpoint descriptors retain the aixand.local/1 wire format.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import asyncio
|
|
7
|
+
import contextlib
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
import secrets
|
|
12
|
+
import socket
|
|
13
|
+
import uuid
|
|
14
|
+
|
|
15
|
+
from aixand_filesystem import private_directory, private_path, verify_private
|
|
16
|
+
|
|
17
|
+
WINDOWS = os.name == "nt"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def endpoint_exists(path):
|
|
21
|
+
path = Path(path)
|
|
22
|
+
if not WINDOWS:
|
|
23
|
+
return path.is_socket()
|
|
24
|
+
try:
|
|
25
|
+
endpoint(path)
|
|
26
|
+
return True
|
|
27
|
+
except (OSError, ValueError):
|
|
28
|
+
return False
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def endpoint(path):
|
|
32
|
+
path = Path(path)
|
|
33
|
+
if WINDOWS:
|
|
34
|
+
verify_private(path)
|
|
35
|
+
with path.open("rb") as stream:
|
|
36
|
+
data = stream.read(4097)
|
|
37
|
+
if len(data) > 4096:
|
|
38
|
+
raise ValueError("Local endpoint descriptor is too large")
|
|
39
|
+
value = json.loads(data)
|
|
40
|
+
if (
|
|
41
|
+
value.get("schema") != "aixand.local/1"
|
|
42
|
+
or type(value.get("port")) is not int
|
|
43
|
+
or not 1024 <= value["port"] <= 65535
|
|
44
|
+
or not isinstance(value.get("token"), str)
|
|
45
|
+
or len(value["token"]) != 64
|
|
46
|
+
):
|
|
47
|
+
raise ValueError("Invalid local endpoint descriptor")
|
|
48
|
+
return value
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class LocalServer:
|
|
52
|
+
def __init__(self, server, path, token):
|
|
53
|
+
self.server, self.path, self.token = server, Path(path), token
|
|
54
|
+
|
|
55
|
+
def close(self):
|
|
56
|
+
self.server.close()
|
|
57
|
+
try:
|
|
58
|
+
if endpoint(self.path)["token"] == self.token:
|
|
59
|
+
self.path.unlink()
|
|
60
|
+
except (OSError, ValueError):
|
|
61
|
+
pass
|
|
62
|
+
|
|
63
|
+
async def wait_closed(self):
|
|
64
|
+
await self.server.wait_closed()
|
|
65
|
+
|
|
66
|
+
@property
|
|
67
|
+
def sockets(self):
|
|
68
|
+
return self.server.sockets
|
|
69
|
+
|
|
70
|
+
async def __aenter__(self):
|
|
71
|
+
return self
|
|
72
|
+
|
|
73
|
+
async def __aexit__(self, *_):
|
|
74
|
+
self.close()
|
|
75
|
+
await self.wait_closed()
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
async def start_local_server(callback, path=None, *, limit=65536):
|
|
79
|
+
if not WINDOWS:
|
|
80
|
+
return await asyncio.start_unix_server(callback, path=str(path), limit=limit)
|
|
81
|
+
path = Path(path)
|
|
82
|
+
private_directory(path.parent)
|
|
83
|
+
token = secrets.token_hex(32)
|
|
84
|
+
|
|
85
|
+
async def authenticate(reader, writer):
|
|
86
|
+
try:
|
|
87
|
+
async with asyncio.timeout(5):
|
|
88
|
+
raw = await reader.readuntil(b"\n")
|
|
89
|
+
if len(raw) > 256:
|
|
90
|
+
raise ValueError("Invalid local authentication")
|
|
91
|
+
auth = json.loads(raw)
|
|
92
|
+
if not secrets.compare_digest(str(auth.get("token", "")), token):
|
|
93
|
+
raise ValueError("Invalid local authentication")
|
|
94
|
+
await callback(reader, writer)
|
|
95
|
+
except (
|
|
96
|
+
OSError,
|
|
97
|
+
ValueError,
|
|
98
|
+
TimeoutError,
|
|
99
|
+
asyncio.IncompleteReadError,
|
|
100
|
+
asyncio.LimitOverrunError,
|
|
101
|
+
):
|
|
102
|
+
pass
|
|
103
|
+
finally:
|
|
104
|
+
writer.close()
|
|
105
|
+
with contextlib.suppress(OSError):
|
|
106
|
+
await writer.wait_closed()
|
|
107
|
+
|
|
108
|
+
server = await asyncio.start_server(authenticate, "127.0.0.1", 0, limit=limit)
|
|
109
|
+
temporary = path.with_name(path.name + "." + uuid.uuid4().hex)
|
|
110
|
+
try:
|
|
111
|
+
temporary.write_text(
|
|
112
|
+
json.dumps(
|
|
113
|
+
{
|
|
114
|
+
"schema": "aixand.local/1",
|
|
115
|
+
"port": server.sockets[0].getsockname()[1],
|
|
116
|
+
"token": token,
|
|
117
|
+
}
|
|
118
|
+
)
|
|
119
|
+
)
|
|
120
|
+
private_path(temporary)
|
|
121
|
+
os.replace(temporary, path)
|
|
122
|
+
except BaseException:
|
|
123
|
+
server.close()
|
|
124
|
+
await server.wait_closed()
|
|
125
|
+
temporary.unlink(missing_ok=True)
|
|
126
|
+
raise
|
|
127
|
+
return LocalServer(server, path, token)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
async def open_local_connection(path, *, limit=65536):
|
|
131
|
+
if not WINDOWS:
|
|
132
|
+
return await asyncio.open_unix_connection(str(path), limit=limit)
|
|
133
|
+
value = endpoint(path)
|
|
134
|
+
reader, writer = await asyncio.open_connection(
|
|
135
|
+
"127.0.0.1", value["port"], limit=limit
|
|
136
|
+
)
|
|
137
|
+
writer.write(json.dumps({"token": value["token"]}).encode() + b"\n")
|
|
138
|
+
await writer.drain()
|
|
139
|
+
return reader, writer
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def connect_local(path, *, timeout=30):
|
|
143
|
+
if not WINDOWS:
|
|
144
|
+
channel = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
145
|
+
try:
|
|
146
|
+
channel.settimeout(timeout)
|
|
147
|
+
channel.connect(str(path))
|
|
148
|
+
return channel
|
|
149
|
+
except BaseException:
|
|
150
|
+
channel.close()
|
|
151
|
+
raise
|
|
152
|
+
value = endpoint(path)
|
|
153
|
+
channel = socket.create_connection(("127.0.0.1", value["port"]), timeout)
|
|
154
|
+
try:
|
|
155
|
+
channel.sendall(json.dumps({"token": value["token"]}).encode() + b"\n")
|
|
156
|
+
return channel
|
|
157
|
+
except BaseException:
|
|
158
|
+
channel.close()
|
|
159
|
+
raise
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Opaque process-birth identities on the current host."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
WINDOWS = os.name == "nt"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def process_start(pid):
|
|
11
|
+
"""Return an opaque process-birth identity, or None when not identifiable.
|
|
12
|
+
|
|
13
|
+
Compare tokens only for the same PID on the same host. A zombie or exited
|
|
14
|
+
process has no live identity. Never use a PID alone to authorize a signal.
|
|
15
|
+
"""
|
|
16
|
+
try:
|
|
17
|
+
pid = int(pid)
|
|
18
|
+
except (TypeError, ValueError, OverflowError):
|
|
19
|
+
return None
|
|
20
|
+
if not 0 < pid <= 2147483647:
|
|
21
|
+
return None
|
|
22
|
+
if WINDOWS:
|
|
23
|
+
from ._windows_process import process_start as query
|
|
24
|
+
|
|
25
|
+
return query(pid)
|
|
26
|
+
if sys.platform == "darwin":
|
|
27
|
+
from ._macos_process import process_start as query
|
|
28
|
+
|
|
29
|
+
return query(pid)
|
|
30
|
+
try:
|
|
31
|
+
fields = Path(f"/proc/{pid}/stat").read_text().rsplit(")", 1)[1].split()
|
|
32
|
+
return None if fields[0] == "Z" else fields[19]
|
|
33
|
+
except (OSError, IndexError):
|
|
34
|
+
return None
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Install shutdown callbacks on the current asyncio event loop."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
WINDOWS = os.name == "nt"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def install_signals(callback):
|
|
10
|
+
import signal
|
|
11
|
+
|
|
12
|
+
loop = asyncio.get_running_loop()
|
|
13
|
+
numbers = (
|
|
14
|
+
(signal.SIGTERM, signal.SIGINT, signal.SIGBREAK)
|
|
15
|
+
if WINDOWS
|
|
16
|
+
else (signal.SIGTERM, signal.SIGINT)
|
|
17
|
+
)
|
|
18
|
+
for number in numbers:
|
|
19
|
+
if WINDOWS:
|
|
20
|
+
signal.signal(number, lambda *_: loop.call_soon_threadsafe(callback))
|
|
21
|
+
else:
|
|
22
|
+
loop.add_signal_handler(number, callback)
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""Authenticated local WebSockets, retaining the Unix socket API on POSIX."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
import secrets
|
|
7
|
+
import uuid
|
|
8
|
+
from .ipc import WINDOWS, LocalServer, endpoint
|
|
9
|
+
from aixand_filesystem import private_directory, private_path
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def connect(path, **kwargs):
|
|
13
|
+
if not WINDOWS:
|
|
14
|
+
from websockets.asyncio.client import unix_connect
|
|
15
|
+
|
|
16
|
+
return unix_connect(str(path), **kwargs)
|
|
17
|
+
from websockets.asyncio.client import connect as tcp_connect
|
|
18
|
+
|
|
19
|
+
value = endpoint(path)
|
|
20
|
+
return tcp_connect(
|
|
21
|
+
f"ws://127.0.0.1:{value['port']}",
|
|
22
|
+
additional_headers={"Authorization": "Bearer " + value["token"]},
|
|
23
|
+
proxy=None,
|
|
24
|
+
**kwargs,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
async def serve(callback, path, **kwargs):
|
|
29
|
+
if not WINDOWS:
|
|
30
|
+
from websockets.asyncio.server import unix_serve
|
|
31
|
+
|
|
32
|
+
return await unix_serve(callback, str(path), **kwargs)
|
|
33
|
+
from websockets.asyncio.server import serve as tcp_serve
|
|
34
|
+
from http import HTTPStatus
|
|
35
|
+
|
|
36
|
+
token = secrets.token_hex(32)
|
|
37
|
+
|
|
38
|
+
def authenticate(connection, request):
|
|
39
|
+
if not secrets.compare_digest(
|
|
40
|
+
request.headers.get("Authorization", ""), "Bearer " + token
|
|
41
|
+
):
|
|
42
|
+
return connection.respond(
|
|
43
|
+
HTTPStatus.UNAUTHORIZED, "Local authentication required\n"
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
server = await tcp_serve(
|
|
47
|
+
callback, "127.0.0.1", 0, process_request=authenticate, **kwargs
|
|
48
|
+
)
|
|
49
|
+
path = Path(path)
|
|
50
|
+
private_directory(path.parent)
|
|
51
|
+
temporary = path.with_name(path.name + "." + uuid.uuid4().hex)
|
|
52
|
+
try:
|
|
53
|
+
temporary.write_text(
|
|
54
|
+
json.dumps(
|
|
55
|
+
{
|
|
56
|
+
"schema": "aixand.local/1",
|
|
57
|
+
"port": server.sockets[0].getsockname()[1],
|
|
58
|
+
"token": token,
|
|
59
|
+
}
|
|
60
|
+
)
|
|
61
|
+
)
|
|
62
|
+
private_path(temporary)
|
|
63
|
+
os.replace(temporary, path)
|
|
64
|
+
except BaseException:
|
|
65
|
+
server.close()
|
|
66
|
+
await server.wait_closed()
|
|
67
|
+
temporary.unlink(missing_ok=True)
|
|
68
|
+
raise
|
|
69
|
+
return LocalServer(server, path, token)
|