diff --git a/doc/man.md.j2 b/doc/man.md.j2 index bd9db7fb..926a1cbb 100644 --- a/doc/man.md.j2 +++ b/doc/man.md.j2 @@ -163,7 +163,7 @@ file. **-U \| \--unique** : Conky won't start if another Conky process is already running. Implemented - only for Linux, FreeBSD and Haiku. + only for Linux, FreeBSD, NetBSD and Haiku. **-v \| -V \| \--version** diff --git a/src/bsdcommon.cc b/src/bsdcommon.cc index da1da895..b67f8901 100644 --- a/src/bsdcommon.cc +++ b/src/bsdcommon.cc @@ -53,7 +53,7 @@ bool bsdcommon::init_kvm() { } kvm_initialised = true; - return false; + return true; } void bsdcommon::deinit_kvm() { @@ -223,7 +223,7 @@ static bool is_top_process(BSD_COMMON_PROC_STRUCT *p) { #endif } -static int32_t get_pd(BSD_COMMON_PROC_STRUCT *p) { +static int32_t get_pid(BSD_COMMON_PROC_STRUCT *p) { #if defined(__NetBSD__) return p->p_pid; #else @@ -309,7 +309,7 @@ void bsdcommon::update_top_info() { continue; } - proc = get_process(get_pd(p)); + proc = get_process(get_pid(p)); if (!proc) { continue; } @@ -317,3 +317,36 @@ void bsdcommon::update_top_info() { proc_from_bsdproc(proc, p); } } + +static bool is_process(BSD_COMMON_PROC_STRUCT *p, const char *name) { +#if defined(__NetBSD__) + return p->p_comm[0] != 0 && strcmp(p->p_comm, name) == 0; +#else + #error Not supported BSD system +#endif +} + +bool bsdcommon::is_conky_already_running() { + if (!init_kvm()) { + return false; + } + + struct process *proc = nullptr; + short unsigned int nprocs = 0; + int instances = 0; + + BSD_COMMON_PROC_STRUCT *ps = get_processes(&nprocs); + if (ps == nullptr) { + return false; + } + + for (int i = 0; i < nprocs && instances < 2; ++i) { + BSD_COMMON_PROC_STRUCT *p = &ps[i]; + + if (is_process(p, "conky")) { + ++instances; + } + } + + return instances > 1; +} diff --git a/src/bsdcommon.h b/src/bsdcommon.h index 5c4047ff..e50085c1 100644 --- a/src/bsdcommon.h +++ b/src/bsdcommon.h @@ -56,6 +56,7 @@ namespace bsdcommon { void get_number_of_running_processes(short unsigned int *run_procs); void update_top_info(); -}; + bool is_conky_already_running(); +} #endif /*BSDCOMMON_H_*/ diff --git a/src/conky.cc b/src/conky.cc index c450365c..889109c1 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -2110,9 +2110,9 @@ void set_current_config() { const char *getopt_string = "vVqdDSs:t:u:i:hc:p:" #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ - defined(__HAIKU__) + defined(__HAIKU__) || defined(__NetBSD__) "U" -#endif /* Linux || FreeBSD || Haiku */ +#endif /* Linux || FreeBSD || Haiku || NetBSD */ #ifdef BUILD_X11 "x:y:w:a:X:m:f:" #ifdef OWN_WINDOW @@ -2144,9 +2144,9 @@ const struct option longopts[] = { {"text", 1, nullptr, 't'}, {"interval", 1, nullptr, 'u'}, {"pause", 1, nullptr, 'p'}, #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ - defined(__HAIKU__) + defined(__HAIKU__) || defined(__NetBSD__) {"unique", 0, nullptr, 'U'}, -#endif /* Linux || FreeBSDi || Haiku */ +#endif /* Linux || FreeBSDi || Haiku || NetBSD */ {nullptr, 0, nullptr, 0} }; diff --git a/src/main.cc b/src/main.cc index c83ab8c6..b7a8545f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -282,9 +282,9 @@ static void print_help(const char *prog_name) { " -p, --pause=SECS pause for SECS seconds at startup " "before doing anything\n" #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ - defined(__HAIKU__) + defined(__HAIKU__) || defined(__NetBSD__) " -U, --unique only one conky process can be created\n" -#endif /* Linux || FreeBSD || Haiku */ +#endif /* Linux || FreeBSD || Haiku || NetBSD */ , prog_name); } @@ -368,18 +368,18 @@ int main(int argc, char **argv) { break; #endif /* BUILD_X11 */ #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ - defined(__HAIKU__) + defined(__HAIKU__) || defined(__NetBSD__) case 'U': unique_process = true; break; -#endif /* Linux || FreeBSD || Haiku */ +#endif /* Linux || FreeBSD || Haiku || NetBSD */ case '?': return EXIT_FAILURE; } } #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ - defined(__HAIKU__) + defined(__HAIKU__) || defined(__NetBSD__) if (unique_process && is_conky_already_running()) { NORM_ERR("already running"); return 0; diff --git a/src/netbsd.cc b/src/netbsd.cc index 40a307c4..e18343c5 100644 --- a/src/netbsd.cc +++ b/src/netbsd.cc @@ -322,3 +322,6 @@ int update_diskio(void) { return 1; } +bool is_conky_already_running() { + return bsdcommon::is_conky_already_running(); +} diff --git a/src/netbsd.h b/src/netbsd.h index 62be0fc4..e9fb7deb 100644 --- a/src/netbsd.h +++ b/src/netbsd.h @@ -20,4 +20,6 @@ int get_entropy_avail(unsigned int *); int get_entropy_poolsize(unsigned int *); +bool is_conky_already_running(); + #endif /*NETBSD_H_*/