FreeNOS
Login.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <string.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <sys/utsname.h>
25#include <sys/types.h>
26#include <sys/wait.h>
27#include "Login.h"
28
29Login::Login(int argc, char **argv)
30 : POSIXApplication(argc, argv)
31{
32 parser().setDescription("Login program which starts the user shell");
33 parser().registerPositional("INPUT", "Input character device for reading standard input");
34 parser().registerPositional("OUTPUT", "Output character device for writing standard output");
35}
36
38{
39}
40
42{
43 struct utsname uts;
44
45 if (uname(&uts) != -1)
46 {
47 printf("\r\n%s %s\r\n\r\nlogin: ",
48 uts.sysname,
49 uts.release
50 );
51 }
52}
53
54const char * Login::getUsername() const
55{
56 static char line[1024];
57 Size total = 0;
58
59 /* Read a line. */
60 while (total < sizeof(line) - 1)
61 {
62 /* Read a character. */
63 read(0, line + total, 1);
64
65 /* Process character. */
66 switch (line[total])
67 {
68 case '\r':
69 case '\n':
70 printf("\r\n");
71 line[total] = ZERO;
72 return line;
73
74 case '\b':
75 if (total > 0)
76 {
77 total--;
78 printf("\b \b");
79 }
80 break;
81
82 default:
83 printf("%c", line[total]);
84 total++;
85 break;
86 }
87 }
88 line[total] = ZERO;
89 return line;
90}
91
93{
94 const char *user;
95 const char *sh_argv[] = { "/bin/sh", 0 };
96 pid_t pid;
97 int status;
98
99 // Wait until the I/O files are available (busy loop)
100 while (true)
101 {
102 // Re-open standard I/O
103 close(0);
104 close(1);
105 close(2);
106
107 // Stdin
108 if (open(arguments().get("INPUT"), O_RDONLY) == -1)
109 continue;
110
111 // Stdout
112 if (open(arguments().get("OUTPUT"), O_RDWR) == -1)
113 continue;
114
115 // Stderr
116 if (open(arguments().get("OUTPUT"), O_RDWR) == -1)
117 continue;
118
119 // Done
120 break;
121 }
122
123 // Loop forever with a login prompt
124 while (true)
125 {
126 printPrompt();
127 user = getUsername();
128
129 if (strlen(user) > 0)
130 {
131 // Start the shell
132 if ((pid = forkexec("/bin/sh", sh_argv)) != (pid_t) -1)
133 {
134 waitpid(pid, &status, 0);
135 }
136 else
137 {
138 ERROR("forkexec '" << sh_argv[0] << "' failed: " << strerror(errno));
139 }
140 }
141 }
142 return Success;
143}
Result
Result codes.
Definition Application.h:54
const ArgumentContainer & arguments() const
Get program arguments.
ArgumentParser & parser()
Get program arguments parser.
void setDescription(const String &desc)
Set program description.
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
const char * getUsername() const
Read username from standard input.
Definition Login.cpp:54
Login(int argc, char **argv)
Constructor.
Definition Login.cpp:29
virtual ~Login()
Destructor.
Definition Login.cpp:37
void printPrompt() const
Print the login program prompt.
Definition Login.cpp:41
virtual Result exec()
Execute the application.
Definition Login.cpp:92
POSIX-compatible application.
C size_t strlen(const char *str)
Calculate the length of a string.
Definition strlen.cpp:21
C int forkexec(const char *path, const char *argv[])
Create a new process and execute program.
Definition forkexec.cpp:27
C int open(const char *path, int oflag,...)
Open file relative to directory file descriptor.
Definition open.cpp:26
C char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition strerror.cpp:20
C int close(int fildes)
Close a file descriptor.
Definition close.cpp:22
C int errno
The lvalue errno is used by many functions to return error values.
#define O_RDONLY
Open for reading only.
Definition fcntl.h:81
ProcessID pid_t
Used for process IDs and process group IDs.
Definition types.h:32
C int printf(const char *format,...)
Output a formatted string to standard output.
Definition printf.cpp:22
C ssize_t read(int fildes, void *buf, size_t nbyte)
Read from a file.
Definition read.cpp:22
#define O_RDWR
Open for reading and writing.
Definition fcntl.h:84
C int uname(struct utsname *name)
Get the name of the current system.
Definition utsname.cpp:23
C pid_t waitpid(pid_t pid, int *stat_loc, int options)
Wait for a child process to stop or terminate.
Definition waitpid.cpp:23
#define ERROR(msg)
Output an error message.
Definition Log.h:61
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
System name structure.
Definition utsname.h:39
char release[UTSBUF]
Current release level of this implementation.
Definition utsname.h:50
char sysname[UTSBUF]
Name of this implementation of the operating system.
Definition utsname.h:41