aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/rfs.c
diff options
context:
space:
mode:
authorSimon Busch <morphis@gravedo.de>2012-08-09 08:30:19 +0200
committerSimon Busch <morphis@gravedo.de>2012-08-09 08:30:19 +0200
commitd4f41d7dc21a8e977f78a3fb88c5b5774e7d33ed (patch)
treea8d810e8974785789af5ff5a4081686b49d696a6 /samsung-ipc/rfs.c
parent81660e380835e6fe6d23c92f5da8fbeeaffa39ea (diff)
downloadexternal_libsamsung-ipc-d4f41d7dc21a8e977f78a3fb88c5b5774e7d33ed.zip
external_libsamsung-ipc-d4f41d7dc21a8e977f78a3fb88c5b5774e7d33ed.tar.gz
external_libsamsung-ipc-d4f41d7dc21a8e977f78a3fb88c5b5774e7d33ed.tar.bz2
rfs: fix warnings about ignored return results from std. library functions
Diffstat (limited to 'samsung-ipc/rfs.c')
-rw-r--r--samsung-ipc/rfs.c237
1 files changed, 185 insertions, 52 deletions
diff --git a/samsung-ipc/rfs.c b/samsung-ipc/rfs.c
index a416999..c6e58f8 100644
--- a/samsung-ipc/rfs.c
+++ b/samsung-ipc/rfs.c
@@ -151,9 +151,10 @@ void nv_data_md5_compute(void *data_p, int size, char *secret, void *hash)
void nv_data_md5_generate(struct ipc_client *client)
{
uint8_t nv_data_md5_hash[MD5_DIGEST_LENGTH];
- char *nv_data_md5_hash_string;
- void *nv_data_p;
+ char *nv_data_md5_hash_string = NULL;
+ void *nv_data_p = NULL;
int fd;
+ int rc;
ipc_client_log(client, "nv_data_md5_generate: enter\n");
@@ -172,20 +173,28 @@ void nv_data_md5_generate(struct ipc_client *client)
ipc_client_log(client, "nv_data_md5_generate: new MD5 hash is %s\n", nv_data_md5_hash_string);
ipc_client_log(client, "nv_data_md5_generate: writing MD5 hash\n");
+
/* Write the MD5 hash in nv_data.bin.md5. */
fd = open(nv_data_md5_path(client), O_RDWR | O_CREAT | O_TRUNC, 0644);
-
if (fd < 0)
{
ipc_client_log(client, "nv_data_md5_generate: fd open failed\n");
goto exit;
}
- write(fd, nv_data_md5_hash_string, MD5_STRING_SIZE);
+ rc = write(fd, nv_data_md5_hash_string, MD5_STRING_SIZE);
+ if (rc < 0)
+ {
+ ipc_client_log(client, "nv_data_md5_generate: failed to write MD5 hash to file\n");
+ close(fd);
+ goto exit;
+ }
+
close(fd);
exit:
- free(nv_data_md5_hash_string);
+ if (nv_data_md5_hash_string != NULL)
+ free(nv_data_md5_hash_string);
ipc_client_log(client, "nv_data_md5_generate: exit\n");
}
@@ -193,13 +202,13 @@ exit:
void nv_data_backup_create(struct ipc_client *client)
{
uint8_t nv_data_md5_hash[MD5_DIGEST_LENGTH];
- char *nv_data_md5_hash_string;
- char *nv_data_md5_hash_read;
+ char *nv_data_md5_hash_string = NULL;
+ char *nv_data_md5_hash_read = NULL;
int nv_data_write_tries = 0;
struct stat nv_stat;
- void *nv_data_p;
- void *nv_data_bak_p;
+ void *nv_data_p = NULL;
+ void *nv_data_bak_p = NULL;
uint8_t data;
int fd;
@@ -244,8 +253,21 @@ void nv_data_backup_create(struct ipc_client *client)
md5hash2string(nv_data_md5_hash_string, nv_data_md5_hash);
/* Read the stored backup file MD5 hash. */
- fd=open(nv_data_md5_path(client), O_RDONLY);
- read(fd, nv_data_md5_hash_read, MD5_STRING_SIZE);
+ fd = open(nv_data_md5_path(client), O_RDONLY);
+ if (fd < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_create: failed to openstored backup file with MD5 hash\n");
+ goto exit;
+ }
+
+ rc = read(fd, nv_data_md5_hash_read, MD5_STRING_SIZE);
+ if (rc < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_create: failed to read MD5 hash from backup file\n");
+ close(fd);
+ goto exit;
+ }
+
close(fd);
/* Add 0x0 to end the string: not sure this is always part of the file. */
@@ -259,8 +281,20 @@ void nv_data_backup_create(struct ipc_client *client)
ipc_client_log(client, "nv_data_backup_create: MD5 hash mismatch on backup file\n");
ipc_client_log(client, "nv_data_backup_create: Consider the computed one as correct\n");
- fd=open(nv_data_md5_path(client), O_WRONLY);
- read(fd, nv_data_md5_hash_string, MD5_STRING_SIZE);
+ fd = open(nv_data_md5_path(client), O_WRONLY);
+ if (fd < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_create: failed to open file with MD5 hash of data file\n");
+ goto exit;
+ }
+
+ rc = read(fd, nv_data_md5_hash_string, MD5_STRING_SIZE);
+ if (rc < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_create: failed to read MD5 hash for data file from file\n");
+ goto exit;
+ }
+
close(fd);
/*
@@ -279,7 +313,7 @@ nv_data_backup_create_write:
{
ipc_client_log(client, "nv_data_backup_create: .nv_data.bak write try #%d\n", nv_data_write_tries + 1);
- fd=open(nv_data_bak_path(client), O_RDWR | O_CREAT | O_TRUNC, 0644);
+ fd = open(nv_data_bak_path(client), O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
{
ipc_client_log(client, "nv_data_backup_create: negative fd while opening /efs/.nv_data.bak, error: %s\n", strerror(errno));
@@ -308,14 +342,15 @@ nv_data_backup_create_write:
}
/* Read the newly-written .nv_data.bak. */
- nv_data_bak_p=ipc_client_file_read(client, nv_data_bak_path(client),
+ nv_data_bak_p = ipc_client_file_read(client, nv_data_bak_path(client),
nv_data_size(client), nv_data_chunk_size(client));
/* Compute the MD5 hash for nv_data.bin. */
nv_data_md5_compute(nv_data_bak_p, nv_data_size(client), nv_data_secret(client), nv_data_md5_hash);
md5hash2string(nv_data_md5_hash_string, nv_data_md5_hash);
- free(nv_data_bak_p);
+ if (nv_data_bak_p != NULL)
+ free(nv_data_bak_p);
ipc_client_log(client, "nv_data_backup_create: written file computed MD5: %s read MD5: %s\n",
nv_data_md5_hash_string, nv_data_md5_hash_read);
@@ -330,20 +365,48 @@ nv_data_backup_create_write:
}
/* Write the MD5 hash in .nv_data.bak.md5. */
- fd=open(nv_data_md5_bak_path(client), O_WRONLY | O_CREAT | O_TRUNC, 0644);
- write(fd, nv_data_md5_hash_read, MD5_STRING_SIZE);
+ fd = open(nv_data_md5_bak_path(client), O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ if (fd < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_create: failed to open MD5 hash file\n");
+ goto exit;
+ }
+
+ rc = write(fd, nv_data_md5_hash_read, MD5_STRING_SIZE);
+ if (rc < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_create: failed to write MD5 hash to file\n");
+ close(fd);
+ goto exit;
+ }
close(fd);
/* Write the correct .nv_state. */
- fd=open(nv_state_path(client), O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ fd = open(nv_state_path(client), O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ if (fd < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_create: failed to open NV state file\n");
+ goto exit;
+ }
+
data='1';
- write(fd, &data, sizeof(data));
+ rc = write(fd, &data, sizeof(data));
+ if (rc < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_create: failed to write state of NV data\n");
+ close(fd);
+ goto exit;
+ }
+
close(fd);
exit:
- free(nv_data_p);
- free(nv_data_md5_hash_string);
- free(nv_data_md5_hash_read);
+ if (nv_data_p != NULL)
+ free(nv_data_p);
+ if (nv_data_md5_hash_string != NULL)
+ free(nv_data_md5_hash_string);
+ if (nv_data_md5_hash_read)
+ free(nv_data_md5_hash_read);
ipc_client_log(client, "nv_data_backup_create: exit\n");
}
@@ -351,13 +414,13 @@ exit:
void nv_data_backup_restore(struct ipc_client *client)
{
uint8_t nv_data_md5_hash[MD5_DIGEST_LENGTH];
- char *nv_data_md5_hash_string;
- char *nv_data_md5_hash_read;
+ char *nv_data_md5_hash_string = NULL;
+ char *nv_data_md5_hash_read = NULL;
int nv_data_write_tries = 0;
struct stat nv_stat;
- void *nv_data_p;
- void *nv_data_bak_p;
+ void *nv_data_p = NULL;
+ void *nv_data_bak_p = NULL;
uint8_t data;
int fd;
@@ -407,7 +470,14 @@ void nv_data_backup_restore(struct ipc_client *client)
/* Read the stored backup file MD5 hash. */
fd=open(nv_data_md5_bak_path(client), O_RDONLY);
- read(fd, nv_data_md5_hash_read, MD5_STRING_SIZE);
+ rc = read(fd, nv_data_md5_hash_read, MD5_STRING_SIZE);
+ if (rc < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_restore: Failed to read md5 hash for stored back file\n");
+ close(fd);
+ goto exit;
+ }
+
close(fd);
/* Add 0x0 to end the string: not sure this is always part of the file. */
@@ -421,8 +491,21 @@ void nv_data_backup_restore(struct ipc_client *client)
ipc_client_log(client, "nv_data_backup_restore: MD5 hash mismatch on backup file\n");
ipc_client_log(client, "nv_data_backup_restore: Consider the computed one as correct\n");
- fd=open(nv_data_md5_bak_path(client), O_WRONLY);
- read(fd, nv_data_md5_hash_string, MD5_STRING_SIZE);
+ fd = open(nv_data_md5_bak_path(client), O_WRONLY);
+ if (fd < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_restore: failed to open MD5 hash backup file\n");
+ goto exit;
+ }
+
+ rc = read(fd, nv_data_md5_hash_string, MD5_STRING_SIZE);
+ if (rc < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_restore: failed to read MD5 hash from backup file\n");
+ close(fd);
+ goto exit;
+ }
+
close(fd);
/*
@@ -467,7 +550,6 @@ nv_data_backup_restore_write:
ipc_client_log(client, "nv_data_backup_restore: writing the backup to nv_data.bin failed too many times\n");
unlink(nv_data_path(client));
goto exit;
-
}
/* Read the newly-written nv_data.bin. */
@@ -478,7 +560,11 @@ nv_data_backup_restore_write:
nv_data_md5_compute(nv_data_p, nv_data_size(client), nv_data_secret(client), nv_data_md5_hash);
md5hash2string(nv_data_md5_hash_string, nv_data_md5_hash);
- free(nv_data_p);
+ if (nv_data_p != NULL)
+ {
+ free(nv_data_p);
+ nv_data_p = NULL;
+ }
ipc_client_log(client, "nv_data_backup_restore: written file computed MD5: %s read MD5: %s\n",
nv_data_md5_hash_string, nv_data_md5_hash_read);
@@ -493,29 +579,58 @@ nv_data_backup_restore_write:
}
/* Write the MD5 hash in nv_data.bin.md5. */
- fd=open(nv_data_md5_path(client), O_WRONLY | O_CREAT | O_TRUNC, 0644);
- write(fd, nv_data_md5_hash_read, MD5_STRING_SIZE);
+ fd = open(nv_data_md5_path(client), O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ if (fd < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_restore: failed to open file with MD5 hash\n");
+ goto exit;
+ }
+
+ rc = write(fd, nv_data_md5_hash_read, MD5_STRING_SIZE);
+ if (rc < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_restore: failed to write MD5 hash to file\n");
+ close(fd);
+ goto exit;
+ }
close(fd);
/* Write the correct .nv_state. */
- fd=open(nv_state_path(client), O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ fd = open(nv_state_path(client), O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ if (fd < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_restore: failed to open NV state file\n");
+ goto exit;
+ }
+
data='1';
- write(fd, &data, sizeof(data));
+ rc = write(fd, &data, sizeof(data));
+ if (rc < 0)
+ {
+ ipc_client_log(client, "nv_data_backup_restore: failed to write state to file\n");
+ close(fd);
+ goto exit;
+ }
+
close(fd);
exit:
- free(nv_data_bak_p);
- free(nv_data_md5_hash_string);
- free(nv_data_md5_hash_read);
+ if (nv_data_bak_p != NULL)
+ free(nv_data_bak_p);
+ if (nv_data_md5_hash_string != NULL)
+ free(nv_data_md5_hash_string);
+ if (nv_data_md5_hash_read != NULL)
+ free(nv_data_md5_hash_read);
ipc_client_log(client, "nv_data_backup_restore: exit\n");
}
-void nv_data_check(struct ipc_client *client)
+int nv_data_check(struct ipc_client *client)
{
struct stat nv_stat;
int nv_state_fd=-1;
int nv_state=0;
+ int rc;
ipc_client_log(client, "nv_data_check: enter\n");
@@ -542,7 +657,7 @@ void nv_data_check(struct ipc_client *client)
{
ipc_client_log(client, "nv_data_check: .nv_data.bak or .nv_data.bak.md5 missing\n");
nv_data_backup_create(client);
- }
+ }
nv_state_fd=open(nv_state_path(client), O_RDONLY);
@@ -552,7 +667,12 @@ void nv_data_check(struct ipc_client *client)
nv_data_backup_restore(client);
}
- read(nv_state_fd, &nv_state, sizeof(nv_state));
+ rc = read(nv_state_fd, &nv_state, sizeof(nv_state));
+ if (rc < 0)
+ {
+ ipc_client_log(client, "nv_data_check: couldn't read state of NV item from file\n");
+ return -1;
+ }
close(nv_state_fd);
@@ -564,17 +684,19 @@ void nv_data_check(struct ipc_client *client)
ipc_client_log(client, "nv_data_check: everything should be alright\n");
ipc_client_log(client, "nv_data_check: exit\n");
+
+ return 0;
}
-void nv_data_md5_check(struct ipc_client *client)
+int nv_data_md5_check(struct ipc_client *client)
{
struct stat nv_stat;
uint8_t nv_data_md5_hash[MD5_DIGEST_LENGTH];
- char *nv_data_md5_hash_string;
- char *nv_data_md5_hash_read;
- void *nv_data_p;
-
+ char *nv_data_md5_hash_string = NULL;
+ char *nv_data_md5_hash_read = NULL;
+ void *nv_data_p = NULL;
int fd;
+ int rc;
uint8_t *data_p;
ipc_client_log(client, "nv_data_md5_check: enter\n");
@@ -598,7 +720,12 @@ void nv_data_md5_check(struct ipc_client *client)
fd=open(nv_data_md5_path(client), O_RDONLY);
/* Read the md5 stored in the file. */
- read(fd, nv_data_md5_hash_read, MD5_STRING_SIZE);
+ rc = read(fd, nv_data_md5_hash_read, MD5_STRING_SIZE);
+ if (rc < 0)
+ {
+ ipc_client_log(client, "nv_data_md5_check: Can't read md5 hash from file\n");
+ return -1;
+ }
/* Add 0x0 to end the string: not sure this is part of the file. */
nv_data_md5_hash_read[MD5_STRING_SIZE - 1]='\0';
@@ -612,10 +739,14 @@ void nv_data_md5_check(struct ipc_client *client)
nv_data_backup_restore(client);
}
- free(nv_data_md5_hash_string);
- free(nv_data_md5_hash_read);
+ if (nv_data_md5_hash_string != NULL)
+ free(nv_data_md5_hash_string);
+ if (nv_data_md5_hash_read != NULL)
+ free(nv_data_md5_hash_read);
ipc_client_log(client, "nv_data_md5_check: exit\n");
+
+ return 0;
}
int nv_data_read(struct ipc_client *client, int offset, int length, char *buf)
@@ -635,7 +766,8 @@ int nv_data_read(struct ipc_client *client, int offset, int length, char *buf)
return -1;
}
- nv_data_check(client);
+ if (nv_data_check(client) < 0)
+ return -1;
fd = open(nv_data_path(client), O_RDONLY);
@@ -675,7 +807,8 @@ int nv_data_write(struct ipc_client *client, int offset, int length, char *buf)
return -1;
}
- nv_data_check(client);
+ if (nv_data_check(client) < 0)
+ return -1;
fd = open(nv_data_path(client), O_WRONLY);