| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | htop - ProcessLocksScreen.c |
| 3 | (C) 2020 htop dev team |
| 4 | Released under the GNU GPLv2, see the COPYING file |
| 5 | in the source distribution for its full text. |
| 6 | */ |
| 7 | |
| 8 | #include "config.h" // IWYU pragma: keep |
| 9 | |
| 10 | #include "ProcessLocksScreen.h" |
| 11 | |
| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 12 | #include <inttypes.h> |
| 13 | #include <limits.h> |
| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 14 | #include <stdlib.h> |
| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 15 | |
| Christian Göttsche | 7cf5277 | 2020-11-18 14:26:30 +0100 | [diff] [blame] | 16 | #include "Panel.h" |
| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 17 | #include "Platform.h" |
| Christian Göttsche | 7cf5277 | 2020-11-18 14:26:30 +0100 | [diff] [blame] | 18 | #include "ProvideCurses.h" |
| 19 | #include "Vector.h" |
| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 20 | #include "XUtils.h" |
| 21 | |
| 22 | |
| 23 | ProcessLocksScreen* ProcessLocksScreen_new(const Process* process) { |
| 24 | ProcessLocksScreen* this = xMalloc(sizeof(ProcessLocksScreen)); |
| 25 | Object_setClass(this, Class(ProcessLocksScreen)); |
| 26 | if (Process_isThread(process)) |
| 27 | this->pid = process->tgid; |
| 28 | else |
| 29 | this->pid = process->pid; |
| Daniel Lange | 1ffe5d7 | 2021-01-11 12:53:07 +0100 | [diff] [blame] | 30 | return (ProcessLocksScreen*) InfoScreen_init(&this->super, process, NULL, LINES - 2, " ID TYPE EXCLUSION READ/WRITE DEVICE:INODE START END FILENAME"); |
| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void ProcessLocksScreen_delete(Object* this) { |
| 34 | free(InfoScreen_done((InfoScreen*)this)); |
| 35 | } |
| 36 | |
| 37 | static void ProcessLocksScreen_draw(InfoScreen* this) { |
| Christian Göttsche | 1d5b052 | 2020-11-25 12:43:30 +0100 | [diff] [blame] | 38 | InfoScreen_drawTitled(this, "Snapshot of file locks of process %d - %s", ((ProcessLocksScreen*)this)->pid, Process_getCommand(this->process)); |
| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static inline void FileLocks_Data_clear(FileLocks_Data* data) { |
| Benny Baumann | d431786 | 2020-11-13 21:34:21 +0100 | [diff] [blame] | 42 | free(data->locktype); |
| 43 | free(data->exclusive); |
| 44 | free(data->readwrite); |
| 45 | free(data->filename); |
| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static void ProcessLocksScreen_scan(InfoScreen* this) { |
| 49 | Panel* panel = this->display; |
| 50 | int idx = Panel_getSelectedIndex(panel); |
| 51 | Panel_prune(panel); |
| 52 | FileLocks_ProcessData* pdata = Platform_getProcessLocks(((ProcessLocksScreen*)this)->pid); |
| 53 | if (!pdata) { |
| 54 | InfoScreen_addLine(this, "This feature is not supported on your platform."); |
| 55 | } else if (pdata->error) { |
| 56 | InfoScreen_addLine(this, "Could not determine file locks."); |
| 57 | } else { |
| 58 | FileLocks_LockData* ldata = pdata->locks; |
| 59 | if (!ldata) { |
| 60 | InfoScreen_addLine(this, "No locks have been found for the selected process."); |
| 61 | } |
| 62 | while (ldata) { |
| 63 | FileLocks_Data* data = &ldata->data; |
| 64 | |
| 65 | char entry[512]; |
| 66 | if (ULLONG_MAX == data->end) { |
| 67 | xSnprintf(entry, sizeof(entry), "%10d %-10s %-10s %-10s %02x:%02x:%020"PRIu64" %20"PRIu64" %20s %s", |
| 68 | data->id, |
| Benny Baumann | d431786 | 2020-11-13 21:34:21 +0100 | [diff] [blame] | 69 | data->locktype, data->exclusive, data->readwrite, |
| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 70 | data->dev[0], data->dev[1], data->inode, |
| 71 | data->start, "<END OF FILE>", |
| Benny Baumann | d431786 | 2020-11-13 21:34:21 +0100 | [diff] [blame] | 72 | data->filename ? data->filename : "<N/A>" |
| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 73 | ); |
| 74 | } else { |
| 75 | xSnprintf(entry, sizeof(entry), "%10d %-10s %-10s %-10s %02x:%02x:%020"PRIu64" %20"PRIu64" %20"PRIu64" %s", |
| 76 | data->id, |
| Benny Baumann | d431786 | 2020-11-13 21:34:21 +0100 | [diff] [blame] | 77 | data->locktype, data->exclusive, data->readwrite, |
| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 78 | data->dev[0], data->dev[1], data->inode, |
| 79 | data->start, data->end, |
| Benny Baumann | d431786 | 2020-11-13 21:34:21 +0100 | [diff] [blame] | 80 | data->filename ? data->filename : "<N/A>" |
| Benny Baumann | 1876305 | 2020-11-11 22:15:35 +0100 | [diff] [blame] | 81 | ); |
| 82 | } |
| 83 | |
| 84 | InfoScreen_addLine(this, entry); |
| 85 | FileLocks_Data_clear(&ldata->data); |
| 86 | |
| 87 | FileLocks_LockData* old = ldata; |
| 88 | ldata = ldata->next; |
| 89 | free(old); |
| 90 | } |
| 91 | } |
| 92 | free(pdata); |
| 93 | Vector_insertionSort(this->lines); |
| 94 | Vector_insertionSort(panel->items); |
| 95 | Panel_setSelected(panel, idx); |
| 96 | } |
| 97 | |
| 98 | const InfoScreenClass ProcessLocksScreen_class = { |
| 99 | .super = { |
| 100 | .extends = Class(Object), |
| 101 | .delete = ProcessLocksScreen_delete |
| 102 | }, |
| 103 | .scan = ProcessLocksScreen_scan, |
| 104 | .draw = ProcessLocksScreen_draw |
| 105 | }; |