machine: Retrieve an unique identifier if one is known.#1095
Open
agatti wants to merge 1 commit intomicropython:masterfrom
Open
machine: Retrieve an unique identifier if one is known.#1095agatti wants to merge 1 commit intomicropython:masterfrom
agatti wants to merge 1 commit intomicropython:masterfrom
Conversation
This commit adds a proper implementation for "machine.unique_id" for selected systems, as opposed to returning a fixed value in every case. On a Unix system there are several incompatible ways to retrieve something that can be called an unique machine identifier. However, the only semblance of a specification comes from freedesktop.org, which says that a file called "/etc/machine-id" has to exist and must contain a 32-digits long hexadecimal identifier. Given that the current specification is the DBus unique identifier retrieval mechanism made official, if the identifier file is not found the code will attempt to read the file provided by DBus. These changes only apply to Linux and recent BSD systems. On other systems the old, fixed value for the machine identifier will be returned instead - to avoid breaking compatibility with existing code. The specification used in this commit is available at https://www.freedesktop.org/software/systemd/man/latest/machine-id.html. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
1ba9531 to
12410fd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a proper implementation for
machine.unique_idfor selected systems, as opposed to returning a fixed value in every case.On a Unix system there are several incompatible ways to retrieve something that can be called an unique machine identifier. However, the only semblance of a specification comes from freedesktop.org, which says that a file called
/etc/machine-idhas to exist and must contain a 32-digits long hexadecimal identifier - thus having a 128 bits unique identifier value.Given that the current specification is the DBus unique identifier retrieval mechanism made official, if the identifier file is not found the code will attempt to read the file provided by DBus.
These changes only apply to Linux and recent BSD systems. On other systems the old, fixed value for the machine identifier will be returned instead, to avoid breaking compatibility with existing code.
The specification used in this commit is available at https://www.freedesktop.org/software/systemd/man/latest/machine-id.html.
The manifest's version was bumped up by 0.0.1 since these changes only affect the behaviour of the module without making changes to the existing API.
Testing
This was tested on current MicroPython master, by adding
require("machine")toports/unix/variants/manifest.pyand rebuilding thestandardvariant of the interpreter.Then the output of
import machine; machine.unique_id()was checked against the contents of/etc/machine-idon the host system. The fallback was tested by changing/etcinto something else, and I/O error passthrough was tested by adding a directory that is not accessible by the running user to the front of the list of paths to check.Trade-offs and Alternatives
These changes add 198 bytes to the
machinemodule. Things could be made smaller by removing the identifier validation and skipping the DBus fallback.Preliminary identifier value validation in theory could be removed, since the "unhexlification" step will raise an exception if the input data is not a series of hexadecimal digits. The current behaviour is to ignore the data read from the current machine identifier file and try the next one in the list. If stopping the identification process on unexpected data at any point is acceptable rather than gracefully handling the condition, this could shrink down by 43 bytes.
However, since there is an existing specification and a known alternative path for the same data, maybe it's not a bad idea to keep those two bits of code in.
Generative AI
I did not use generative AI tools when creating this PR.