diff options
-rw-r--r-- | src/server/forkserver.nim | 4 | ||||
-rw-r--r-- | src/utils/proctitle.nim | 38 |
2 files changed, 42 insertions, 0 deletions
diff --git a/src/server/forkserver.nim b/src/server/forkserver.nim index 12b25dc3..5a123fcd 100644 --- a/src/server/forkserver.nim +++ b/src/server/forkserver.nim @@ -14,6 +14,7 @@ import server/buffer import types/urimethodmap import types/url import types/winattrs +import utils/proctitle import utils/strwidth import chagashi/charset @@ -86,6 +87,7 @@ proc forkLoader(ctx: var ForkServerContext, config: LoaderConfig): int = zeroMem(addr ctx, sizeof(ctx)) discard close(pipefd[0]) # close read try: + setProcessTitle("cha loader") runFileLoader(pipefd[1], config) except CatchableError: let e = getCurrentException() @@ -152,6 +154,7 @@ proc forkBuffer(ctx: var ForkServerContext): int = clientPid: pid ) try: + setBufferProcessTitle(url) launchBuffer(config, url, request, attrs, ishtml, charsetStack, loader, ssock) except CatchableError: @@ -171,6 +174,7 @@ proc forkBuffer(ctx: var ForkServerContext): int = return pid proc runForkServer() = + setProcessTitle("cha forkserver") var ctx = ForkServerContext( istream: newPosixStream(stdin.getFileHandle()), ostream: newPosixStream(stdout.getFileHandle()) diff --git a/src/utils/proctitle.nim b/src/utils/proctitle.nim new file mode 100644 index 00000000..4694be0b --- /dev/null +++ b/src/utils/proctitle.nim @@ -0,0 +1,38 @@ +import types/url + +when defined(freebsd): + proc c_setproctitle(fmt: cstring) {.header: "<unistd.h>", importc: + "setproctitle", varargs.} +elif defined(netbsd) or defined(openbsd): + proc c_setproctitle(fmt: cstring) {.header: "<stdlib.h>", importc: + "setproctitle", varargs.} +elif defined(linux): + let PR_SET_NAME {.importc, header: "<sys/prctl.h>", nodecl.}: cint + proc prctl(option: cint; arg2, arg3, arg4, arg5: culong): cint {.importc, + header: "<sys/prctl.h>".} + +proc setProcessTitle*(s: string) = + when defined(freebsd) or defined(netbsd) or defined(openbsd): + c_setproctitle("%s", cstring(s)) + elif defined(linux): + discard prctl(PR_SET_NAME, cast[culong](cstring(s)), 0, 0, 0) + +when defined(linux): + from std/strutils import startsWith, delete + from utils/twtstr import afterLast + +proc setBufferProcessTitle*(url: URL) = + when defined(linux): + # linux truncates to 15 chars; try to preserve important info + const initTitle = "cha buf " + var title = initTitle + var hostname = url.hostname + if hostname.startsWith("www."): + hostname.delete(0.."www.".high) + title &= hostname + if title.len > initTitle.len: + title &= ' ' + title &= url.pathname.afterLast('/') + setProcessTitle(title) + else: + setProcessTitle("cha buffer " & $url) |