Repository navigation
core: rework device state serialization/enumeration - #9200
Conversation
There was a problem hiding this comment.
That's right I noticed that too. But I wouldn't call that a feature ;)
There was a problem hiding this comment.
Yeah, it's buggy behaviour that we should fix. Hence I added it to the TODO list.
There was a problem hiding this comment.
"(void)" can be dropped I think as device_found_node() doesn't return any value now.
There was a problem hiding this comment.
does the use of "else" is really needed here ?
There was a problem hiding this comment.
no, it's not, but it has the benefit that it naturally provides us with a new {} environment, which we can then use to scope the udev device object with, so that the scope isn't longer than necessary.
There was a problem hiding this comment.
The alternative would be to give up on the strict adherence to C89 and allow (judicious) use of declarations of variables in the right place. Forcing the use of fake scopes just to do that doesn't really give us anything, because it's still possible to define a variable in the middle of a function, it's just more annoying to do so. Allowing declarations in the "natural" place has another big advantage: it's much easier to do "declaration is initialization", also reducing the potential for bugs. So all in all, I think we should just allow this, I'll prep a PR.
There was a problem hiding this comment.
ouch, it does not. I wonder why I didn't notice this. Yikes.
508656b to
9425adc
Compare
|
OK, I force pushed a new version now, fixing the rather glaring typo I made, that @fbuihuu found. I also dropped the pointless (void) casts, as suggested. PTAL! |
Also, proceed, as there's little we can do in this case.
let's drop the "now" argument, it's exactly what MANAGER_IS_RUNNING() returns, hence let's use that instead to simplify things. Moreover, let's change the add/found argument pair to become found/mask, which allows us to change multiple flags at the same time into opposing directions, which will be useful later on. Also, let's change the return type to void. It's a notifier call where callers will ignore the return value anyway as it is nothing actionable. Should not change behaviour.
Let's separate the validate step out. Also, let's update some comments which have long ceased to be true. No change in behaviour.
This is a simple wrapper around udev_device_new_from_devnum(), and uses the data from a struct stat's .st_rdev field to derive the udev_device object.
9425adc to
05f867d
Compare
There was a problem hiding this comment.
A comment explaining what found and mask mean would be very nice, because the naming of those variables is not helpful.
There was a problem hiding this comment.
The alternative would be to give up on the strict adherence to C89 and allow (judicious) use of declarations of variables in the right place. Forcing the use of fake scopes just to do that doesn't really give us anything, because it's still possible to define a variable in the middle of a function, it's just more annoying to do so. Allowing declarations in the "natural" place has another big advantage: it's much easier to do "declaration is initialization", also reducing the potential for bugs. So all in all, I think we should just allow this, I'll prep a PR.
There was a problem hiding this comment.
Is it worth it to define this a separate function when there's just on user?
There was a problem hiding this comment.
i mostly capt it so that it matches nicely with unit_coldplug() which kinda does the same stuff...
There was a problem hiding this comment.
The type of rvalue here has no effect on the enum type (unless the value is large enough to force a certain width, but that's not the case here), so "U" is just misleading.
There was a problem hiding this comment.
so, you are right, this won't have any effect on sizeof(DEVICE_FOUND_UDEV). However, it does have an effect on the shift-left sooner or later. "1 << 31" is undefined in C on 32bit systems, while "1U << 31" is well defined. Now, you might say that "that's a synthetic case, and we never have so many flags defined" and as soon as you shift left by 32 you need "1ULL << 32" anyway, but I think in general, signed-extended shift-left is really really weird, and I'd rather not do that if we are talking about a flags sets...
There was a problem hiding this comment.
maybe the lesson to learn is to maybe add a macro for this...
#define ENUM_BIT(x) (1LLU << (x))
enum {
X = ENUM_BIT(0),
Y = ENUM_BIT(1),
Z = ENUM_BIT(2),
}does that make sense, or does this become too magic?
There was a problem hiding this comment.
I think that's too much magic. Not in a case like this, but we often define masks and "_MAX" and such, and it'd be awkward to combine with a macro. So I think it's fine as is.
There was a problem hiding this comment.
Hmm, by the time I read this i already had done #9224... :-(
PTAL
(I'd be fine if this isn't merged, but I kinda like the unifying work this does, after all we currently use 1 <<, 1U <<, 1UL <<, 1ULL <<, and UINT64_C(1) << at different places all over the place...
There was a problem hiding this comment.
!FLAGS_SET(previous, DEVICE_FOUND_UDEV) ?
There was a problem hiding this comment.
This state was added in v238-539-g918e6f1c01 so it's OK to remove it without backwards compat.
There was a problem hiding this comment.
Hm, that seems unnecessarily harsh. If I'm reading the code correctly, we would accept such paths and escape them, so this change breaks backward compat. And people tend to autogenerate rules, so a non-normalized path is possible.
There was a problem hiding this comment.
this is not about deps, this is about adding alias names for units. For deps defined through udev rules we are tolerant, and will mangle. But I am sure if we accept device aliases we should be be very strict, as otherwise we might get into quite some ambiguity troubles
This is very similar to the existing unit method coldplug() but is called a bit later. The idea is that that coldplug() restores the unit state from before any prior reload/restart, i.e. puts the deserialized state in effect. The catchup() call is then called a bit later, to catch up with the system state for which we missed notifications while we were reloading. This is only really useful for mount, swap and device mount points were we should be careful to generate all missing unit state change events (i.e. call unit_notify() appropriately) for everything that happened while we were reloading.
No need to maintain a NULL marker at the end of the table if we know the size of the array anyway.
This reworks how device units are "powered on". This makes sure that any device changes that might have happened while we were restarting/reloading will be noticed properly. For that we'll now properly serialize/deserialize both the device unit state and the device "found" flags, and restore these initially in the "coldplug" phase of the manager deserialization. While enumerating the udev devices during startup we'll put together a new "found" flags mask, which we'll the switch to in the "catchup" phase of the manager deserialization, which follows the "coldplug" phase. Note that during the "coldplug" phase no unit state change events are generated, which is different for the "catchall" phase which will do that. Thus we correctly make sure that the deserialized state won't pull in new deps, but any device's change while we were reloading would. Fixes: systemd#8832 Replaces: systemd#8675
Make sure that whenever we enter "dead" state we unset the sysfs path, not just when we are changing to it due to "found" mask changes.
Previously the enumerate() callback defined for each unit type would do two things: 1. It would create perpetual units (i.e. -.slice, system.slice, -.mount and init.scope) 2. It would enumerate units from /proc/self/mountinfo, /proc/swaps and the udev database With this change these two parts are split into two seperate methods: enumerate() now only does #2, while enumerate_perpetual() is responsible for #1. Why make this change? Well, perpetual units should have a slightly different effect that those found through enumeration: as perpetual units should be up unconditionally, perpetually and thus never change state, they should also not pull in deps by their state changing, not even when the state is first set to active. Thus, their state is generally initialized through the per-device coldplug() method in similar fashion to the deserialized state from a previous run would be put into place. OTOH units found through regular enumeration should result in state changes (and thus pull in deps due to state changes), hence their state should be put in effect in the catchup() method instead. Hence, given this difference, let's also separate the functions, so that the rule is: 1. What is created in enumerate_perpetual() should be started in coldplug() 2. What is created in enumerate() should be started in catchup().
This is mostly fall-out from d1a1f0a, however some cases are older bugs. There might be more issues lurking, this was a simple grep for "%m" across the tree, with all lines removed that mention "errno" at all.
05f867d to
bb527e1
Compare
|
OK, force pushed a new version. Only changes are the suggested comment about the "DeviceFound" mask parameters, more FLAGS_SET() usage. I left the other stuff as it is, for the mentioned reasons |
|
OK, let's merge. I tested this a bit, and everything seems OK. |
|
All green! Yay! |
The refactor in systemd#9200 inadvertently dropped the variable assignment to traverse the device and its hierarchy in add_matches_for_device(). This was uncovered by Coverity (CID #1393310). Fix that by restoring the assignment. Tested: `journalctl /dev/sda` now filters journalctl output again.
The refactor in #9200 inadvertently dropped the variable assignment to traverse the device and its hierarchy in add_matches_for_device(). This was uncovered by Coverity (CID #1393310). Fix that by restoring the assignment. Tested: `journalctl /dev/sda` now filters journalctl output again.
This is a fix for #8832. It's a bit larger than I hoped for. But this was quite broken before...
@fbuihuu could you give this a whirl? does this fix things for you? If not, could you provide me with a reproducer?