Skip to content

Commit a0ee703

Browse files
g0mb4brndnmtthws
authored andcommitted
Implement -U for FreeBSD.
Tested on FreeBSD 14.1. Takes part of brndnmtthws#2072.
1 parent 9e265a3 commit a0ee703

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

doc/man.md.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ file.
163163
**-U \| \--unique**
164164

165165
: Conky won't start if another Conky process is already running. Implemented
166-
only for Linux.
166+
only for Linux and FreeBSD.
167167

168168
**-v \| -V \| \--version**
169169

src/conky.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -2107,9 +2107,9 @@ void set_current_config() {
21072107
/* : means that character before that takes an argument */
21082108
const char *getopt_string =
21092109
"vVqdDSs:t:u:i:hc:p:"
2110-
#if defined(__linux__)
2110+
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
21112111
"U"
2112-
#endif
2112+
#endif /* Linux || FreeBSD */
21132113
#ifdef BUILD_X11
21142114
"x:y:w:a:X:m:f:"
21152115
#ifdef OWN_WINDOW
@@ -2140,9 +2140,9 @@ const struct option longopts[] = {
21402140
#endif /* BUILD_X11 */
21412141
{"text", 1, nullptr, 't'}, {"interval", 1, nullptr, 'u'},
21422142
{"pause", 1, nullptr, 'p'},
2143-
#if defined(__linux__)
2143+
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
21442144
{"unique", 0, nullptr, 'U'},
2145-
#endif /* Linux */
2145+
#endif /* Linux || FreeBSD */
21462146
{nullptr, 0, nullptr, 0}
21472147
};
21482148

src/freebsd.cc

+36
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <devstat.h>
4747
#include <ifaddrs.h>
4848
#include <limits.h>
49+
#include <paths.h>
4950
#include <unistd.h>
5051

5152
#include <dev/acpica/acpiio.h>
@@ -778,3 +779,38 @@ void print_sysctlbyname(struct text_object *obj, char *p,
778779
snprintf(p, p_max_size, "%lu", (unsigned long)val[0]);
779780
}
780781
}
782+
783+
/******************************************
784+
* Check if more than one conky process *
785+
* is running *
786+
******************************************/
787+
788+
bool is_conky_already_running(void) {
789+
kvm_t *kd;
790+
struct kinfo_proc *kp;
791+
char errbuf[_POSIX2_LINE_MAX];
792+
int entries = -1;
793+
int instances = 0;
794+
795+
kd = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, errbuf);
796+
if (kd == NULL) {
797+
NORM_ERR("%s\n", errbuf);
798+
return false;
799+
}
800+
801+
kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &entries);
802+
if ((kp == NULL && errno != ESRCH) || (kp != NULL && entries < 0)) {
803+
NORM_ERR("%s\n", kvm_geterr(kd));
804+
goto cleanup;
805+
}
806+
807+
for (int i = 0; i < entries && instances < 2; ++i) {
808+
if (!strcmp("conky", kp[i].ki_comm)) {
809+
++instances;
810+
}
811+
}
812+
813+
cleanup:
814+
kvm_close(kd);
815+
return instances > 1;
816+
}

src/freebsd.h

+2
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ int get_entropy_avail(unsigned int *);
4747
int get_entropy_poolsize(unsigned int *);
4848
void print_sysctlbyname(struct text_object *, char *, unsigned int);
4949

50+
bool is_conky_already_running(void);
51+
5052
#endif /*FREEBSD_H_*/

src/main.cc

+6-6
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ static void print_help(const char *prog_name) {
273273
" (and quit)\n"
274274
" -p, --pause=SECS pause for SECS seconds at startup "
275275
"before doing anything\n"
276-
#if defined(__linux__)
276+
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
277277
" -U, --unique only one conky process can be created\n"
278-
#endif /* Linux */
278+
#endif /* Linux || FreeBSD */
279279
, prog_name);
280280
}
281281

@@ -358,22 +358,22 @@ int main(int argc, char **argv) {
358358
window.window = strtol(optarg, nullptr, 0);
359359
break;
360360
#endif /* BUILD_X11 */
361-
#if defined(__linux__)
361+
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
362362
case 'U':
363363
unique_process = true;
364364
break;
365-
#endif /* Linux */
365+
#endif /* Linux || FreeBSD */
366366
case '?':
367367
return EXIT_FAILURE;
368368
}
369369
}
370370

371-
#if defined(__linux__)
371+
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
372372
if (unique_process && is_conky_already_running()) {
373373
NORM_ERR("already running");
374374
return 0;
375375
}
376-
#endif /* Linux */
376+
#endif /* Linux || FreeBSD */
377377

378378
try {
379379
set_current_config();

0 commit comments

Comments
 (0)