Unable to determine reason for SIGPIPE - unix

I have a Client Server where client issues file operations to Server. Program runs perfectly when first read/delete command is issued. But when I issue second command read/delete, it exits with exit code 141. I determine reason to be SIGPIPE.But unable to resolve it. Can someone help me on this
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <sys/wait.h>
#include <mqueue.h>
#include <sys/stat.h>
//#include <limits.h>
#include "Functions.h"
#define PIPE_BUF 50000
#define MAXMESGDATA (PIPE_BUF -2*sizeof(long))
#define MESGHDRSIZE (sizeof(Message_buf) -MAXMESGDATA)
#define MAX_SIZE 512
pid_t serverPid;
pid_t clientPid;
void Server(int readfd,int writefd)
{
Message_buf server_MessageBuf;
int operationStatus = 0;
char inputFileName[MAXMESGDATA];
char operationToBePerformed[MAXMESGDATA];
char messageOnPIPE[MAXMESGDATA];
ssize_t length;
if((length=mesg_recv(readfd,&server_MessageBuf))==0)
{
printf("\n End of file while reading pathname");
}
strcpy(messageOnPIPE,server_MessageBuf.messageText);
printf("\n Server side Message on PIPE:%s \n ",messageOnPIPE);
operationStatus=interpretCommand(messageOnPIPE,operationToBePerformed,inputFileName);
if(strcasecmp(operationToBePerformed,"read")==0)
{
readFile(writefd,inputFileName);
//printf("\n Read %s ",inputFileName);
}
if(strcasecmp(operationToBePerformed,"delete")==0)
{
deleteFile(writefd,inputFileName);
}
}
int main()
{
int pipe1[2],pipe2[2];
pipe(pipe1);
pipe(pipe2);
//signal(SIGPIPE, SIG_IGN);
pid_t pid;
pid=fork();
serverPid=pid;
if(pid==0)
{
/*Call Server*/
close(pipe1[1]);
close(pipe2[0]);
Server(pipe1[0], pipe2[1]);
}
else
{
close(pipe1[0]);
close(pipe2[1]);
Client(pipe2[0],pipe1[1]);
}
return 0;
}

Your server is not running in a loop. It receives one message and then closes the pipe, so the second write fails and a SIGPIPE is sent to the client.

Related

pthread_cond_timedwait() not working on FreeBSD, returns EPERM

I have a sample program which creates a pthread, waits for the thread to join. The thread will invoke phread_cond_timedwait() to wait for 2 seconds. On Linux platforms, the sample code works fine. On FreeBSD, the call returns immediately with EPERM error code.
pthread_condition_timedwait.cpp
#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
void *thread_handler(void *ptr){
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
struct timespec ts;
struct timeval tp;
gettimeofday(&tp, NULL);
ts.tv_sec = tp.tv_sec;
ts.tv_nsec = tp.tv_usec*1000;
ts.tv_sec += 2;
//Invoke pthread_cond_timedwait() to wait for 2 seconds
int rcode = pthread_cond_timedwait(&cond, &mutex, &ts);
if (rcode == ETIMEDOUT)
printf("Terminated due to time out\n");
else if (rcode == EPERM)
printf("Terminated due to EPERM\n");
else
printf("Return code is %d\n", rcode);
return NULL;
}
int main(int argc, char** argv){
pthread_t thread;
// start the thread
pthread_create(&thread, NULL, &thread_handler, NULL);
// wait for thread to finish
pthread_join(thread, NULL);
return 0;
}
EPERM is returned if the thread that calls timedwait does not own the mutex. You must lock the mutex before calling timedwait. Also, move the static initialization of mutex and condvar to file scope.
UPDATE: If you initialize the mutex to be an error-checking mutex, Linux will also terminate with EPERM (since it is UB to call pthread_cond_wait/timedwait without holding the mutex).
Modified code below:
//#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex;
void *thread_handler(void *ptr){
struct timespec ts;
struct timeval tp;
gettimeofday(&tp, NULL);
ts.tv_sec = tp.tv_sec;
ts.tv_nsec = tp.tv_usec*1000;
ts.tv_sec += 2;
//Invoke pthread_cond_timedwait() to wait for 2 seconds
int rcode = pthread_cond_timedwait(&cond, &mutex, &ts);
if (rcode == ETIMEDOUT)
printf("Terminated due to time out\n");
else if (rcode == EPERM)
printf("Terminated due to EPERM\n");
else
printf("Return code is %d\n", rcode);
return NULL;
}
int main(int argc, char** argv){
pthread_mutexattr_t mta;
pthread_mutexattr_init(&mta);
pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK);
pthread_mutex_init(&mutex, &mta);
pthread_t thread;
// start the thread
pthread_create(&thread, NULL, &thread_handler, NULL);
// wait for thread to finish
pthread_join(thread, NULL);
return 0;
}
Tested on kernel SMP Debian 4.9.82-1+deb9u3 (2018-03-02) x86_64 GNU/Linux, distro Debian GNU/Linux buster/sid.

PWM QT GUI in friendly arm mini2440

I am using a mini2440 board. I want to develop a gui for pwm that can increase and decrease the frequency of the signal. It has four buttons - start, stop, max and min - but pressing the max button makes no change in freq. By using the start and stop the gui program terminate but still pwm is on, I mean buzzer is on.
Here is the code
#include "hello.h"
#include <qlabel.h>
#include <qpushbutton.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>
#include <qobject.h>
#include <fstream>
#include <linux/i2c-dev.h>
#include <string.h>
#include <stdio.h>
#include <linux/fs.h>
#include <termios.h> //Headers
#include <stdint.h>
#include <getopt.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
#define PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 0
int freq=1000;
int fd;
HelloForm::HelloForm( QWidget* parent, const char* name, WFlags fl)
:HelloBaseForm(parent, name, fl)
{
connect(PushButton33,SIGNAL(clicked()),this,SLOT(start()));
connect(PushButton30,SIGNAL(clicked()),this,SLOT(stop()));
connect(PushButton31,SIGNAL(clicked()),this,SLOT(plus()));
connect(PushButton32,SIGNAL(clicked()),this,SLOT(minus()));
}
HelloForm::~HelloForm()
{
}
// this is start button
void HelloForm::start()
{
int fd = open("/dev/pwm", 0);
if (fd < 0) {
status->setText("open pwm_buzzer device");
}
else {
status->setText("start");
}
ioctl(fd, PWM_IOCTL_SET_FREQ, freq);
pwm->setText(QString::number(freq));
//close(fd);
}
//stop button
void HelloForm::stop()
{
close(fd);
int fd=open("/dev/pwm",0); //File handler
ioctl(fd, PWM_IOCTL_STOP); // stop the ioctl
close(fd);
}
// increase the pwm
void HelloForm::plus()
{
if(freq<20000)
{
freq+=10;
ioctl(fd,PWM_IOCTL_SET_FREQ,freq); //freq set
pwm->setText(QString::number(freq));
}
}
void HelloForm::minus()
{
if(freq>11)
{
freq-=10;
ioctl(fd,PWM_IOCTL_SET_FREQ,freq);
pwm->setText(QString::number(freq));
}
}

frama-c malloc Neon-20140301 fatal error

Is it possible to detect memory leaks or double free with Frama-c?
I have tried to test that example But
#include <string.h>
#include <stdlib.h>
#define FRAMA_C_MALLOC_STACK
#include "/usr/share/frama-c/libc/fc_runtime.c"
int main()
{
int *x = malloc(sizeof(int));
free(x);
free(x);
return 0;
}
I get :
Now I am using Version: Neon-20140301 and libc copied from Fluorine-20130601 ( btw why fc_runtime.c and other *.c files are deleted from Neon release ? )
command:
frama-c-gui -cpp-command "gcc -C -E -I/usrhare/frama-c/libc/ -nostdinc" -slevel 1000 -val -val-warn-copy-indeterminate #all main.
Using other defines (FRAMA_C_MALLOC_XXXX) works but is not detecting any bugs.
update:
Other example
#include <string.h>
#include <stdlib.h>
#define FRAMA_C_MALLOC_STACK
#include "/usr/share/frama-c/libc/fc_runtime.c"
int main()
{
int *x = malloc(sizeof(int));
x[2] = 5;
return 0;
}

Rs232 using libevent

I am trying to use libevent for manage the serial communication between an embedded Linux device and a pc.
First problem with libevent. I've created a C Project in eclipse , in the main I am creating some events and it is ok for the compiler:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <event.h>
#include "function_test.h"
....
int main(void) {
struct event ev_sighup; //reports that the user's terminal is disconnected
struct event ev_sigterm; //program termination
struct event ev_sigint; // program interrupt
int rv = 0;
/* Set up libevent & signal handling */
event_init();
event_set(&ev_sighup, SIGHUP, EV_SIGNAL, peripherals_end, NULL);
event_add(&ev_sighup, NULL);
event_set(&ev_sigterm, SIGTERM, EV_SIGNAL, peripherals_end, NULL);
event_add(&ev_sigterm, NULL);
event_set(&ev_sigint, SIGINT, EV_SIGNAL, peripherals_end, NULL);
event_add(&ev_sigint, NULL);
.....
}
But then, in "function_test.c":
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <event.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include "function_test.h"
.....
/*serial file descriptor */
int 232_fd= -1;
/* Event triggered when data is available */
struct event ev_rs232read;
.....
event_set(&ev_rs232read, 232_fd, EV_READ|EV_PERSIST, readRs232, NULL);
if ((rv = event_add(&stm32_ev_read, NULL)) < 0) {
// log error
return RTN_ERR;
}
return RTN_OK;
}
And misteriously Eclipse doesn't finds event.h (only in function_test.c) and thereby I got the next errors:
warning: implicit declaration of function ‘event_set’
../src/function_test.c:114: error: ‘EV_READ’ undeclared (first use in this function)
../src/function_test.c:114: error: (Each undeclared identifier is reported only once
../src/function_test.c:114: error: for each function it appears in.)
../src/function_test.c:114: error: ‘EV_PERSIST’ undeclared (first use in this function)
...
Does this bug repeats during the compilation with GNU Autotools or just simple Makefile?

/usr/include/netinet/in.h:138: note: candidates are: in_addr& in_addr::operator=(const in_addr&)

I wrote a simple TCP/IP network applications (server and client). In the code of client app, I did like this and then build with 'g++ -o client client.cpp' under Linux.
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char* argv[])
{
struct sockaddr_in server_addr;
struct hostent* host;
....
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(nPort);
server_addr.sin_addr = *((struct in_ddr*)host->h_addr); /*It complains as below while building*/
....
}
client.cpp: In function 'int main(int, char**)':
client.cpp:56: error: no match for 'operator=' in 'server_addr.sockaddr_in::sin_addr = *(in_ddr*)(* host->hostent::h_addr_list)'
/usr/include/netinet/in.h:138: note: candidates are: in_addr& in_addr::operator=(const in_addr&)
* Error code 1
clearmake: Error: Build script failed for "client"
what's going on with my implementaion?
You are trying to set an address list as a single address. You'll want to use brackets to refer to the address you want. host->h_addr_list[0]
At least that is what I am getting from your error.
Reference sources

Resources