Skip to content
Permalink
dccea4b510
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
57 lines (51 sloc) 1.47 KB
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <dirent.h>
#include <time.h>
void checkError(int status)
{
if (status < 0) {
printf("socket error(%d): [%s]\n",getpid(),strerror(errno));
exit(-1);
}
}
int main(int argc, char ** argv) {
if (argc != 2) {
printf("usage: ./server <filepath of bash>");
}
int sid = socket(PF_INET,SOCK_STREAM,0);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8025);
addr.sin_addr.s_addr = INADDR_ANY;
int status = bind(sid,(struct sockaddr*)&addr,sizeof(addr));
checkError(status);
status = listen(sid,10);
checkError(status);
while(1) {
struct sockaddr_in client;
socklen_t clientSize;
int chatSocket = accept(sid,(struct sockaddr*)&client,&clientSize);
checkError(chatSocket);
printf("We accepted a socket: %d\n",chatSocket);
pid_t child = fork();
if (child == 0) {
printf("child here\n");
dup2(chatSocket, 0);
dup2(chatSocket, 1);
dup2(chatSocket, 2);
execlp(argv[1], "bash", NULL);
printf("bash failed\n");exit(-1);
} else if (child > 0) {
printf("Created a child: %d\n",child);
close(chatSocket);
}
}
}