mirror of
https://github.com/clearlinux/swupd-client.git
synced 2026-09-07 14:11:52 +00:00
lock: Use a global variable in lock.c to keep lock file descriptor
For many different reasons it isn't possible to use swupd_init to initialize multiple state directories in the same execution of swupd. Lock function is also tied to a single swupd state directory because it's using state_dir global, so moving the lock_fd variable to lock.c won't reduce lock function versatility and it will make swupd_init and swupd_deinit code easier to read.
This commit is contained in:
committed by
Matthew Johnson
parent
bb662e6581
commit
e9722da0d2
+5
-7
@@ -481,7 +481,6 @@ out:
|
||||
*/
|
||||
int remove_bundles(char **bundles)
|
||||
{
|
||||
int lock_fd;
|
||||
int ret = 0;
|
||||
int ret_code = 0;
|
||||
int bad = 0;
|
||||
@@ -491,7 +490,7 @@ int remove_bundles(char **bundles)
|
||||
struct list *subs = NULL;
|
||||
bool mix_exists;
|
||||
|
||||
ret = swupd_init(&lock_fd);
|
||||
ret = swupd_init();
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "Failed updater initialization, exiting now.\n");
|
||||
return ret;
|
||||
@@ -511,7 +510,7 @@ int remove_bundles(char **bundles)
|
||||
ret);
|
||||
|
||||
free_subscriptions(&subs);
|
||||
swupd_deinit(lock_fd);
|
||||
swupd_deinit();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -648,7 +647,7 @@ int remove_bundles(char **bundles)
|
||||
}
|
||||
|
||||
free_subscriptions(&subs);
|
||||
swupd_deinit(lock_fd);
|
||||
swupd_deinit();
|
||||
|
||||
return ret_code;
|
||||
}
|
||||
@@ -1032,7 +1031,6 @@ out:
|
||||
*/
|
||||
int install_bundles_frontend(char **bundles)
|
||||
{
|
||||
int lock_fd;
|
||||
int ret = 0;
|
||||
int current_version;
|
||||
struct list *bundles_list = NULL;
|
||||
@@ -1043,7 +1041,7 @@ int install_bundles_frontend(char **bundles)
|
||||
bool mix_exists;
|
||||
|
||||
/* initialize swupd and get current version from OS */
|
||||
ret = swupd_init(&lock_fd);
|
||||
ret = swupd_init();
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "Failed updater initialization, exiting now.\n");
|
||||
return ret;
|
||||
@@ -1101,7 +1099,7 @@ clean_and_exit:
|
||||
|
||||
free_string(&bundles_list_str);
|
||||
free_subscriptions(&subs);
|
||||
swupd_deinit(lock_fd);
|
||||
swupd_deinit();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
+2
-3
@@ -322,8 +322,7 @@ int clean_main(int argc, char **argv)
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
int lock_fd = 0;
|
||||
ret = swupd_init(&lock_fd);
|
||||
ret = swupd_init();
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "Failed swupd initialization, exiting now.\n");
|
||||
return ret;
|
||||
@@ -354,7 +353,7 @@ int clean_main(int argc, char **argv)
|
||||
}
|
||||
|
||||
end:
|
||||
swupd_deinit(lock_fd);
|
||||
swupd_deinit();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
+2
-3
@@ -182,7 +182,6 @@ err:
|
||||
|
||||
int bundle_list_main(int argc, char **argv)
|
||||
{
|
||||
int lock_fd;
|
||||
int ret;
|
||||
|
||||
if (!parse_options(argc, argv)) {
|
||||
@@ -198,7 +197,7 @@ int bundle_list_main(int argc, char **argv)
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = swupd_init(&lock_fd);
|
||||
ret = swupd_init();
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "Error: Failed updater initialization. Exiting now\n");
|
||||
return ret;
|
||||
@@ -212,7 +211,7 @@ int bundle_list_main(int argc, char **argv)
|
||||
ret = list_installable_bundles();
|
||||
}
|
||||
|
||||
swupd_deinit(lock_fd);
|
||||
swupd_deinit();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
+9
-8
@@ -521,24 +521,26 @@ void free_file_data(void *data)
|
||||
free(file);
|
||||
}
|
||||
|
||||
void swupd_deinit(int lock_fd)
|
||||
void swupd_deinit(void)
|
||||
{
|
||||
terminate_signature();
|
||||
swupd_curl_deinit();
|
||||
free_globals();
|
||||
v_lockfile(lock_fd);
|
||||
v_lockfile();
|
||||
dump_file_descriptor_leaks();
|
||||
}
|
||||
|
||||
/* this function is intended to encapsulate the basic swupd
|
||||
* initializations for the majority of commands, that is:
|
||||
* - Make sure root is the user running the code
|
||||
* - Initialize log facility
|
||||
* - Get the lock
|
||||
* - Initialize globals
|
||||
* - initialize mounted directories
|
||||
* - Create necessary directories
|
||||
* - Get the lock
|
||||
* - Initialize curl
|
||||
* - Initialize signature checking
|
||||
*/
|
||||
int swupd_init(int *lock_fd)
|
||||
int swupd_init(void)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
@@ -566,8 +568,7 @@ int swupd_init(int *lock_fd)
|
||||
goto out_fds;
|
||||
}
|
||||
|
||||
*lock_fd = p_lockfile();
|
||||
if (*lock_fd < 0) {
|
||||
if (p_lockfile() < 0) {
|
||||
ret = ELOCK_FILE;
|
||||
goto out_fds;
|
||||
}
|
||||
@@ -587,7 +588,7 @@ int swupd_init(int *lock_fd)
|
||||
return ret;
|
||||
|
||||
out_close_lock:
|
||||
v_lockfile(*lock_fd);
|
||||
v_lockfile();
|
||||
out_fds:
|
||||
dump_file_descriptor_leaks();
|
||||
|
||||
|
||||
+11
-3
@@ -38,13 +38,19 @@
|
||||
#include "config.h"
|
||||
#include "swupd.h"
|
||||
|
||||
static int lock_fd = -1;
|
||||
|
||||
/* Try to get a write lock region on the lock file. Returns:
|
||||
* >= 0 an fcntl region lock'd fd or exits with a positive error
|
||||
* code and a recommended course of action for user.
|
||||
*/
|
||||
int p_lockfile(void)
|
||||
{
|
||||
int lock_fd, ret;
|
||||
if (lock_fd > 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret;
|
||||
pid_t pid = getpid();
|
||||
struct flock fl = {
|
||||
.l_type = F_WRLCK,
|
||||
@@ -89,7 +95,9 @@ int p_lockfile(void)
|
||||
}
|
||||
|
||||
/* closes lock fd and must not unlink lock file (else race allowed) */
|
||||
void v_lockfile(int fd)
|
||||
void v_lockfile(void)
|
||||
{
|
||||
close(fd);
|
||||
if (lock_fd > 0) {
|
||||
close(lock_fd);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -856,7 +856,6 @@ static int download_manifests(struct manifest **MoM, struct list **subs)
|
||||
int search_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 0;
|
||||
int lock_fd = 0;
|
||||
struct manifest *MoM = NULL;
|
||||
struct list *subs = NULL;
|
||||
|
||||
@@ -864,7 +863,7 @@ int search_main(int argc, char **argv)
|
||||
return EINVALID_OPTION;
|
||||
}
|
||||
|
||||
ret = swupd_init(&lock_fd);
|
||||
ret = swupd_init();
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "Failed swupd initialization, exiting now.\n");
|
||||
return ret;
|
||||
@@ -899,6 +898,6 @@ int search_main(int argc, char **argv)
|
||||
clean_exit:
|
||||
free_manifest(MoM);
|
||||
free_subscriptions(&subs);
|
||||
swupd_deinit(lock_fd);
|
||||
swupd_deinit();
|
||||
return ret;
|
||||
}
|
||||
|
||||
+3
-3
@@ -350,14 +350,14 @@ extern void record_fds(void);
|
||||
|
||||
/* lock.c */
|
||||
int p_lockfile(void);
|
||||
void v_lockfile(int fd);
|
||||
void v_lockfile(void);
|
||||
|
||||
/* helpers.c */
|
||||
extern int swupd_rm(const char *path);
|
||||
extern int rm_bundle_file(const char *bundle);
|
||||
extern void print_manifest_files(struct manifest *m);
|
||||
extern void swupd_deinit(int lock_fd);
|
||||
extern int swupd_init(int *lock_fd);
|
||||
extern void swupd_deinit(void);
|
||||
extern int swupd_init(void);
|
||||
extern void string_or_die(char **strp, const char *fmt, ...);
|
||||
char *strdup_or_die(const char *const str);
|
||||
extern void free_string(char **s);
|
||||
|
||||
+2
-3
@@ -212,7 +212,6 @@ static int main_update()
|
||||
struct list *current_subs = NULL;
|
||||
struct list *latest_subs = NULL;
|
||||
int ret;
|
||||
int lock_fd;
|
||||
int retries = 0;
|
||||
int timeout = 10;
|
||||
struct timespec ts_start, ts_stop; // For main swupd update time
|
||||
@@ -224,7 +223,7 @@ static int main_update()
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
ret = swupd_init(&lock_fd);
|
||||
ret = swupd_init();
|
||||
if (ret != 0) {
|
||||
/* being here means we already close log by a previously caught error */
|
||||
fprintf(stderr, "Updater failed to initialize, exiting now.\n");
|
||||
@@ -548,7 +547,7 @@ clean_curl:
|
||||
* we need and the clean helps us prevent cache bloat. */
|
||||
clean_statedir(false, false);
|
||||
free_subscriptions(&latest_subs);
|
||||
swupd_deinit(lock_fd);
|
||||
swupd_deinit();
|
||||
|
||||
if (nonpack > 0) {
|
||||
printf("%i files were not in a pack\n", nonpack);
|
||||
|
||||
+2
-3
@@ -698,7 +698,6 @@ int verify_main(int argc, char **argv)
|
||||
{
|
||||
struct manifest *official_manifest = NULL;
|
||||
int ret;
|
||||
int lock_fd;
|
||||
int retries = 0;
|
||||
int timeout = 10;
|
||||
struct list *subs = NULL;
|
||||
@@ -713,7 +712,7 @@ int verify_main(int argc, char **argv)
|
||||
assert(argc >= 0);
|
||||
assert(argv != NULL);
|
||||
|
||||
ret = swupd_init(&lock_fd);
|
||||
ret = swupd_init();
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "Failed verify initialization, exiting now.\n");
|
||||
goto clean_args_and_exit;
|
||||
@@ -1020,7 +1019,7 @@ clean_and_exit:
|
||||
|
||||
print_time_stats(×);
|
||||
free_subscriptions(&subs);
|
||||
swupd_deinit(lock_fd);
|
||||
swupd_deinit();
|
||||
|
||||
clean_args_and_exit:
|
||||
if (picky_whitelist) {
|
||||
|
||||
Reference in New Issue
Block a user