| /* Common utmpx implementation |
| Copyright 2015-2023 Rivoreo |
| |
| Permission is hereby granted, free of charge, to any person obtaining |
| a copy of this software and associated documentation files (the |
| "Software"), to deal in the Software without restriction, including |
| without limitation the rights to use, copy, modify, merge, publish, |
| distribute, sublicense, and/or sell copies of the Software, and to |
| permit persons to whom the Software is furnished to do so, subject to |
| the following conditions: |
| |
| The above copyright notice and this permission notice shall be |
| included in all copies or substantial portions of the Software. |
| |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR |
| THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| */ |
| |
| #define _GNU_SOURCE |
| #include "utmpx.h" |
| #include <unistd.h> |
| #include <string.h> |
| #include <fcntl.h> |
| #include <errno.h> |
| #include <limits.h> |
| |
| static int utmpx_fd = -1; |
| static char utmpx_file[PATH_MAX+1] = UTMPX_FILE; |
| |
| static int open_utmpx() { |
| if(utmpx_fd != -1) return utmpx_fd; |
| utmpx_fd = open(utmpx_file, O_RDWR); |
| return utmpx_fd; |
| } |
| |
| void setutxent() { |
| if(utmpx_fd == -1) open_utmpx(); |
| else lseek(utmpx_fd, 0, SEEK_SET); |
| } |
| |
| void endutxent() { |
| if(utmpx_fd == -1) return; |
| close(utmpx_fd); |
| utmpx_fd = -1; |
| } |
| |
| struct utmpx *getutxent() { |
| static struct utmpx buffer; |
| if(utmpx_fd == -1 && open_utmpx() == -1) return NULL; |
| int s = read(utmpx_fd, &buffer, sizeof buffer); |
| if(s < (int)sizeof buffer) return NULL; |
| return &buffer; |
| } |
| |
| struct utmpx *getutxid(const struct utmpx *id) { |
| static struct utmpx buffer; |
| if(utmpx_fd == -1 && open_utmpx() == -1) return NULL; |
| int s; |
| while((s = read(utmpx_fd, &buffer, sizeof buffer))) { |
| if(s == -1 && (errno == EINTR || errno == EAGAIN)) continue; |
| if(s < (int)sizeof buffer) break; |
| switch(id->ut_type) { |
| case RUN_LVL: |
| case BOOT_TIME: |
| case NEW_TIME: |
| case OLD_TIME: |
| if(id->ut_type == buffer.ut_type) return &buffer; |
| break; |
| case INIT_PROCESS: |
| case LOGIN_PROCESS: |
| case USER_PROCESS: |
| case DEAD_PROCESS: |
| switch(buffer.ut_type) { |
| case INIT_PROCESS: |
| case LOGIN_PROCESS: |
| case USER_PROCESS: |
| case DEAD_PROCESS: |
| if(memcmp(id->ut_id, buffer.ut_id, sizeof buffer.ut_id) == 0) return &buffer; |
| break; |
| } |
| break; |
| default: |
| return NULL; |
| } |
| } |
| return NULL; |
| } |
| |
| struct utmpx *getutxline(const struct utmpx *line) { |
| static struct utmpx buffer; |
| if(utmpx_fd == -1 && open_utmpx() == -1) return NULL; |
| int s; |
| while((s = read(utmpx_fd, &buffer, sizeof buffer))) { |
| if(s == -1 && (errno == EINTR || errno == EAGAIN)) continue; |
| if(s < (int)sizeof buffer) break; |
| switch(buffer.ut_type) { |
| case LOGIN_PROCESS: |
| case USER_PROCESS: |
| if(strncmp(line->ut_line, buffer.ut_line, sizeof buffer.ut_line) == 0) return &buffer; |
| break; |
| } |
| } |
| return NULL; |
| } |
| |
| static int lock_write_end(int fd, const void *buffer, size_t count) { |
| int r, e; |
| if(lockf(fd, F_LOCK, 0) < 0) return -1; |
| if(lseek(fd, 0, SEEK_END) == (off_t)-1) { |
| r = -1; |
| e = errno; |
| goto end; |
| } |
| do { |
| r = write(fd, buffer, count); |
| } while(r < 0 && ((e = errno) == EINTR || e == EAGAIN)); |
| end: |
| lockf(fd, F_ULOCK, 0); |
| if(r < 0) errno = e; |
| return r; |
| } |
| |
| struct utmpx *pututxline(const struct utmpx *utx) { |
| static struct utmpx buffer; |
| if(!utx) { |
| errno = EFAULT; |
| return NULL; |
| } |
| //if(utmpx_fd == -1 && open_utmpx() == -1) return NULL; |
| setutxent(); |
| if(utmpx_fd == -1) return NULL; |
| memcpy(&buffer, utx, sizeof buffer); |
| struct utmpx *old = getutxid(utx); |
| if(old) { |
| lseek(utmpx_fd, -(off_t)sizeof buffer, SEEK_CUR); |
| int s; |
| do { |
| s = write(utmpx_fd, &buffer, sizeof buffer); |
| } while(s == -1 && (errno == EINTR || errno == EAGAIN)); |
| if(s < 0) return NULL; |
| } else if(lock_write_end(utmpx_fd, &buffer, sizeof buffer) < 0) { |
| return NULL; |
| } |
| return &buffer; |
| } |
| |
| #undef utmpxname |
| int utmpxname(const char *name) { |
| if(!name) { |
| errno = EFAULT; |
| return -1; |
| } |
| size_t len = strlen(name) + 1; |
| if(len > sizeof utmpx_file) { |
| errno = ENAMETOOLONG; |
| return -1; |
| } |
| memcpy(utmpx_file, name, len); |
| return 0; |
| } |