# User Log in



## uslanmaz (Nov 25, 2009)

Hi everyone,

I am trying to do something when a user logs in and logs out the system. I have implemented my functions and run them via syscall and they work fine. but I could not find which function runs when a user logs in so that I can call my function from that and which function runs when logs out. I tried to use pam_sm_open session() but could not call my function from there. actually I guess it is not the right function to do what I want, because nothing happen when user logs in Altoug I put some printf in the pam_sm_open_session. which functions gets run when a user logs in in kernel?

Thanks..


----------



## anemos (Nov 25, 2009)

Did you check /usr/src/usr.bin/login/login.c ?
Note that it is a userspace process.


----------



## DutchDaemon (Nov 25, 2009)

OP, could you _please_ stop posting in FreeBSD Development? Your personal programming efforts belong in Userland Programming & Scripting.


----------



## uslanmaz (Nov 26, 2009)

I tried to call my function from login.c, but still my function does not get called. I need to find out which file or function in the kernel source code gets called just after a successful login and log out.


----------



## SirDice (Nov 26, 2009)

> Immediately after logging a user in, login displays the system copyright notice, the date and time the user last logged in, the message of the day as well as other information. If the file .hushlogin exists in the user's home directory, all of these messages are suppressed. This is to simplify logins for non-human users, such as uucp(1).


From login(1).


----------



## anemos (Nov 26, 2009)

Right after /sbin/init starts until the user gets prompted and authenticated to log in to the system, all this work is handled by user-level processes using the system call interface. So, there's not much you can do kernel-wise. Plus, it is not recommended to place code arbitrarily into the kernel. I can accept however that you want to experiment so everything is allowed.  

Unfortunately, you haven't given a good description of your problem, therefore, I don't think you'll get a satisfactory answer to your question.

Please either share more info or even better post some code so we can better be aware of what you're searching for. Fear not. No one's going to steal your ideas. :e

I insist however on looking at /usr/bin/login's code closer.
Where did you put your code and how did you compile?


----------



## expl (Nov 26, 2009)

Look at /usr/src/usr.bin/login/login.c

To handle login:

```
...
[color="DarkRed"]/*insert your code here before shell takes over*/[/color]
[color="Green"]Line 643:[/color] execlp(shell, arg0, (char *)0);
...
```

To handle logoff:

```
...
[color="Green"]Line 546:[/color] waitpid(pid, &status, 0);
[color="DarkRed"]/*insert your code here before cleanup is done*/[/color]
...
```


----------



## uslanmaz (Nov 26, 2009)

the thing I am trying to do is, I have a function in kern_switch.c which increments one of the global variables in the same file by one. when a user logs in, I want to call the function to increment the variable. therfore basically the variable will always be equal to the number of user? Can I call my function from the login.c directly? I added the function in proc.h and include the proc.h in login.c


----------



## uslanmaz (Nov 26, 2009)

I need to find the place where the prinf prints 


```
Nov 26 13:30:12 (date time) login:ROOT LOGIN (root) ON ttyv1
```


----------



## uslanmaz (Nov 26, 2009)

Dear expl,

I have some other things in those lines. now I am trying to find this lines waitpid and execlp, I hope I can find and they will work


----------



## uslanmaz (Nov 26, 2009)

To wrap up my question:

in proc.h


```
void increment(void);
```

in kernel_switch.c


```
int a=0;
void
increment(void)
{
   a=a+1;
}
```

now whenever a user logs in I want to call increment in the kern_switch.c? should I use a syscall and where?


----------

