Skip to content

Commit

Permalink
Implement -U for FreeBSD.
Browse files Browse the repository at this point in the history
Tested on FreeBSD 14.1.
Takes part of #2072.
  • Loading branch information
g0mb4 authored and brndnmtthws committed Nov 13, 2024
1 parent 9e265a3 commit a0ee703
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
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.
only for Linux and FreeBSD.

**-v \| -V \| \--version**

Expand Down
8 changes: 4 additions & 4 deletions src/conky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2107,9 +2107,9 @@ void set_current_config() {
/* : means that character before that takes an argument */
const char *getopt_string =
"vVqdDSs:t:u:i:hc:p:"
#if defined(__linux__)
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
"U"
#endif
#endif /* Linux || FreeBSD */
#ifdef BUILD_X11
"x:y:w:a:X:m:f:"
#ifdef OWN_WINDOW
Expand Down Expand Up @@ -2140,9 +2140,9 @@ const struct option longopts[] = {
#endif /* BUILD_X11 */
{"text", 1, nullptr, 't'}, {"interval", 1, nullptr, 'u'},
{"pause", 1, nullptr, 'p'},
#if defined(__linux__)
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
{"unique", 0, nullptr, 'U'},
#endif /* Linux */
#endif /* Linux || FreeBSD */
{nullptr, 0, nullptr, 0}
};

Expand Down
36 changes: 36 additions & 0 deletions src/freebsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <devstat.h>
#include <ifaddrs.h>
#include <limits.h>
#include <paths.h>
#include <unistd.h>

#include <dev/acpica/acpiio.h>
Expand Down Expand Up @@ -778,3 +779,38 @@ void print_sysctlbyname(struct text_object *obj, char *p,
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;
}
2 changes: 2 additions & 0 deletions src/freebsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ int get_entropy_avail(unsigned int *);
int get_entropy_poolsize(unsigned int *);
void print_sysctlbyname(struct text_object *, char *, unsigned int);

bool is_conky_already_running(void);

#endif /*FREEBSD_H_*/
12 changes: 6 additions & 6 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ static void print_help(const char *prog_name) {
" (and quit)\n"
" -p, --pause=SECS pause for SECS seconds at startup "
"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"
#endif /* Linux */
#endif /* Linux || FreeBSD */
, prog_name);
}

Expand Down Expand Up @@ -358,22 +358,22 @@ int main(int argc, char **argv) {
window.window = strtol(optarg, nullptr, 0);
break;
#endif /* BUILD_X11 */
#if defined(__linux__)
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
case 'U':
unique_process = true;
break;
#endif /* Linux */
#endif /* Linux || FreeBSD */
case '?':
return EXIT_FAILURE;
}
}

#if defined(__linux__)
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
if (unique_process && is_conky_already_running()) {
NORM_ERR("already running");
return 0;
}
#endif /* Linux */
#endif /* Linux || FreeBSD */

try {
set_current_config();
Expand Down

0 comments on commit a0ee703

Please sign in to comment.