[systemd-commits] 4 commits - shell-completion/bash shell-completion/systemd-zsh-completion.zsh src/shared src/systemctl src/udev TODO

Zbigniew Jędrzejewski-Szmek zbyszek at kemper.freedesktop.org
Wed Apr 10 16:52:59 PDT 2013


 TODO                                        |    3 
 shell-completion/bash/systemctl             |   33 +-
 shell-completion/systemd-zsh-completion.zsh |   31 +-
 src/shared/util.h                           |    7 
 src/systemctl/systemctl.c                   |  111 ++++++++-
 src/udev/ata_id/ata_id.c                    |  315 ++++++++++++----------------
 6 files changed, 287 insertions(+), 213 deletions(-)

New commits:
commit c2e09812e9c088f1582ce542e43f6b5743b2e4fe
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Wed Apr 10 19:31:58 2013 -0400

    shell-completion: show failed units as candidates for start
    
    Also show disabled units as candidates for reenable,
    since it works and one may want to do enable-or-reenable
    in one line.

diff --git a/TODO b/TODO
index 369fb78..0e58468 100644
--- a/TODO
+++ b/TODO
@@ -1,6 +1,5 @@
 Bugfixes:
-* systemctl completion doesn't want to start failed systemd-tmpfiles-setup.service
-  systemctl --system show -p Fr<TAB> default.target doesn't show anything
+* systemctl --system show -p Fr<TAB> default.target doesn't show anything
 
 * check systemd-tmpfiles for selinux context hookup for mknod(), symlink() and similar
 
diff --git a/shell-completion/bash/systemctl b/shell-completion/bash/systemctl
index b019d6a..aba76f6 100644
--- a/shell-completion/bash/systemctl
+++ b/shell-completion/bash/systemctl
@@ -46,7 +46,7 @@ __get_all_units      () { __systemctl $1 list-units --all \
 __get_active_units   () { __systemctl $1 list-units       \
         | { while read -r a b; do echo " $a"; done; }; }
 __get_inactive_units () { __systemctl $1 list-units --all \
-        | { while read -r a b c d; do [[ $c == "inactive" ]] && echo " $a"; done; }; }
+        | { while read -r a b c d; do [[ $c == "inactive" || $c == "failed " ]] && echo " $a"; done; }; }
 __get_failed_units   () { __systemctl $1 list-units       \
         | { while read -r a b c d; do [[ $c == "failed"   ]] && echo " $a"; done; }; }
 __get_enabled_units  () { __systemctl $1 list-unit-files  \
@@ -109,8 +109,9 @@ _systemctl () {
 
         local -A VERBS=(
                 [ALL_UNITS]='is-active is-failed is-enabled status show mask preset'
-            [ENABLED_UNITS]='disable reenable'
+            [ENABLED_UNITS]='disable'
            [DISABLED_UNITS]='enable'
+        [REENABLABLE_UNITS]='reenable'
              [FAILED_UNITS]='reset-failed'
           [STARTABLE_UNITS]='start'
           [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart'
@@ -149,6 +150,10 @@ _systemctl () {
         elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
                 comps=$( __get_disabled_units $mode )
 
+        elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
+                comps=$( __get_disabled_units $mode;
+                         __get_enabled_units $mode )
+
         elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
                 comps=$( __filter_units_by_property $mode CanStart yes \
                       $( __get_inactive_units $mode \
diff --git a/shell-completion/systemd-zsh-completion.zsh b/shell-completion/systemd-zsh-completion.zsh
index 52a5fdb..a54e9d7 100644
--- a/shell-completion/systemd-zsh-completion.zsh
+++ b/shell-completion/systemd-zsh-completion.zsh
@@ -455,7 +455,7 @@ _filter_units_by_property() {
 }
 
 _systemctl_active_units()  {_sys_active_units=(  $(__systemctl list-units          | { while read a b; do echo " $a"; done; }) )}
-_systemctl_inactive_units(){_sys_inactive_units=($(__systemctl list-units --all    | { while read a b c d; do [[ $c == "inactive" ]] && echo " $a"; done; }) )}
+_systemctl_inactive_units(){_sys_inactive_units=($(__systemctl list-units --all    | { while read a b c d; do [[ $c == "inactive" || $c == "failed" ]] && echo " $a"; done; }) )}
 _systemctl_failed_units()  {_sys_failed_units=(  $(__systemctl list-units --failed | { while read a b; do echo " $a"; done; }) )}
 _systemctl_enabled_units() {_sys_enabled_units=( $(__systemctl list-unit-files     | { while read a b; do [[ $b == "enabled" ]] && echo " $a"; done; }) )}
 _systemctl_disabled_units(){_sys_disabled_units=($(__systemctl list-unit-files     | { while read a b; do [[ $b == "disabled" ]] && echo " $a"; done; }) )}
@@ -475,7 +475,8 @@ for fun in disable reenable ; do
   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
   {
     _systemctl_enabled_units
-    compadd "$@" -a - _sys_enabled_units
+    _systemctl_disabled_units
+    compadd "$@" -a - _sys_enabled_units _sys_disabled_units
   }
 done
 

commit 79c1638303142b81e1b72dd5bde0c656ccf19999
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Wed Apr 10 19:06:00 2013 -0400

    shell-completion: replace printf with echo
    
    It is faster to use a bash built-in, then to invoke an external
    program. The problem of unit names starting with a dash is solved
    by prepending a space. Spaces are ignored anyway.
    
    For zsh, replace echo "$unit", which is vulnerable to dashes,
    with echo " $unit".

diff --git a/shell-completion/bash/systemctl b/shell-completion/bash/systemctl
index ce46d50..b019d6a 100644
--- a/shell-completion/bash/systemctl
+++ b/shell-completion/bash/systemctl
@@ -36,25 +36,25 @@ __filter_units_by_property () {
             <(__systemctl $mode show --property "$property" -- "${units[@]}")
         for ((i=0; $i < ${#units[*]}; i++)); do
                 if [[ "${props[i]}" = "$property=$value" ]]; then
-                        printf "%s\n" "${units[i]}"
+                        echo " ${units[i]}"
                 fi
         done
 }
 
 __get_all_units      () { __systemctl $1 list-units --all \
-        | { while read -r a b; do printf "%s\n" "$a"; done; }; }
+        | { while read -r a b; do echo " $a"; done; }; }
 __get_active_units   () { __systemctl $1 list-units       \
-        | { while read -r a b; do printf "%s\n" "$a"; done; }; }
+        | { while read -r a b; do echo " $a"; done; }; }
 __get_inactive_units () { __systemctl $1 list-units --all \
-        | { while read -r a b c d; do [[ $c == "inactive" ]] && printf "%s\n" "$a"; done; }; }
+        | { while read -r a b c d; do [[ $c == "inactive" ]] && echo " $a"; done; }; }
 __get_failed_units   () { __systemctl $1 list-units       \
-        | { while read -r a b c d; do [[ $c == "failed"   ]] && printf "%s\n" "$a"; done; }; }
+        | { while read -r a b c d; do [[ $c == "failed"   ]] && echo " $a"; done; }; }
 __get_enabled_units  () { __systemctl $1 list-unit-files  \
-        | { while read -r a b c  ; do [[ $b == "enabled"  ]] && printf "%s\n" "$a"; done; }; }
+        | { while read -r a b c  ; do [[ $b == "enabled"  ]] && echo " $a"; done; }; }
 __get_disabled_units () { __systemctl $1 list-unit-files  \
-        | { while read -r a b c  ; do [[ $b == "disabled" ]] && printf "%s\n" "$a"; done; }; }
+        | { while read -r a b c  ; do [[ $b == "disabled" ]] && echo " $a"; done; }; }
 __get_masked_units   () { __systemctl $1 list-unit-files  \
-        | { while read -r a b c  ; do [[ $b == "masked"   ]] && printf "%s\n" "$a"; done; }; }
+        | { while read -r a b c  ; do [[ $b == "masked"   ]] && echo " $a"; done; }; }
 
 _systemctl () {
         local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
@@ -153,14 +153,14 @@ _systemctl () {
                 comps=$( __filter_units_by_property $mode CanStart yes \
                       $( __get_inactive_units $mode \
                         | while read -r line; do \
-                                [[ "$line" =~ \.(device|snapshot)$ ]] || printf "%s\n" "$line"; \
+                                [[ "$line" =~ \.(device|snapshot)$ ]] || echo " $line"; \
                         done ))
 
         elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
                 comps=$( __filter_units_by_property $mode CanStart yes \
                       $( __get_all_units $mode \
                         | while read -r line; do \
-                                [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || printf "%s\n" "$line"; \
+                                [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || echo " $line"; \
                         done ))
 
         elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
@@ -185,15 +185,15 @@ _systemctl () {
                 comps=''
 
         elif __contains_word "$verb" ${VERBS[JOBS]}; then
-                comps=$( __systemctl $mode list-jobs | { while read -r a b; do printf "%s\n" "$a"; done; } )
+                comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
 
         elif __contains_word "$verb" ${VERBS[SNAPSHOTS]}; then
                 comps=$( __systemctl $mode list-units --type snapshot --full --all \
-                        | { while read -r a b; do printf "%s\n" "$a"; done; } )
+                        | { while read -r a b; do echo " $a"; done; } )
 
         elif __contains_word "$verb" ${VERBS[ENVS]}; then
                 comps=$( __systemctl $mode show-environment \
-                    | while read -r line; do printf "%s\n" "${line%%=*}=";done )
+                    | while read -r line; do echo " ${line%%=*}=";done )
                 compopt -o nospace
 
         elif __contains_word "$verb" ${VERBS[FILE]}; then
diff --git a/shell-completion/systemd-zsh-completion.zsh b/shell-completion/systemd-zsh-completion.zsh
index 46a6a19..52a5fdb 100644
--- a/shell-completion/systemd-zsh-completion.zsh
+++ b/shell-completion/systemd-zsh-completion.zsh
@@ -416,7 +416,7 @@ _systemctl_all_units()
   if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS ) &&
     ! _retrieve_cache SYS_ALL_UNITS;
   then
-    _sys_all_units=( $(__systemctl list-units --all | { while read a b; do echo "$a"; done; }) )
+    _sys_all_units=( $(__systemctl list-units --all | { while read a b; do echo " $a"; done; }) )
     _store_cache SYS_ALL_UNITS _sys_all_units
   fi
 }
@@ -429,7 +429,7 @@ _systemctl_really_all_units()
   if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS ) &&
     ! _retrieve_cache SYS_REALLY_ALL_UNITS;
   then
-    all_unit_files=( $(__systemctl list-unit-files | { while read a b; do echo "$a"; done; }) )
+    all_unit_files=( $(__systemctl list-unit-files | { while read a b; do echo " $a"; done; }) )
     _systemctl_all_units
     really_all_units=($_sys_all_units $all_unit_files)
     _sys_really_all_units=(${(u)really_all_units})
@@ -449,17 +449,17 @@ _filter_units_by_property() {
     unit=${units[i]}
     prop=${(f)"$(_call_program units "$service show --no-pager --property="$property" ${unit} 2>/dev/null")"}
     if [[ "${prop}" = "$property=$value" ]]; then
-      echo "${unit}"
+      echo " ${unit}"
     fi
   done
 }
 
-_systemctl_active_units()  {_sys_active_units=(  $(__systemctl list-units          | { while read a b; do echo "$a"; done; }) )}
-_systemctl_inactive_units(){_sys_inactive_units=($(__systemctl list-units --all    | { while read a b c d; do [[ $c == "inactive" ]] && echo "$a"; done; }) )}
-_systemctl_failed_units()  {_sys_failed_units=(  $(__systemctl list-units --failed | { while read a b; do echo "$a"; done; }) )}
-_systemctl_enabled_units() {_sys_enabled_units=( $(__systemctl list-unit-files     | { while read a b; do [[ $b == "enabled" ]] && echo "$a"; done; }) )}
-_systemctl_disabled_units(){_sys_disabled_units=($(__systemctl list-unit-files     | { while read a b; do [[ $b == "disabled" ]] && echo "$a"; done; }) )}
-_systemctl_masked_units()  {_sys_masked_units=(  $(__systemctl list-unit-files     | { while read a b; do [[ $b == "masked" ]] && echo "$a"; done; }) )}
+_systemctl_active_units()  {_sys_active_units=(  $(__systemctl list-units          | { while read a b; do echo " $a"; done; }) )}
+_systemctl_inactive_units(){_sys_inactive_units=($(__systemctl list-units --all    | { while read a b c d; do [[ $c == "inactive" ]] && echo " $a"; done; }) )}
+_systemctl_failed_units()  {_sys_failed_units=(  $(__systemctl list-units --failed | { while read a b; do echo " $a"; done; }) )}
+_systemctl_enabled_units() {_sys_enabled_units=( $(__systemctl list-unit-files     | { while read a b; do [[ $b == "enabled" ]] && echo " $a"; done; }) )}
+_systemctl_disabled_units(){_sys_disabled_units=($(__systemctl list-unit-files     | { while read a b; do [[ $b == "disabled" ]] && echo " $a"; done; }) )}
+_systemctl_masked_units()  {_sys_masked_units=(  $(__systemctl list-unit-files     | { while read a b; do [[ $b == "masked" ]] && echo " $a"; done; }) )}
 
 # Completion functions for ALL_UNITS
 for fun in is-active is-failed is-enabled status show mask preset ; do
@@ -535,7 +535,7 @@ for fun in restart reload-or-restart ; do
     _systemctl_all_units
     compadd "$@" - $( _filter_units_by_property CanStart yes \
       ${_sys_all_units[*]} | while read line; do \
-      [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || echo "$line"; \
+      [[ "$line" =~ \.(device|snapshot|socket|timer)$ ]] || echo " $line"; \
       done )
   }
 done
@@ -572,7 +572,7 @@ for fun in set-environment unset-environment ; do
     fi
 
     compadd "$@" ${suf} - $(systemctl show-environment \
-      | while read line; do echo "${line%%\=}";done )
+      | while read line; do echo " ${line%%\=}";done )
   }
 done
 
@@ -638,9 +638,9 @@ _journal_fields() {
 }
 
 
-_loginctl_all_sessions(){_sys_all_sessions=($(loginctl list-sessions | { while read a b; do echo "$a"; done; }) )}
-_loginctl_all_users()   {_sys_all_users=(   $(loginctl list-users    | { while read a b; do echo "$a"; done; }) )}
-_loginctl_all_seats()   {_sys_all_seats=(   $(loginctl list-seats    | { while read a b; do echo "$a"; done; }) )}
+_loginctl_all_sessions(){_sys_all_sessions=($(loginctl list-sessions | { while read a b; do echo " $a"; done; }) )}
+_loginctl_all_users()   {_sys_all_users=(   $(loginctl list-users    | { while read a b; do echo " $a"; done; }) )}
+_loginctl_all_seats()   {_sys_all_seats=(   $(loginctl list-seats    | { while read a b; do echo " $a"; done; }) )}
 
 # Completion functions for SESSIONS
 for fun in session-status show-session activate lock-session unlock-session terminate-session kill-session ; do

commit 75add28aa17678fbf5b10947027efe7ac75d113d
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Wed Apr 10 18:30:19 2013 -0400

    systemctl: ellipsize job list only when necessary, highlight running
    
    I was debugging systemd waiting on a missing disk, and noticed
    that the job listing could use some polishing. Jobs that are
    actually running are highlighted, so it's easier to see what
    very actually waiting for.
    
    Also, the needed widths are precalculated, to use available columns
    more ecomically.

diff --git a/src/shared/util.h b/src/shared/util.h
index 3885123..a8e962e 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -647,4 +647,11 @@ static inline bool logind_running(void) {
         return access("/run/systemd/seats/", F_OK) >= 0;
 }
 
+static inline unsigned decimal_str_max(unsigned x) {
+        unsigned ans = 1;
+        while (x /= 10)
+                ans ++;
+        return ans;
+}
+
 int unlink_noerrno(const char *path);
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index fd9f580..af7ecd7 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -895,13 +895,84 @@ static int list_dependencies(DBusConnection *bus, char **args) {
         return list_dependencies_one(bus, u, 0, NULL, 0);
 }
 
+struct job_info {
+        uint32_t id;
+        char *name, *type, *state;
+};
+
+static void list_jobs_print(struct job_info* jobs, size_t n) {
+        size_t i;
+        struct job_info *j;
+        const char *on, *off;
+        bool shorten = false;
+
+        assert(n == 0 || jobs);
+
+        if (n == 0) {
+                on = ansi_highlight_green(true);
+                off = ansi_highlight_green(false);
+
+                printf("%sNo jobs running.%s\n", on, off);
+                return;
+        }
+
+        pager_open_if_enabled();
+
+        {
+                /* JOB UNIT TYPE STATE */
+                unsigned l0 = 3, l1 = 4, l2 = 4, l3 = 5;
+
+                for (i = 0, j = jobs; i < n; i++, j++) {
+                        assert(j->name && j->type && j->state);
+                        l0 = MAX(l0, decimal_str_max(j->id));
+                        l1 = MAX(l1, strlen(j->name));
+                        l2 = MAX(l2, strlen(j->type));
+                        l3 = MAX(l3, strlen(j->state));
+                }
+
+                if (!arg_full && l0 + 1 + l1 + l2 + 1 + l3 > columns()) {
+                        l1 = MAX(33u, columns() - l0 - l2 - l3 - 3);
+                        shorten = true;
+                }
+
+                if (on_tty())
+                        printf("%*s %-*s %-*s %-*s\n",
+                               l0, "JOB",
+                               l1, "UNIT",
+                               l2, "TYPE",
+                               l3, "STATE");
+
+                for (i = 0, j = jobs; i < n; i++, j++) {
+                        char _cleanup_free_ *e = NULL;
+
+                        if (streq(j->state, "running")) {
+                                on = ansi_highlight(true);
+                                off = ansi_highlight(false);
+                        } else
+                                on = off = "";
+
+                        e = shorten ? ellipsize(j->name, l1, 33) : NULL;
+                        printf("%*u %s%-*s%s %-*s %s%-*s%s\n",
+                               l0, j->id,
+                               on, l1, e ? e : j->name, off,
+                               l2, j->type,
+                               on, l3, j->state, off);
+                }
+        }
+
+        on = ansi_highlight(true);
+        off = ansi_highlight(false);
+
+        if (on_tty())
+                printf("\n%s%zu jobs listed%s.\n", on, n, off);
+}
+
 static int list_jobs(DBusConnection *bus, char **args) {
         _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
         DBusMessageIter iter, sub, sub2;
-        unsigned k = 0;
         int r;
-
-        pager_open_if_enabled();
+        struct job_info *jobs = NULL;
+        size_t size = 0, used = 0;
 
         r = bus_method_call_with_reply(
                         bus,
@@ -924,13 +995,9 @@ static int list_jobs(DBusConnection *bus, char **args) {
 
         dbus_message_iter_recurse(&iter, &sub);
 
-        if (on_tty())
-                printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
-
         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
                 const char *name, *type, *state, *job_path, *unit_path;
                 uint32_t id;
-                char _cleanup_free_ *e = NULL;
 
                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
                         log_error("Failed to parse reply.");
@@ -946,19 +1013,37 @@ static int list_jobs(DBusConnection *bus, char **args) {
                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
                     bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
                         log_error("Failed to parse reply.");
-                        return -EIO;
+                        r = -EIO;
+                        goto finish;
                 }
 
-                e = arg_full ? NULL : ellipsize(name, 25, 33);
-                printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
+                if (!greedy_realloc((void**) &jobs, &size,
+                                    sizeof(struct job_info) * (used + 1))) {
+                        r = log_oom();
+                        goto finish;
+                }
 
-                k++;
+                jobs[used++] = (struct job_info) { id,
+                                                   strdup(name),
+                                                   strdup(type),
+                                                   strdup(state) };
+                if (!jobs[used-1].name || !jobs[used-1].type || !jobs[used-1].state) {
+                        r = log_oom();
+                        goto finish;
+                }
 
                 dbus_message_iter_next(&sub);
         }
 
-        if (on_tty())
-                printf("\n%u jobs listed.\n", k);
+        list_jobs_print(jobs, used);
+
+ finish:
+        while (used--) {
+                free(jobs[used].name);
+                free(jobs[used].type);
+                free(jobs[used].state);
+        }
+        free(jobs);
 
         return 0;
 }

commit b8a2b0f76b133524224d384641e124b7f6c5b9af
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Wed Apr 10 18:58:44 2013 -0400

    udev/ata_id: use initalization instead of explicit zeroing

diff --git a/src/udev/ata_id/ata_id.c b/src/udev/ata_id/ata_id.c
index 488fed4..4d30fd8 100644
--- a/src/udev/ata_id/ata_id.c
+++ b/src/udev/ata_id/ata_id.c
@@ -51,65 +51,58 @@ static int disk_scsi_inquiry_command(int      fd,
                                      void    *buf,
                                      size_t   buf_len)
 {
-        struct sg_io_v4 io_v4;
-        uint8_t cdb[6];
-        uint8_t sense[32];
+        uint8_t cdb[6] = {
+                /*
+                 * INQUIRY, see SPC-4 section 6.4
+                 */
+                [0] = 0x12,                /* OPERATION CODE: INQUIRY */
+                [3] = (buf_len >> 8),      /* ALLOCATION LENGTH */
+                [4] = (buf_len & 0xff),
+        };
+        uint8_t sense[32] = {};
+        struct sg_io_v4 io_v4 = {
+                .guard = 'Q',
+                .protocol = BSG_PROTOCOL_SCSI,
+                .subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD,
+                .request_len = sizeof(cdb),
+                .request = (uintptr_t) cdb,
+                .max_response_len = sizeof(sense),
+                .response = (uintptr_t) sense,
+                .din_xfer_len = buf_len,
+                .din_xferp = (uintptr_t) buf,
+                .timeout = COMMAND_TIMEOUT_MSEC,
+        };
         int ret;
 
-        /*
-         * INQUIRY, see SPC-4 section 6.4
-         */
-        memset(cdb, 0, sizeof(cdb));
-        cdb[0] = 0x12;                         /* OPERATION CODE: INQUIRY */
-        cdb[3] = (buf_len >> 8);         /* ALLOCATION LENGTH */
-        cdb[4] = (buf_len & 0xff);
-
-        memset(sense, 0, sizeof(sense));
-
-        memset(&io_v4, 0, sizeof(struct sg_io_v4));
-        io_v4.guard = 'Q';
-        io_v4.protocol = BSG_PROTOCOL_SCSI;
-        io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD;
-        io_v4.request_len = sizeof (cdb);
-        io_v4.request = (uintptr_t) cdb;
-        io_v4.max_response_len = sizeof (sense);
-        io_v4.response = (uintptr_t) sense;
-        io_v4.din_xfer_len = buf_len;
-        io_v4.din_xferp = (uintptr_t) buf;
-        io_v4.timeout = COMMAND_TIMEOUT_MSEC;
-
         ret = ioctl(fd, SG_IO, &io_v4);
         if (ret != 0) {
                 /* could be that the driver doesn't do version 4, try version 3 */
                 if (errno == EINVAL) {
-                        struct sg_io_hdr io_hdr;
-
-                        memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
-                        io_hdr.interface_id = 'S';
-                        io_hdr.cmdp = (unsigned char*) cdb;
-                        io_hdr.cmd_len = sizeof (cdb);
-                        io_hdr.dxferp = buf;
-                        io_hdr.dxfer_len = buf_len;
-                        io_hdr.sbp = sense;
-                        io_hdr.mx_sb_len = sizeof (sense);
-                        io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
-                        io_hdr.timeout = COMMAND_TIMEOUT_MSEC;
+                        struct sg_io_hdr io_hdr = {
+                                .interface_id = 'S',
+                                .cmdp = (unsigned char*) cdb,
+                                .cmd_len = sizeof (cdb),
+                                .dxferp = buf,
+                                .dxfer_len = buf_len,
+                                .sbp = sense,
+                                .mx_sb_len = sizeof(sense),
+                                .dxfer_direction = SG_DXFER_FROM_DEV,
+                                .timeout = COMMAND_TIMEOUT_MSEC,
+                        };
 
                         ret = ioctl(fd, SG_IO, &io_hdr);
                         if (ret != 0)
-                                goto out;
+                                return ret;
 
                         /* even if the ioctl succeeds, we need to check the return value */
                         if (!(io_hdr.status == 0 &&
                               io_hdr.host_status == 0 &&
                               io_hdr.driver_status == 0)) {
                                 errno = EIO;
-                                ret = -1;
-                                goto out;
+                                return -1;
                         }
-                } else {
-                        goto out;
-                }
+                } else
+                        return ret;
         }
 
         /* even if the ioctl succeeds, we need to check the return value */
@@ -117,172 +110,156 @@ static int disk_scsi_inquiry_command(int      fd,
               io_v4.transport_status == 0 &&
               io_v4.driver_status == 0)) {
                 errno = EIO;
-                ret = -1;
-                goto out;
+                return -1;
         }
 
- out:
-        return ret;
+        return 0;
 }
 
 static int disk_identify_command(int          fd,
                                  void         *buf,
                                  size_t          buf_len)
 {
-        struct sg_io_v4 io_v4;
-        uint8_t cdb[12];
-        uint8_t sense[32];
-        uint8_t *desc = sense+8;
+        uint8_t cdb[12] = {
+                /*
+                 * ATA Pass-Through 12 byte command, as described in
+                 *
+                 *  T10 04-262r8 ATA Command Pass-Through
+                 *
+                 * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
+                 */
+                [0] = 0xa1,     /* OPERATION CODE: 12 byte pass through */
+                [1] = 4 << 1,   /* PROTOCOL: PIO Data-in */
+                [2] = 0x2e,     /* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */
+                [3] = 0,        /* FEATURES */
+                [4] = 1,        /* SECTORS */
+                [5] = 0,        /* LBA LOW */
+                [6] = 0,        /* LBA MID */
+                [7] = 0,        /* LBA HIGH */
+                [8] = 0 & 0x4F, /* SELECT */
+                [9] = 0xEC,     /* Command: ATA IDENTIFY DEVICE */
+        };
+        uint8_t sense[32] = {};
+        uint8_t *desc = sense + 8;
+        struct sg_io_v4 io_v4 = {
+                .guard = 'Q',
+                .protocol = BSG_PROTOCOL_SCSI,
+                .subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD,
+                .request_len = sizeof(cdb),
+                .request = (uintptr_t) cdb,
+                .max_response_len = sizeof(sense),
+                .response = (uintptr_t) sense,
+                .din_xfer_len = buf_len,
+                .din_xferp = (uintptr_t) buf,
+                .timeout = COMMAND_TIMEOUT_MSEC,
+        };
         int ret;
 
-        /*
-         * ATA Pass-Through 12 byte command, as described in
-         *
-         *  T10 04-262r8 ATA Command Pass-Through
-         *
-         * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
-         */
-        memset(cdb, 0, sizeof(cdb));
-        cdb[0] = 0xa1;                        /* OPERATION CODE: 12 byte pass through */
-        cdb[1] = 4 << 1;                /* PROTOCOL: PIO Data-in */
-        cdb[2] = 0x2e;                        /* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */
-        cdb[3] = 0;                        /* FEATURES */
-        cdb[4] = 1;                        /* SECTORS */
-        cdb[5] = 0;                        /* LBA LOW */
-        cdb[6] = 0;                        /* LBA MID */
-        cdb[7] = 0;                        /* LBA HIGH */
-        cdb[8] = 0 & 0x4F;                /* SELECT */
-        cdb[9] = 0xEC;                        /* Command: ATA IDENTIFY DEVICE */;
-        memset(sense, 0, sizeof(sense));
-
-        memset(&io_v4, 0, sizeof(struct sg_io_v4));
-        io_v4.guard = 'Q';
-        io_v4.protocol = BSG_PROTOCOL_SCSI;
-        io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD;
-        io_v4.request_len = sizeof (cdb);
-        io_v4.request = (uintptr_t) cdb;
-        io_v4.max_response_len = sizeof (sense);
-        io_v4.response = (uintptr_t) sense;
-        io_v4.din_xfer_len = buf_len;
-        io_v4.din_xferp = (uintptr_t) buf;
-        io_v4.timeout = COMMAND_TIMEOUT_MSEC;
-
         ret = ioctl(fd, SG_IO, &io_v4);
         if (ret != 0) {
                 /* could be that the driver doesn't do version 4, try version 3 */
                 if (errno == EINVAL) {
-                        struct sg_io_hdr io_hdr;
-
-                        memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
-                        io_hdr.interface_id = 'S';
-                        io_hdr.cmdp = (unsigned char*) cdb;
-                        io_hdr.cmd_len = sizeof (cdb);
-                        io_hdr.dxferp = buf;
-                        io_hdr.dxfer_len = buf_len;
-                        io_hdr.sbp = sense;
-                        io_hdr.mx_sb_len = sizeof (sense);
-                        io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
-                        io_hdr.timeout = COMMAND_TIMEOUT_MSEC;
+                        struct sg_io_hdr io_hdr = {
+                                .interface_id = 'S',
+                                .cmdp = (unsigned char*) cdb,
+                                .cmd_len = sizeof (cdb),
+                                .dxferp = buf,
+                                .dxfer_len = buf_len,
+                                .sbp = sense,
+                                .mx_sb_len = sizeof (sense),
+                                .dxfer_direction = SG_DXFER_FROM_DEV,
+                                .timeout = COMMAND_TIMEOUT_MSEC,
+                        };
 
                         ret = ioctl(fd, SG_IO, &io_hdr);
                         if (ret != 0)
-                                goto out;
-                } else {
-                        goto out;
-                }
+                                return ret;
+                } else
+                        return ret;
         }
 
         if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c)) {
                 errno = EIO;
-                ret = -1;
-                goto out;
+                return -1;
         }
 
- out:
-        return ret;
+        return 0;
 }
 
 static int disk_identify_packet_device_command(int          fd,
                                                void         *buf,
                                                size_t          buf_len)
 {
-        struct sg_io_v4 io_v4;
-        uint8_t cdb[16];
-        uint8_t sense[32];
-        uint8_t *desc = sense+8;
+        uint8_t cdb[16] = {
+                /*
+                 * ATA Pass-Through 16 byte command, as described in
+                 *
+                 *  T10 04-262r8 ATA Command Pass-Through
+                 *
+                 * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
+                 */
+                [0] = 0x85,   /* OPERATION CODE: 16 byte pass through */
+                [1] = 4 << 1, /* PROTOCOL: PIO Data-in */
+                [2] = 0x2e,   /* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */
+                [3] = 0,      /* FEATURES */
+                [4] = 0,      /* FEATURES */
+                [5] = 0,      /* SECTORS */
+                [6] = 1,      /* SECTORS */
+                [7] = 0,      /* LBA LOW */
+                [8] = 0,      /* LBA LOW */
+                [9] = 0,      /* LBA MID */
+                [10] = 0,     /* LBA MID */
+                [11] = 0,     /* LBA HIGH */
+                [12] = 0,     /* LBA HIGH */
+                [13] = 0,     /* DEVICE */
+                [14] = 0xA1,  /* Command: ATA IDENTIFY PACKET DEVICE */
+                [15] = 0,     /* CONTROL */
+        };
+        uint8_t sense[32] = {};
+        uint8_t *desc = sense + 8;
+        struct sg_io_v4 io_v4 = {
+                .guard = 'Q',
+                .protocol = BSG_PROTOCOL_SCSI,
+                .subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD,
+                .request_len = sizeof (cdb),
+                .request = (uintptr_t) cdb,
+                .max_response_len = sizeof (sense),
+                .response = (uintptr_t) sense,
+                .din_xfer_len = buf_len,
+                .din_xferp = (uintptr_t) buf,
+                .timeout = COMMAND_TIMEOUT_MSEC,
+        };
         int ret;
 
-        /*
-         * ATA Pass-Through 16 byte command, as described in
-         *
-         *  T10 04-262r8 ATA Command Pass-Through
-         *
-         * from http://www.t10.org/ftp/t10/document.04/04-262r8.pdf
-         */
-        memset(cdb, 0, sizeof(cdb));
-        cdb[0] = 0x85;                        /* OPERATION CODE: 16 byte pass through */
-        cdb[1] = 4 << 1;                /* PROTOCOL: PIO Data-in */
-        cdb[2] = 0x2e;                        /* OFF_LINE=0, CK_COND=1, T_DIR=1, BYT_BLOK=1, T_LENGTH=2 */
-        cdb[3] = 0;                        /* FEATURES */
-        cdb[4] = 0;                        /* FEATURES */
-        cdb[5] = 0;                        /* SECTORS */
-        cdb[6] = 1;                        /* SECTORS */
-        cdb[7] = 0;                        /* LBA LOW */
-        cdb[8] = 0;                        /* LBA LOW */
-        cdb[9] = 0;                        /* LBA MID */
-        cdb[10] = 0;                        /* LBA MID */
-        cdb[11] = 0;                        /* LBA HIGH */
-        cdb[12] = 0;                        /* LBA HIGH */
-        cdb[13] = 0;                        /* DEVICE */
-        cdb[14] = 0xA1;                        /* Command: ATA IDENTIFY PACKET DEVICE */;
-        cdb[15] = 0;                        /* CONTROL */
-        memset(sense, 0, sizeof(sense));
-
-        memset(&io_v4, 0, sizeof(struct sg_io_v4));
-        io_v4.guard = 'Q';
-        io_v4.protocol = BSG_PROTOCOL_SCSI;
-        io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD;
-        io_v4.request_len = sizeof (cdb);
-        io_v4.request = (uintptr_t) cdb;
-        io_v4.max_response_len = sizeof (sense);
-        io_v4.response = (uintptr_t) sense;
-        io_v4.din_xfer_len = buf_len;
-        io_v4.din_xferp = (uintptr_t) buf;
-        io_v4.timeout = COMMAND_TIMEOUT_MSEC;
-
         ret = ioctl(fd, SG_IO, &io_v4);
         if (ret != 0) {
                 /* could be that the driver doesn't do version 4, try version 3 */
                 if (errno == EINVAL) {
-                        struct sg_io_hdr io_hdr;
-
-                        memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
-                        io_hdr.interface_id = 'S';
-                        io_hdr.cmdp = (unsigned char*) cdb;
-                        io_hdr.cmd_len = sizeof (cdb);
-                        io_hdr.dxferp = buf;
-                        io_hdr.dxfer_len = buf_len;
-                        io_hdr.sbp = sense;
-                        io_hdr.mx_sb_len = sizeof (sense);
-                        io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
-                        io_hdr.timeout = COMMAND_TIMEOUT_MSEC;
+                        struct sg_io_hdr io_hdr = {
+                                .interface_id = 'S',
+                                .cmdp = (unsigned char*) cdb,
+                                .cmd_len = sizeof (cdb),
+                                .dxferp = buf,
+                                .dxfer_len = buf_len,
+                                .sbp = sense,
+                                .mx_sb_len = sizeof (sense),
+                                .dxfer_direction = SG_DXFER_FROM_DEV,
+                                .timeout = COMMAND_TIMEOUT_MSEC,
+                        };
 
                         ret = ioctl(fd, SG_IO, &io_hdr);
                         if (ret != 0)
-                                goto out;
-                } else {
-                        goto out;
-                }
+                                return ret;
+                } else
+                        return ret;
         }
 
         if (!(sense[0] == 0x72 && desc[0] == 0x9 && desc[1] == 0x0c)) {
                 errno = EIO;
-                ret = -1;
-                goto out;
+                return -1;
         }
 
- out:
-        return ret;
+        return 0;
 }
 
 /**
@@ -348,9 +325,9 @@ static void disk_identify_fixup_uint16 (uint8_t identify[512], unsigned int offs
  * non-zero with errno set.
  */
 static int disk_identify(struct udev *udev,
-                         int               fd,
-                         uint8_t      out_identify[512],
-                         int              *out_is_packet_device)
+                         int fd,
+                         uint8_t out_identify[512],
+                         int *out_is_packet_device)
 {
         int ret;
         uint8_t inquiry_buf[36];
@@ -360,7 +337,7 @@ static int disk_identify(struct udev *udev,
         int is_packet_device;
 
         /* init results */
-        memset(out_identify, '\0', 512);
+        zero(out_identify);
         is_packet_device = 0;
 
         /* If we were to use ATA PASS_THROUGH (12) on an ATAPI device



More information about the systemd-commits mailing list