| 3130 | } |
| 3131 | |
| 3132 | static int systemd_timer_enable_unit(int enable, |
| 3133 | enum schedule_priority schedule, |
| 3134 | int minute) |
| 3135 | { |
| 3136 | char *cmd = NULL; |
| 3137 | struct child_process child = CHILD_PROCESS_INIT; |
| 3138 | const char *frequency = get_frequency(schedule); |
| 3139 | int ret; |
| 3140 | |
| 3141 | /* |
| 3142 | * Disabling the systemd unit while it is already disabled makes |
| 3143 | * systemctl print an error. |
| 3144 | * Let's ignore it since it means we already are in the expected state: |
| 3145 | * the unit is disabled. |
| 3146 | * |
| 3147 | * On the other hand, enabling a systemd unit which is already enabled |
| 3148 | * produces no error. |
| 3149 | */ |
| 3150 | if (!enable) { |
| 3151 | child.no_stderr = 1; |
| 3152 | } else if (systemd_timer_write_timer_file(schedule, minute)) { |
| 3153 | ret = -1; |
| 3154 | goto out; |
| 3155 | } |
| 3156 | |
| 3157 | get_schedule_cmd("systemctl", NULL, &cmd); |
| 3158 | strvec_split(&child.args, cmd); |
| 3159 | strvec_pushl(&child.args, "--user", enable ? "enable" : "disable", |
| 3160 | "--now", NULL); |
| 3161 | strvec_pushf(&child.args, SYSTEMD_UNIT_FORMAT, frequency, "timer"); |
| 3162 | |
| 3163 | if (start_command(&child)) { |
| 3164 | ret = error(_("failed to start systemctl")); |
| 3165 | goto out; |
| 3166 | } |
| 3167 | |
| 3168 | if (finish_command(&child)) { |
| 3169 | /* |
| 3170 | * Disabling an already disabled systemd unit makes |
| 3171 | * systemctl fail. |
| 3172 | * Let's ignore this failure. |
| 3173 | * |
| 3174 | * Enabling an enabled systemd unit doesn't fail. |
| 3175 | */ |
| 3176 | if (enable) { |
| 3177 | ret = error(_("failed to run systemctl")); |
| 3178 | goto out; |
| 3179 | } |
| 3180 | } |
| 3181 | |
| 3182 | ret = 0; |
| 3183 | |
| 3184 | out: |
| 3185 | free(cmd); |
| 3186 | return ret; |
| 3187 | } |
| 3188 | |
| 3189 | /* |
no test coverage detected