+waitpid()
[meinos.git] / apps / login / login.c
blob414216e7a002ee8e34ca14a5033c805a603862d2
1 /*
2 meinOS - A unix-like x86 microkernel operating system
3 Copyright (C) 2008 Janosch Gräf <janosch.graef@gmx.net>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <sys/wait.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <limits.h>
23 #include <misc.h>
24 #include <pwd.h>
26 /// @todo this should be "/bin/sh" (when symlinks work)
27 #define DEFAULT_SHELL "/boot/bin/sh"
29 #ifdef LOGIN
30 FILE *console;
32 int init_input() {
33 console = fopen("/dev/console","rw");
34 return console!=NULL?0:-1;
37 char *input(int hide) {
38 static char buf[LOGIN_NAME_MAX];
39 int i = 0;
40 for (i=0;i<LOGIN_NAME_MAX-1 && (buf[i] = fgetc(console))!='\n';i++) fputc(hide?'*':buf[i],console);
41 fputc('\n',console);
42 buf[i] = 0;
43 return buf;
46 char *login() {
47 char *user;
48 //char *pass;
49 struct passwd *passwd;
51 do {
52 fputs("User: ",console);
53 fflush(stdout);
54 user = input(0);
55 passwd = getpwnam(user);
56 if (passwd==NULL) {
57 fputs("User does not exist.\n",console);
58 break;
60 /// @todo Check password
61 /*fputs("Password: ",console);
62 fflush(stdout);
63 pass = input(1);*/
64 } while (0);
66 setreuid(passwd->pw_uid);
67 setregid(passwd->pw_gid);
68 chdir(passwd->pw_dir);
69 return passwd->pw_shell;
71 #endif
73 void shell_run(char *shell) {
74 pid_t pid = execute(shell,NULL,NULL,NULL,NULL);
75 waitpid(pid,NULL,0);
78 int main(int argc,char *argv[]) {
79 #ifdef LOGIN
80 if (init_input()==-1) abort();
81 while (1) shell_run(login());
82 #else
83 shell_run(DEFAULT_SHELL);
84 #endif
85 printf("TODO %s:%d: Run shutdown\n",__FILE__,__LINE__);
86 while (1);
87 return 0;