1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2025-01-30 02:28:31 +00:00

Implement -U for FreeBSD.

Tested on FreeBSD 14.1.
Takes part of #2072.
This commit is contained in:
Tóth János 2024-11-13 11:04:18 +01:00 committed by Brenden Matthews
parent 9e265a37c2
commit a0ee703ee2
5 changed files with 49 additions and 11 deletions

View File

@ -163,7 +163,7 @@ file.
**-U \| \--unique** **-U \| \--unique**
: Conky won't start if another Conky process is already running. Implemented : Conky won't start if another Conky process is already running. Implemented
only for Linux. only for Linux and FreeBSD.
**-v \| -V \| \--version** **-v \| -V \| \--version**

View File

@ -2107,9 +2107,9 @@ void set_current_config() {
/* : means that character before that takes an argument */ /* : means that character before that takes an argument */
const char *getopt_string = const char *getopt_string =
"vVqdDSs:t:u:i:hc:p:" "vVqdDSs:t:u:i:hc:p:"
#if defined(__linux__) #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
"U" "U"
#endif #endif /* Linux || FreeBSD */
#ifdef BUILD_X11 #ifdef BUILD_X11
"x:y:w:a:X:m:f:" "x:y:w:a:X:m:f:"
#ifdef OWN_WINDOW #ifdef OWN_WINDOW
@ -2140,9 +2140,9 @@ const struct option longopts[] = {
#endif /* BUILD_X11 */ #endif /* BUILD_X11 */
{"text", 1, nullptr, 't'}, {"interval", 1, nullptr, 'u'}, {"text", 1, nullptr, 't'}, {"interval", 1, nullptr, 'u'},
{"pause", 1, nullptr, 'p'}, {"pause", 1, nullptr, 'p'},
#if defined(__linux__) #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
{"unique", 0, nullptr, 'U'}, {"unique", 0, nullptr, 'U'},
#endif /* Linux */ #endif /* Linux || FreeBSD */
{nullptr, 0, nullptr, 0} {nullptr, 0, nullptr, 0}
}; };

View File

@ -46,6 +46,7 @@
#include <devstat.h> #include <devstat.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <limits.h> #include <limits.h>
#include <paths.h>
#include <unistd.h> #include <unistd.h>
#include <dev/acpica/acpiio.h> #include <dev/acpica/acpiio.h>
@ -778,3 +779,38 @@ void print_sysctlbyname(struct text_object *obj, char *p,
snprintf(p, p_max_size, "%lu", (unsigned long)val[0]); snprintf(p, p_max_size, "%lu", (unsigned long)val[0]);
} }
} }
/******************************************
* Check if more than one conky process *
* is running *
******************************************/
bool is_conky_already_running(void) {
kvm_t *kd;
struct kinfo_proc *kp;
char errbuf[_POSIX2_LINE_MAX];
int entries = -1;
int instances = 0;
kd = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, errbuf);
if (kd == NULL) {
NORM_ERR("%s\n", errbuf);
return false;
}
kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &entries);
if ((kp == NULL && errno != ESRCH) || (kp != NULL && entries < 0)) {
NORM_ERR("%s\n", kvm_geterr(kd));
goto cleanup;
}
for (int i = 0; i < entries && instances < 2; ++i) {
if (!strcmp("conky", kp[i].ki_comm)) {
++instances;
}
}
cleanup:
kvm_close(kd);
return instances > 1;
}

View File

@ -47,4 +47,6 @@ int get_entropy_avail(unsigned int *);
int get_entropy_poolsize(unsigned int *); int get_entropy_poolsize(unsigned int *);
void print_sysctlbyname(struct text_object *, char *, unsigned int); void print_sysctlbyname(struct text_object *, char *, unsigned int);
bool is_conky_already_running(void);
#endif /*FREEBSD_H_*/ #endif /*FREEBSD_H_*/

View File

@ -273,9 +273,9 @@ static void print_help(const char *prog_name) {
" (and quit)\n" " (and quit)\n"
" -p, --pause=SECS pause for SECS seconds at startup " " -p, --pause=SECS pause for SECS seconds at startup "
"before doing anything\n" "before doing anything\n"
#if defined(__linux__) #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
" -U, --unique only one conky process can be created\n" " -U, --unique only one conky process can be created\n"
#endif /* Linux */ #endif /* Linux || FreeBSD */
, prog_name); , prog_name);
} }
@ -358,22 +358,22 @@ int main(int argc, char **argv) {
window.window = strtol(optarg, nullptr, 0); window.window = strtol(optarg, nullptr, 0);
break; break;
#endif /* BUILD_X11 */ #endif /* BUILD_X11 */
#if defined(__linux__) #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
case 'U': case 'U':
unique_process = true; unique_process = true;
break; break;
#endif /* Linux */ #endif /* Linux || FreeBSD */
case '?': case '?':
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
#if defined(__linux__) #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
if (unique_process && is_conky_already_running()) { if (unique_process && is_conky_already_running()) {
NORM_ERR("already running"); NORM_ERR("already running");
return 0; return 0;
} }
#endif /* Linux */ #endif /* Linux || FreeBSD */
try { try {
set_current_config(); set_current_config();