11-15-2003
Read paragraph 3 of my first post for comments on getuid(). Hmmm, I see that I said to use getpwnam(). Actually, since we have the uid, we should use getpwuid().
ctermid() is a silly little function that always returns the string "/dev/tty". I will never understand why people write functions to return a constant.
cuserid() was dropped from posix in 1990. It is still available on many systems, but I recommend that it not be used for new code. It first tries the technique I mentioned in the second paragraph of my first post. If that fails, it then uses the getuid() technique that I mentioned in my third paragraph. No information is available to the caller about which technique worked. And it never tries the technique mentioned in my first paragraph which posix seems to promote as the preferred method.
Also there is getlogin() which automates looking up the controlling terminal in utmp.
I have the uneasy feeling that there are more functions that automate some conbination of the 3 techniques that I mentioned. It's dangerous to be sure of a negative, but I'm rather sure that there isn't a 4th technique lurking around.
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
hi all,
kindly let me know the command to get password and user for current user in unix.
thanks in advance.
--Bali Reddy.Y (1 Reply)
Discussion started by: balireddy_77
1 Replies
2. Shell Programming and Scripting
I have crated a file xx wth env redirected into it 5 times n couldnot proceed further
n next we have to create a script that takes 1 argument being a file, in this instance we use the newly created file above xx(xx file contains the env redirected into it 5 times) read the inputted file, in... (1 Reply)
Discussion started by: bobby36
1 Replies
3. Shell Programming and Scripting
When I typed in ps -a I get this:
PID TTY TIME CMD
31799 pts/3 00:00:00 vim
31866 pts/3 00:00:00 vim
And to check who is currently logged in, I type who
Felix Whoals
Tada Whoals
Lala Whoals
How can I get the user process for all current users who logged in?? I think I need to combine... (14 Replies)
Discussion started by: felixwhoals
14 Replies
4. UNIX for Dummies Questions & Answers
I'm trying to write a grep command that will be good for the current user of the command but it doesn't work on my server with bash shell. On the server with tcsh I used:
last | grep "$user" and the current user could see how many times they logged on this month. On the other server it doesn't... (11 Replies)
Discussion started by: steveu
11 Replies
5. Shell Programming and Scripting
Ok, so, in order to install some dependencies of a program I made, a script has to be run as root. The thing is that I have to copy some things into the home folder of currently logged in user, but the variable $HOME returns '/root' and the $USER returns 'root' :(
Is there any way to see who is... (7 Replies)
Discussion started by: hakermania
7 Replies
6. Shell Programming and Scripting
Hey guys
I need a script that reads a login name and verifies if that user is currently logged in
i have found few commands like "who" and "users"
but i wonder how can i verify it that login name is logged in or not? (3 Replies)
Discussion started by: nishrestha
3 Replies
7. Shell Programming and Scripting
Hi Every body,
I would need a shell script program to login as different user and perform some copy commands in the script.
example: Supppose ora_toms is the active user
ora_toms should be able to run a script where user: ftptomsp pass: XXX should login through and run the commands
... (9 Replies)
Discussion started by: ujjwal27
9 Replies
8. Shell Programming and Scripting
Hi,
'ps -ef' returns output of the following format
UID PID PPID C STIME TTY TIME CMD
root 17573 1 0 Sep12 tty6 00:00:00 /sbin/mingetty tty6
hpsmh 18150 14864 0 Sep12 ? 00:00:00 /opt/hp/hpsmh/sbin/hpsmhd -DSSL -f /opt/hp/hpsmh/conf/smhpd.conf
root ... (3 Replies)
Discussion started by: ysrini
3 Replies
9. UNIX for Dummies Questions & Answers
In any non-root account, whenever I enter mail, it gives me:
/var/spool/mail/root: Permission deniedI am not logged in as root, why is mail accessing root's mailbox ?
I am unable to enter the currently logged in user's mailbox.
Any help is appreciated :) (2 Replies)
Discussion started by: Hijanoqu
2 Replies
10. Shell Programming and Scripting
Hi,
i need to list the processes running only under current logged in user.
EX:
$ whoami
oraaqw
$ ps -ef | grep tnslsnr
oraaqw 11403300 19267592 0 09:14:47 pts/3 0:00 grep tnslsnr
oraaqw 15794208 1 0 Jan 14 - 11:59... (6 Replies)
Discussion started by: aravindadla
6 Replies
LEARN ABOUT SUSE
getlogin
GETLOGIN(3) Linux Programmer's Manual GETLOGIN(3)
NAME
getlogin, getlogin_r, cuserid - get username
SYNOPSIS
#include <unistd.h>
char *getlogin(void);
int getlogin_r(char *buf, size_t bufsize);
#include <stdio.h>
char *cuserid(char *string);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
getlogin_r(): _REENTRANT || _POSIX_C_SOURCE >= 199506L
cuserid(): _XOPEN_SOURCE
DESCRIPTION
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. The string is statically allocated and might be overwritten on subsequent calls to this
function or to cuserid().
getlogin_r() returns this same username in the array buf of size bufsize.
cuserid() returns a pointer to a string containing a username associated with the effective user ID of the process. If string is not a
null pointer, it should be an array that can hold at least L_cuserid characters; the string is returned in this array. Otherwise, a
pointer to a string in a static area is returned. This string is statically allocated and might be overwritten on subsequent calls to this
function or to getlogin().
The macro L_cuserid is an integer constant that indicates how long an array you might need to store a username. L_cuserid is declared in
<stdio.h>.
These functions let your program identify positively the user who is running (cuserid()) or the user who logged in this session (getlo-
gin()). (These can differ when set-user-ID programs are involved.)
For most purposes, it is more useful to use the environment variable LOGNAME to find out who the user is. This is more flexible precisely
because the user can set LOGNAME arbitrarily.
RETURN VALUE
getlogin() returns a pointer to the username when successful, and NULL on failure. getlogin_r() returns 0 when successful, and nonzero on
failure.
ERRORS
POSIX specifies
EMFILE The calling process already has the maximum allowed number of open files.
ENFILE The system already has the maximum allowed number of open files.
ENXIO The calling process has no controlling tty.
ERANGE (getlogin_r) The length of the username, including the terminating null byte, is larger than bufsize.
Linux/glibc also has
ENOENT There was no corresponding entry in the utmp-file.
ENOMEM Insufficient memory to allocate passwd structure.
ENOTTY Standard input didn't refer to a terminal. (See BUGS.)
FILES
/etc/passwd
password database file
/var/run/utmp
(traditionally /etc/utmp; some libc versions used /var/adm/utmp)
CONFORMING TO
getlogin() and getlogin_r() specified in POSIX.1-2001.
System V has a cuserid() function which uses the real user ID rather than the effective user ID. The cuserid() function was included in
the 1988 version of POSIX, but removed from the 1990 version. It was present in SUSv2, but removed in POSIX.1-2001.
OpenBSD has getlogin() and setlogin(), and a username associated with a session, even if it has no controlling tty.
BUGS
Unfortunately, it is often rather easy to fool getlogin(). Sometimes it does not work at all, because some program messed up the utmp
file. Often, it gives only the first 8 characters of the login name. The user currently logged in on the controlling tty of our program
need not be the user who started it. Avoid getlogin() for security-related purposes.
Note that glibc does not follow the POSIX specification and uses stdin instead of /dev/tty. A bug. (Other recent systems, like SunOS 5.8
and HP-UX 11.11 and FreeBSD 4.8 all return the login name also when stdin is redirected.)
Nobody knows precisely what cuserid() does; avoid it in portable programs. Or avoid it altogether: use getpwuid(geteuid()) instead, if
that is what you meant. Do not use cuserid().
SEE ALSO
geteuid(2), getuid(2), utmp(5)
COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU
2008-06-29 GETLOGIN(3)