@@ -135,8 +135,6 @@ static int determine_state_file(
135135
136136static int load_state (Context * c , const struct rfkill_event * event ) {
137137 _cleanup_free_ char * state_file = NULL , * value = NULL ;
138- struct rfkill_event we ;
139- ssize_t l ;
140138 int b , r ;
141139
142140 assert (c );
@@ -168,18 +166,19 @@ static int load_state(Context *c, const struct rfkill_event *event) {
168166 if (b < 0 )
169167 return log_error_errno (b , "Failed to parse state file %s: %m" , state_file );
170168
171- we = (struct rfkill_event ) {
172- .op = RFKILL_OP_CHANGE ,
169+ struct rfkill_event we = {
173170 .idx = event -> idx ,
171+ .op = RFKILL_OP_CHANGE ,
174172 .soft = b ,
175173 };
176174
177- l = write (c -> rfkill_fd , & we , sizeof ( we ) );
175+ ssize_t l = write (c -> rfkill_fd , & we , sizeof we );
178176 if (l < 0 )
179177 return log_error_errno (errno , "Failed to restore rfkill state for %i: %m" , event -> idx );
180- if (l != sizeof ( we ) )
178+ if (l != sizeof we )
181179 return log_error_errno (SYNTHETIC_ERRNO (EIO ),
182- "Couldn't write rfkill event structure, too short." );
180+ "Couldn't write rfkill event structure, too short (wrote %zd of %zu bytes)." ,
181+ l , sizeof we );
183182
184183 log_debug ("Loaded state '%s' from %s." , one_zero (b ), state_file );
185184 return 0 ;
@@ -306,43 +305,38 @@ static int run(int argc, char *argv[]) {
306305
307306 for (;;) {
308307 struct rfkill_event event ;
309- const char * type ;
310- ssize_t l ;
311308
312- l = read (c .rfkill_fd , & event , sizeof ( event ) );
309+ ssize_t l = read (c .rfkill_fd , & event , sizeof event );
313310 if (l < 0 ) {
314- if (errno == EAGAIN ) {
315-
316- if (!ready ) {
317- /* Notify manager that we are
318- * now finished with
319- * processing whatever was
320- * queued */
321- (void ) sd_notify (false, "READY=1" );
322- ready = true;
323- }
324-
325- /* Hang around for a bit, maybe there's more coming */
326-
327- r = fd_wait_for_event (c .rfkill_fd , POLLIN , EXIT_USEC );
328- if (r == - EINTR )
329- continue ;
330- if (r < 0 )
331- return log_error_errno (r , "Failed to poll() on device: %m" );
332- if (r > 0 )
333- continue ;
334-
335- log_debug ("All events read and idle, exiting." );
336- break ;
311+ if (errno != EAGAIN )
312+ return log_error_errno (errno , "Failed to read from /dev/rfkill: %m" );
313+
314+ if (!ready ) {
315+ /* Notify manager that we are now finished with processing whatever was
316+ * queued */
317+ (void ) sd_notify (false, "READY=1" );
318+ ready = true;
337319 }
338320
339- log_error_errno (errno , "Failed to read from /dev/rfkill: %m" );
321+ /* Hang around for a bit, maybe there's more coming */
322+
323+ r = fd_wait_for_event (c .rfkill_fd , POLLIN , EXIT_USEC );
324+ if (r == - EINTR )
325+ continue ;
326+ if (r < 0 )
327+ return log_error_errno (r , "Failed to poll() on device: %m" );
328+ if (r > 0 )
329+ continue ;
330+
331+ log_debug ("All events read and idle, exiting." );
332+ break ;
340333 }
341334
342335 if (l != RFKILL_EVENT_SIZE_V1 )
343- return log_error_errno (SYNTHETIC_ERRNO (EIO ), "Read event structure of invalid size." );
336+ return log_error_errno (SYNTHETIC_ERRNO (EIO ), "Read event structure of unexpected size (%zd, not %d)" ,
337+ l , RFKILL_EVENT_SIZE_V1 );
344338
345- type = rfkill_type_to_string (event .type );
339+ const char * type = rfkill_type_to_string (event .type );
346340 if (!type ) {
347341 log_debug ("An rfkill device of unknown type %i discovered, ignoring." , event .type );
348342 continue ;
0 commit comments