Get Current user login to Linux via Daemon - linux

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;
}

Related

How to get cwd for relative paths?

How can I get current working directory in strace output, for system calls that are being called with relative paths? I'm trying to debug complex application that spawns multiple processes and fails to open particular file.
stat("some_file", 0x7fff6b313df0) = -1 ENOENT (No such file or directory)
Since some_file exists I believe that its located in the wrong directory. I'd tried to trace chdir calls too, but since output is interleaved its hard to deduce working directory that way. Is there a better way?
You can use the -y option and it will print the full path. Another useful flag in this situation is -P which only traces syscalls relating to a specific path, e.g.
strace -y -P "some_file"
Unfortunately -y will only print the path of file descriptors, and since your call doesn't load any it doesn't have one. A possible workaround is to interrupt the process when that syscall is run in a debugger, then you can get its working directory by inspecting /proc/<PID>/cwd. Something like this (totally untested!)
gdb --args strace -P "some_file" -e inject=open:signal=SIGSEGV
Or you may be able to use a conditional breakpoint. Something like this should work, but I had difficulty with getting GDB to follow child processes after a fork. If you only have one process it should be fine I think.
gdb your_program
break open if $_streq((char*)$rdi, "some_file")
run
print getpid()
It is quite easy, use the function char *realpath(const char *path, char *resolved_path) for the current directory.
This is my example:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
char *abs;
abs = realpath(".", NULL);
printf("%s\n", abs);
return 0;
}
output
root#ubuntu1504:~/patches_power_spec# pwd
/root/patches_power_spec
root#ubuntu1504:~/patches_power_spec# ./a.out
/root/patches_power_spec

Unable to make executable that properly communicates with node.js

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);.

login shell for proxy(ssh-D) access only?

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);
}

Getting current working directory within kernel code

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].

Execute shell commands using popen in c++

I need to be able to execute some shell commands such as moving to the right directory where I have some files I need to decode and then decoding them using another command. I read some stuff about using popen but I didnt really understand how to use it for executing multiple commands.
Any pointers will be greatly appreciated :)
Thanks
FILE *pf;
char command[150];
char data[512];
// Execute a process listing
sprintf(command, "cd");
pf = _popen(command,"r");
sprintf(command, "cd Test_copy");
pf = _popen(command,"r"); */
sprintf(command, "java -jar Tool.jar -b x.fit x.csv");
pf = _popen(command,"r");
if(!pf){
fprintf(stderr, "Could not open pipe for output.\n");
return;
}
// Grab data from process execution
fgets(data, 512 , pf);
// Print grabbed data to the screen.
fprintf(stdout, "-%s-\n",data);
if (_pclose(pf) != 0)
fprintf(stderr," Error: Failed to close command stream \n");
Use ShellExecute to play with files (open with default application etc.). Use system to run shell commands.
No, don't. That would be like using a sledgehammer to knock on a door. Furthermore, it is "evil": http://www.cplusplus.com/forum/articles/11153/

Resources