add missing file

This commit is contained in:
Stefan Lankes 2011-09-25 20:43:53 +02:00
parent 42c69cfcfc
commit 0ccdcf6a38

105
newlib/examples/rlogind.c Normal file
View file

@ -0,0 +1,105 @@
/*
* Copyright 2011 Stefan Lankes
* Chair for Operating Systems, RWTH Aachen University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#undef errno
extern int errno;
#define PORT 4211
int main(int argc, char* argv[])
{
int sockd, sockd2;
unsigned int addrlen;
struct sockaddr_in my_name, peer_name;
int status;
int cmd, i=0;
pid_t pid;
/* create a socket */
sockd = socket(AF_INET, SOCK_STREAM, 0);
if (sockd == -1)
{
perror("Socket creation error");
exit(1);
}
memset(&my_name, 0x00, sizeof(my_name));
/* server address */
my_name.sin_family = AF_INET;
my_name.sin_addr.s_addr = INADDR_ANY;
my_name.sin_port = htons(PORT);
status = bind(sockd, (struct sockaddr*)&my_name, sizeof(my_name));
if (status == -1)
{
perror("Binding error");
exit(1);
}
status = listen(sockd, 5);
if (status == -1)
{
perror("Listening error");
exit(1);
}
while(1)
{
/* wait for a connection */
addrlen = sizeof(peer_name);
sockd2 = accept(sockd, (struct sockaddr*)&peer_name, &addrlen);
if (sockd2 == -1)
{
perror("Wrong connection");
exit(1);
}
pid = fork();
if (pid == 0) { // child
char* newargs[] = {"/bin/mshell", NULL};
char* newenv[] = {"USER=root", "PATH=/bin:/sbin:/usr/bin", "PWD=/", "TEMP=/tmp", NULL};
// set stdout, stdin and stderr to the socket descriptor
close(STDIN_FILENO);
dup(sockd2);
close(STDOUT_FILENO);
dup(sockd2);
close(STDERR_FILENO);
dup(sockd2);
execve("/bin/mshell", newargs, newenv);
return errno;
}
printf("Create mshell with pid %u\n", pid);
//close(sockd2);
}
return 0;
}