I want create an user in remote host(e.g. proxy.example.com), is there something Restricted login shell like git-shell for proxy-only ssh access?
Write your own! restricted-shell.c:
#include <stdio.h>
int main() {
printf("You are now connected to the proxy. Press enter to exit.\n");
scanf("*c");
exit(0);
}
Related
I am logging a user that is login to a linux system at an interval of 1-minute. The logging will be done using an init.d script owned by root and it auto start at bootup.
I tried using getlogin() and getlogin_r() with a simple init.d script. However, it will work if I run the init.d script via console but when I register the init.d script via chkconfig --add [initscript] and reboot the system, it runs as check via ps -ef but when I checked the log file, the username is empty.
Did I miss something? Is their an alternative approach of getting the login user?
getlogin() returns a pointer to a string containing the name of the user logged in on the controlling terminal of the process, or a null pointer if this information cannot be determined.
A script run through init does not have a controlling terminal. Whereas, if you run a script via console, the console is the controlling (virtual) terminal.
getlogin() does not do what you want to do. I assume you should have a look for users and who commands.
#ypnos,
I did not bother to check the who.c you provided from the link github.com/coreutils/coreutils/blob/master/src/who.c.
I did a different approach from the code snippet below.
#include <stdio.h>
#include <utmpx.h>
#include <time.h>
int main (void)
{
struct utmpx *UtmpxPtr = NULL;
struct tm *TimePtr = NULL;
time_t TimeInSec;
char TimeBuff[32];
printf("...Start \"who logged-in\"...\n");
setutxent();
while ((UtmpxPtr = getutxent()) != NULL)
{
if (UtmpxPtr->ut_type != USER_PROCESS)
{
continue;
}
TimeInSec = UtmpxPtr->ut_tv.tv_sec;
TimePtr = localtime(&TimeInSec);
strftime(TimeBuff, sizeof(TimeBuff), "%Y-%m-%d|%H:%M", TimePtr);
printf("%s|%s|%s\n", UtmpxPtr->ut_user, TimeBuff, UtmpxPtr->ut_host);
fflush(stdout);
}
endutxent();
return 0;
}
I am using x86_64 GNU/Linux with bash
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
system("set > setc"); // A subset of `$ set`
return 0;
}
I can see the file setc contains a subset of $ set.
I am feeling curious as to know how the shell (parent process) decides what to supply to child process and what not to?
What if I want to supply more environ variables to child process? How one can control that?
A shell variable can be either exported or not exported. The shell will only pass exported variables to child processes. In bash, you can export a variable(for example, $var) by executing export var.
I'm testing the communication between node.js and executables launched as child processes. An executable will be launched from within node.js via child_process.spawn() and its output will be monitored by node.js. I'm testing this capability both on Linux and Windows OSs.
I've successfully spawned tail -f /var/log/syslog and listened to its output, but my own executables can't seem to write correctly to stdout (in whatever form it exists when captured by node.js).
Test code:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
int main()
{
using namespace std;
long x = 1;
while (true)
{
fprintf(stdout, "xtime - %ld\n", x++);
usleep(1000000);
}
}
(Note: some includes may be useless; I've not checked them)
stdout output is not automatically flushed (at least on *nix) when stdout is not a tty (even if there is a newline in the output, otherwise a newline generally flushes when stdout is a tty).
So you can either disable stdout buffering entirely via setbuf(stdout, NULL); or you can manually flush output via fflush(stdout);.
I need to be able to change the prompt on running an executable of a c file to get a custom prompt
E.g:
$ abc
abc>
Here the user can give the commands acceptable to the program.
I saw this happen for programs like MySQL and was wondering if it is possible to do this.
You can use gnu readline for custom prompt
#include <readline/readline.h>
#include <readline/history.h>
while (1)
{
command = readline ("$abc");
command = readline ("abc>");
//validate your command name
system(command);
add_history (command); ///add command in history
}
you can include the readline library in your program to make it have a modern command line interface.
Or you can simply build a loop get each line from input and get the tokens from that line of input to execute commands, and there printout your abc> prompt.
I am working on a project in which I need to know the current working directory of the executable which called the system call. I think it would be possible as some system calls like open would make use of that information.
Could you please tell how I can get the current working directory path in a string?
You can look at how the getcwd syscall is implemented to see how to do that.
That syscall is in fs/dcache.c and calls:
get_fs_root_and_pwd(current->fs, &root, &pwd);
root and pwd are struct path variables,
That function is defined as an inline function in include/linux/fs_struct.h, which also contains:
static inline void get_fs_pwd(struct fs_struct *fs, struct path *pwd)
and that seems to be what you are after.
How do you do that in a terminal ? You use pwd which looks at the environment variable named PWD.
#include <stdlib.h>
int main(int ac, char **av) {
printf("%s\n", getenv("PWD");
return 0;
}
If you want to know in which directory the executable is located you can combine the information from getenv and from argv[0].