Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement -U for NetBSD. #2118

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/man.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
39 changes: 36 additions & 3 deletions src/bsdcommon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ bool bsdcommon::init_kvm() {
}

kvm_initialised = true;
return false;
return true;
}

void bsdcommon::deinit_kvm() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -309,11 +309,44 @@ void bsdcommon::update_top_info() {
continue;
}

proc = get_process(get_pd(p));
proc = get_process(get_pid(p));
if (!proc) {
continue;
}

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;
}
3 changes: 2 additions & 1 deletion src/bsdcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_*/
8 changes: 4 additions & 4 deletions src/conky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
};

Expand Down
10 changes: 5 additions & 5 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/netbsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,6 @@ int update_diskio(void) {
return 1;
}

bool is_conky_already_running() {
return bsdcommon::is_conky_already_running();
}
2 changes: 2 additions & 0 deletions src/netbsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
int get_entropy_avail(unsigned int *);
int get_entropy_poolsize(unsigned int *);

bool is_conky_already_running();

#endif /*NETBSD_H_*/
Loading