Segmentation fault in client program - c

Simply doing the following results in a segmentation fault.
Could it be something with argv or argc?
Really lost. Thanks in advance!
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#define MESSAGE "Not so important message"
int main(int argc, char * argv[])
{
printf(argc);
return 0;
}

if(hp != NULL)
{
printf("Here 6");
perror("Failed to get host by name!");
exit(1);
}
Why are you quitting if hp!=NULL ? It should be other way round. You are de-referencing null pointer after this incorrect check:
bcopy((char *)hp->h_addr, (char *) &server.sin_addr.s_addr, hp->h_length)
Change to:
if(hp == NULL)
{
perror("Failed to get host by name!");
exit(1);
}

I only get a segfault if I don't supply a hostname as an argument. This makes sense as you try to call gethostbyname with the argument of argv[1], which will be a NULL pointer if no argument is supplied.
The reason you don't see any of the printf() messages is that the output is buffered, if you changed them to print to stderr (unbuffered by default) you would see them before the segfault.

Related

Why I can't read from and write to socket with printf and scanf?

I wonder why this code is not working. I mean: When both the server and client are running I can type and type with no result. However, when I kill the client, the server starts outputting everything I previously wrote to the client app.
full server.c: https://pastebin.com/iXSYLZS3
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8080
int main(void)
{
// some code to initialize connection
printf("connection accepted!\n");
fflush(stdout);
close(0);
dup(sock);
while (1) {
scanf("%s", str);
printf("%s", str);
fflush(stdout);
}
return(0);
}
full client.c: https://pastebin.com/sFmi72ZP
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT 8080
int main(void)
{
// some code to initialize connection
printf("connected!\n");
fflush(stdout);
close(1);
dup(sock);
while (1) {
scanf("%s", str);
printf("%s", str);
fflush(stdout);
}
return(0);
}
It may appear to 'work', but that is just by accident. Closing the underlying descriptor of a FILE* and replacing it with another is bound to confuse the FILE implementation at some point (most likely during that important demo)
The safe way is to use fdopen to create a new FILE* from the given socket and then use fscanf/fprintf if you insist on using that.
fdopen is a posix function, but so is dup so I guess you are on a posix system.
It works. I just had to add newline at the end of the string for pipe to be flushed. Eg. I used:
printf("%s\n", str);
instead of:
printf("%s", str);

Execve is giving bad address

I'm having a little problem with the execve command. The program Test should create two children and each one should run an execve to load and run another program. But I'm getting a bad address on both the execve. The code is as follows:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(){
int child_1, child_2;
child_1=fork();
if(child_1==0){
char* argv[]={"Test", "Test_1", "NULL"};
if((execve("Test_1", argv, NULL))<0) perror("execve 1 fail");
exit(0);
}else if(child_1<0)perror("error fork");
else wait(NULL);
child_2=fork();
if(child_2==0){
char* argv1[]={"Test", "Test_2", "NULL"};
if((execve("Test_2", argv1, NULL))<0) perror("execve 2 fail");
exit(0);
}else if(child_2<0)perror("error fork");
else wait(NULL);
return 0;
}
You are not terminating the argument array correctly:
char* argv[]={"Test", "Test_1", "NULL"};
"NULL" is a string literal, it's not same as NULL. The array needs to be terminated with a null pointer. Instead do:
char* argv[]={"Test", "Test_1", (char*)0};
Similarly, fix the other argument array.
or you have to change "NULL" to simply NULL. In the case "NULL" you give NULL as a commandline parameter. The case NULL without " means end of argument list.

Command Line Argument processing in c

On running the below code, it stucks after displaying the argv[0], argv[1] and argv[2] line.
Further flow of code is blocked at this point, can any one help why it is stopping its execution or is it entering into an infinite loop.
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include "p8log.h"
#include <errno.h>
int main(int argc, char* argv[])
{
char* PORT;
char* IPADDR;
printf("Arg Count=%d\n",argc);
printf("Arguments are=%s,%s,%s\n",argv[0],argv[1],argv[2]);
printf("HELLO");
PORT=argv[1],
printf("WORLD");
IPADDR=argv[2];
printf("START");
printf("port num=%s",PORT);
printf("IP ADDR=%s",IPADDR);
printf("END");
/* some algorithm of calculation */
return 0;
}
Execution
./file-exe 11111 127.0.0.1
Output
Arg Count=3
Arguments are=./file-exe,11111,127.0.0.1
fflush(NULL); is good to do after any output, if you want to make sure it prints to screen. printf is buffered, so it can get lost.
./a.out 11111 127.0.0.1
Arg Count=3
Arguments are=./a.out,11111,127.0.0.1
HELLO
WORLD
START
port num=11111
IP ADDR=127.0.0.1
END
works fine, you needed some \n to break up lines, like so..
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
char* PORT;
char* IPADDR;
printf("Arg Count=%d\n",argc);
printf("Arguments are=%s,%s,%s\n",argv[0],argv[1],argv[2]);
printf("HELLO\n");
PORT=argv[1],
printf("WORLD\n");
IPADDR=argv[2];
printf("START\n");
printf("port num=%s\n",PORT);
printf("IP ADDR=%s\n",IPADDR);
printf("END\n");
fflush(NULL);
/* some algorithm of calculation */
return 0;
}

write socket causes "program exited with code 141" in C

I'm trying to setup a client/server in C. I establish the connection, then I want to send an username and a pwd to the server, and the server has to reply confirming that he has received the usr/pwd. The problem is that both server and client exit as soon as they meet the "write" or "read" function. What should I do?
server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <signal.h>
void main(){
int ds_sock;
struct sockaddr_in my_addr;
ds_sock=socket(AF_INET,SOCK_STREAM,0);
memset(&my_addr,0,sizeof(my_addr));
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(25000);
my_addr.sin_addr.s_addr=INADDR_ANY;
if(bind(ds_sock,(struct sockaddr *)&my_addr,sizeof(my_addr))<0){
printf("error in bind");
}
listen(ds_sock,2);
int ds_sock_acc;
struct sockaddr_in addr;
size_t sin_size = sizeof(struct sockaddr_in);
signal(SIGCHLD,SIG_IGN);
while(1){
if((ds_sock_acc=accept(ds_sock,(struct sockaddr *)&addr,&sin_size))<1){
printf("error accept");
}
printf("connected");
char usr[10];
read(ds_sock,usr,10);
char* confirm_usr;
confirm_usr="Username received";
write(ds_sock,confirm_usr,100);
char pwd[10];
read(ds_sock,pwd,10);
char* confirm_pwd;
confirm_pwd="Password received";
write(ds_sock,confirm_pwd,100);
}
}
client.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
void main(){
int ds_sock;
ds_sock = socket(AF_INET, SOCK_STREAM,0);
int ret;
struct sockaddr_in Eaddr;
Eaddr.sin_family = AF_INET;
Eaddr.sin_port = htons(25000);
Eaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
ret = connect(ds_sock,(struct sockaddr *)&Eaddr,sizeof(Eaddr));
if(ret==-1){
printf("error connect");
exit(EXIT_FAILURE);
}
printf("connect OK");
char usr[10];
printf("Insert username");
scanf("%s",usr);
char pwd[12];
printf("Insert password");
scanf("%s",pwd);
printf("%s",pwd);
write(ds_sock,usr,10);
char usr_reply[100];
read(ds_sock,usr_reply,100);
printf("%s",usr_reply);
write(ds_sock,pwd,12);
char pwd_reply[100];
read(ds_sock,pwd_reply,100);
printf("%s",pwd_reply);
}
While the first answer is pretty complete, I wanted to add the 141 error is a SIGPIPE error. (check this in linux manual)
You can strace your program to find the error code or implement errno and perror.
You're accessing out of bounds, thus triggering undefined behavior in your write() call:
char* confirm_usr;
confirm_usr="Username received";
write(ds_sock,confirm_usr,100);
You're asking write() to send 100 bytes, but you only provide a pointer to 18 bytes. Reading any more than 18 from that location triggers undefined behavior. It would make more sense to use strlen() to avoid hardcoding the length:
const char *confirm_usr = "Username received";
write(ds_sock, confirm_usr, strlen(confirm_usr) + 1);
I made it + 1 to actually send the terminating '\0'-character, otherwise the other end has no way of figuring out when the string ends.
Also, you must check the return value of lots of calls, I/O can and will fail and just must deal with that.
It's also pretty strange that it looks as if both server and client begin by doing a read(), and that the server does I/O on a socket different from ds_sock_acc.

Using fork() for split process- what's wrong with the program

I'd really love your help with understanding why doesn't the process reach the "son process" after using fork() command. I'm trying to write a program that runs another program, but It seems that the program dosen't even reach the son process. I can tell that since "son process" is not being printed to the screen, and I really wonder why.
Here's a sketch of the code- I can't even check if it is alright since as I said, it doesn't even reaching the son process, I always get "son exited with error".
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <assert.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>
#define MAXARGV 5;
int main() {
char* cmd;
int child_status;
char* s;
char** argv;
int counter;
cmd= (char*) calloc( 5, sizeof(char)*20);
s=(char*) calloc(1,sizeof(char)*20);
argv=(char**) calloc(5, sizeof(char*)*20);
printf("Please write a command\n");
gets(cmd);
counter = 0;
while (strcmp(cmd, "exit") != 0) {
int pid = fork();
if (pid == 0) {
printf("son process");
while (sscanf(cmd, "%s", s) == 1) {
strcpy(argv[counter], s);
counter++;
}
execv(argv[0], argv);
printf("the command is not legal");
assert(0);
}
else {
if (wait(&child_status) == -1) {
printf("error waiting for pid=%d\n", pid);
exit(-1);
}
if(WIFEXITED(child_status)!=0)
printf("son status=%d\n", WEXITSTATUS(child_status));
else
printf("son exited with error\n");
}
printf("Please write a command");
gets(cmd);
}
free(s);
free(cmd);
free(argv);
printf("here as well");
return 1;
}
The program reaches the printf("son process") just fine, but that just puts the string in a buffer inside the process and since you didn't fflush() it, it doesn't make it to the screen and is discarded with the rest of the process' memory in the exec call. Note, that stdout is normally line-buffered, so if you had newline there, it would auto-flush. Also stderr is by default unbuffered and more suitable for debug prints (fprintf(stderr, "child process")).
You are trying to assemble the command read from standard input in argv, but it only has memory for the actual arguments given to you, so you overrun this memory and get segmentation fault.
if WIFEXITED gives zero, you should use WIFSIGNALED and WTERMSIG to confirm that the error is indeed SIGSEGV.
assert(0) is not a good way to terminate process after error. exit(1) is. Assertions are only for conditions that indicate bug in the code itself if they happen and are often eliminated (by defining NDEBUG) from production code.

Resources