Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f40bdfa

Browse files
ikerexxehallyn
authored andcommitted
libmisc: implement get_session_host()
Implement `get_session_host()` in `utmp.c` and `logind.c`. Signed-off-by: Iker Pedrosa <[email protected]>
1 parent fb35ad1 commit f40bdfa

4 files changed

Lines changed: 76 additions & 2 deletions

File tree

lib/prototypes.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,19 @@ extern int set_filesize_limit (int blocks);
472472
/* user_busy.c */
473473
extern int user_busy (const char *name, uid_t uid);
474474

475-
/* utmp.c */
475+
/*
476+
* Session management: utmp.c or logind.c
477+
*/
478+
479+
/**
480+
* @brief Get host for the current session
481+
*
482+
* @param[out] out Host name
483+
*
484+
* @return 0 or a positive integer if the host was obtained properly,
485+
* another value on error.
486+
*/
487+
extern int get_session_host (char **out);
476488
extern /*@null@*/struct utmp *get_current_utmp (void);
477489
extern struct utmp *prepare_utmp (const char *name,
478490
const char *line,

libmisc/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@ endif
9494

9595
if ENABLE_LASTLOG
9696
libmisc_la_SOURCES += log.c
97-
endif
97+
endif

libmisc/logind.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2023, Iker Pedrosa <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include <config.h>
8+
9+
#ident "$Id$"
10+
11+
#include "defines.h"
12+
#include "prototypes.h"
13+
14+
#include <systemd/sd-login.h>
15+
16+
int get_session_host (char **out)
17+
{
18+
char *host = NULL;
19+
char *session = NULL;
20+
int ret;
21+
22+
ret = sd_pid_get_session (getpid(), &session);
23+
if (ret < 0) {
24+
return ret;
25+
}
26+
ret = sd_session_get_remote_host (session, &host);
27+
if (ret < 0) {
28+
goto done;
29+
}
30+
31+
*out = host;
32+
33+
done:
34+
free (session);
35+
return ret;
36+
}

libmisc/utmp.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,32 @@ static bool is_my_tty (const char tty[UT_LINESIZE])
101101
return ret;
102102
}
103103

104+
int get_session_host (char **out)
105+
{
106+
char *hostname = NULL;
107+
struct utmp *ut = NULL;
108+
int ret = 0;
109+
110+
ut = get_current_utmp();
111+
112+
#ifdef HAVE_STRUCT_UTMP_UT_HOST
113+
if ((ut != NULL) && (ut->ut_host[0] != '\0')) {
114+
hostname = XMALLOC(sizeof(ut->ut_host) + 1, char);
115+
strncpy (hostname, ut->ut_host, sizeof (ut->ut_host));
116+
hostname[sizeof (ut->ut_host)] = '\0';
117+
*out = hostname;
118+
free (ut);
119+
} else {
120+
*out = NULL;
121+
ret = -2;
122+
}
123+
#else
124+
*out = NULL;
125+
ret = -2;
126+
#endif /* HAVE_STRUCT_UTMP_UT_HOST */
127+
128+
return ret;
129+
}
104130

105131
#ifndef USE_PAM
106132
/*

0 commit comments

Comments
 (0)