File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -472,7 +472,19 @@ extern int set_filesize_limit (int blocks);
472472/* user_busy.c */
473473extern 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 );
476488extern /*@null@*/ struct utmp * get_current_utmp (void );
477489extern struct utmp * prepare_utmp (const char * name ,
478490 const char * line ,
Original file line number Diff line number Diff line change 9494
9595if ENABLE_LASTLOG
9696libmisc_la_SOURCES += log.c
97- endif
97+ endif
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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/*
You can’t perform that action at this time.
0 commit comments