LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c prog that logs user off... (https://www.linuxquestions.org/questions/programming-9/c-prog-that-logs-user-off-13572/)

acromi 02-05-2002 12:42 AM

c prog that logs user off...
 
ok, heres what i want i need....well want, hehe...

in a c program, its a simple menu, i need to log the user running the program off...back to the login prompt. this is not a super user, just a normal account.

i know i could just set the program to be the users login shell..but i'd rather do it this way. im not sure if this is possible. and i know it may not be that secure. but is there a way to do this?

if its not possible from within a c prog, what about from a shell script?

any one? :(

isajera 02-05-2002 01:06 AM

well... i'm sure it's possible. compiling against the kernel headers might be one way to go (and you'd be totally on your own there :)), but running "exit" or "logout" using an exec variant would probably be the easiest way to go outside of a shell script. i'm not sure the shell script would even work.

acromi 02-05-2002 01:43 AM

yeah, ive tried using exit..but i couldnt get it to work. maybe (dont hate me =) you could give me an example. pretty please...

you know you cant resist :D

isajera 02-05-2002 01:56 AM

Quote:

Originally posted by acromi
yeah, ive tried using exit..but i couldnt get it to work. maybe (dont hate me =) you could give me an example. pretty please...

you know you cant resist :D

lol - you don't know how true that is. this is gonna bug me for days.

anyway, i tried writing a quick shell script, and all that happened was that it exited from itself. nothing exciting.

offhand, if you want to write this prog, then you're going to need to learn more about the login process than i currently know. all i know is something vague about gettys and virtual consoles. i was wondering if it might be that a user-space prog can't access that particular function... it might be a security risk somehow.

ummm... try checking out the source to "su" - that seems like it might have something useful or similar.

isajera 02-05-2002 02:00 AM

or... (this just occured to me) you could just kill your user's main process and see what happens. i think that would work. (haven't tried it :))

acromi 02-05-2002 02:04 AM

heh...sorry i got you interested =)

im faily new to programming in a unix environment. i've been running a linux box for about a year i guess now, and im just trying to think of ways to get more familiar with how it ticks...and this is one of those ways...

let me know if you find anything, or if you get bored looking. in the meantime i will keep lookin for a solution (after i sleep a little)

thx alot

micke.prag 02-05-2002 10:16 AM

Instead of checking the source of su. Wouldn´t it be better to check the source for logout?

isajera 02-05-2002 02:50 PM

logout isn't a program - it's a function in a shell program to terminate the shell. afaik, the login just activates a shell program, like bash, csh, tsh, ect, which then goes on to do all the other stuff, like X - after the original shell dies, the login terminates. so, we're looking for a way to kill the first shell after login. you can't call logout because it's a function inside the shell, and you don't have access to it if you're operating outside of it, as we hypotheticially are.

crabboy 02-05-2002 10:40 PM

If you start the program from your login shell, this program will kill it. It gets the pid of its parent and calls kill on that pid.
Code:

#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

main()
{
  pid_t iParentPid = 0;

  iParentPid = getppid();
  kill( ParentPid, SIGHUP );
}


isajera 02-05-2002 11:34 PM

that would only work from the first shell - it wouldn't work inside X. what would work is to grep the output of 'ps -f' to get the shell program belonging to your user that has the lowest pid - that would be the first shell called after login, send it the SIGHUP.

acromi 02-05-2002 11:49 PM

i pretty much wanted this to work from the console. so that will work. but now that intrigues me to get it to work in X :)

thanks for the help guys

crabboy 02-05-2002 11:54 PM

I thought about a popen( "ps" ) type of deal, but you would really have to kill all the users processes. There is no guarantee that the lowest pid is the login shell especially on a busy system.


All times are GMT -5. The time now is 05:55 PM.