MT#57411 warning: ignoring return value in `main()` (sems.cpp)

sems.cpp: In function 'int main(int, char**)':
     sems.cpp:505:19: warning: ignoring return value of 'ssize_t read(int, void*, size_t)' declared with attribute 'warn_unused_result' [-Wunused-result]
       505 |         (void)read(fd[0], &pid, sizeof(int));
           |               ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
     sems.cpp:521:18: warning: ignoring return value of 'ssize_t write(int, const void*, size_t)' declared with attribute 'warn_unused_result' [-Wunused-result]
       521 |       (void)write(fd[1], &main_pid, sizeof(int));
           |             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     sems.cpp:629:16: warning: ignoring return value of 'ssize_t write(int, const void*, size_t)' declared with attribute 'warn_unused_result' [-Wunused-result]
       629 |     (void)write(fd[1], &main_pid, sizeof(int));
           |           ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     sems.cpp:670:17: warning: ignoring return value of 'ssize_t write(int, const void*, size_t)' declared with attribute 'warn_unused_result' [-Wunused-result]
       670 |      (void)write(fd[1], &main_pid, sizeof(int));
           |            ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Treat the error cases during read, close and write usage in the main() function.
Upon return of the `-1` throw a warning.

Change-Id: Iaa2da26573e5793e12095c3273aa1e567b8e6ef8
mr11.4.1
Donat Zenichev 3 years ago
parent 7f5a4bdd3f
commit 1a25a841d8

@ -502,7 +502,10 @@ int main(int argc, char* argv[])
/* parent process => wait for result from child*/
for(int i=0;i<2;i++){
DBG("waiting for child[%d] response\n", i);
(void)read(fd[0], &pid, sizeof(int));
if (read(fd[0], &pid, sizeof(int)) == -1)
WARN("Cannot read from the given fd: '%d', errno: '%s'. You probably want to take care of that.\n", fd[0], strerror(errno));
if(pid<0){
ERROR("Child [%d] return an error: %d\n", i, pid);
close(fd[0]);
@ -513,12 +516,17 @@ int main(int argc, char* argv[])
DBG("all children return OK. bye world!\n");
close(fd[0]);
return 0;
}else {
} else {
/* child */
close(fd[0]);
if (close(fd[0]) == -1)
WARN("Cannot properly close the fd: '%d', errno: '%s'. You probably want to take care of that.\n", fd[0], strerror(errno));
main_pid = getpid();
DBG("hi world! I'm child [%d]\n", main_pid);
(void)write(fd[1], &main_pid, sizeof(int));
if (write(fd[1], &main_pid, sizeof(int)) == -1)
WARN("Cannot properly write a data to the fd: '%d', errno: '%s'. You probably want to take care of that.\n", fd[1], strerror(errno));
}
/* become session leader to drop the ctrl. terminal */
if (setsid()<0){
@ -626,8 +634,13 @@ int main(int argc, char* argv[])
#ifndef DISABLE_DAEMON_MODE
if(fd[1]) {
DBG("hi world! I'm main child [%d]\n", main_pid);
(void)write(fd[1], &main_pid, sizeof(int));
close(fd[1]); fd[1] = 0;
if (write(fd[1], &main_pid, sizeof(int)) == -1)
WARN("Cannot properly write a data to the fd: '%d', errno: '%s'. You probably want to take care of that.\n", fd[1], strerror(errno));
if (close(fd[1]) == -1)
WARN("Cannot properly close the fd: '%d', errno: '%s'. You probably want to take care of that.\n", fd[1], strerror(errno));
fd[1] = 0;
}
#endif
@ -665,10 +678,14 @@ int main(int argc, char* argv[])
#ifndef DISABLE_DAEMON_MODE
if(fd[1]){
main_pid = -1;
DBG("send -1 to parent\n");
(void)write(fd[1], &main_pid, sizeof(int));
close(fd[1]);
main_pid = -1;
DBG("send -1 to parent\n");
if (write(fd[1], &main_pid, sizeof(int)) == -1)
WARN("Cannot properly write a data to the fd: '%d', errno: '%s'. You probably want to take care of that.\n", fd[1], strerror(errno));
if (close(fd[1]) == -1)
WARN("Cannot properly close the fd: '%d', errno: '%s'. You probably want to take care of that.\n", fd[1], strerror(errno));
}
#endif

Loading…
Cancel
Save