diff --git a/bin/varnishadm/varnishadm.c b/bin/varnishadm/varnishadm.c index 3f84afcc299..bc527cc9e56 100644 --- a/bin/varnishadm/varnishadm.c +++ b/bin/varnishadm/varnishadm.c @@ -1,6 +1,6 @@ /*- * Copyright (c) 2006 Verdens Gang AS - * Copyright (c) 2006-2015 Varnish Software AS + * Copyright (c) 2006-2026 Varnish Software AS * All rights reserved. * * Author: Cecilie Fritzvold @@ -67,6 +67,7 @@ #include "vapi/vsm.h" #include "vas.h" #include "vcli.h" +#include "vin.h" #include "vjsn.h" #include "vtcp.h" @@ -390,6 +391,8 @@ usage(int status) fprintf(stderr, "Usage: varnishadm [-h] [-n workdir] [-p] [-S secretfile] " "[-T [address]:port] [-t timeout] [command [...]]\n"); + fprintf(stderr, + " varnishadm -x workdir [name]\n"); fprintf(stderr, "\t-n is mutually exclusive with -S and -T\n"); exit(status); } @@ -452,11 +455,12 @@ t_arg_timeout(const char *t_arg) return (1); } -#define OPTARG "hn:pS:T:t:" +#define OPTARG "hn:pS:T:t:x:" int main(int argc, char * const *argv) { + char *wd; const char *T_arg = NULL; const char *S_arg = NULL; const char *n_arg = NULL; @@ -495,6 +499,21 @@ main(int argc, char * const *argv) case 't': t_arg = optarg; break; + case 'x': + if (argc != 3 && argc != 4) { + fprintf(stderr, + "-x must be the only option\n"); + usage(1); + } + if (strcmp(optarg, "workdir")) { + fprintf(stderr, "Invalid -x argument\n"); + usage(1); + } + wd = VIN_n_Arg(argc == 4 ? argv[3] : NULL); + AN(wd); + printf("%s\n", wd); + free(wd); + exit(0); default: usage(1); } diff --git a/bin/varnishtest/tests/u00022.vtc b/bin/varnishtest/tests/u00022.vtc new file mode 100644 index 00000000000..c6db648b5dc --- /dev/null +++ b/bin/varnishtest/tests/u00022.vtc @@ -0,0 +1,32 @@ +varnishtest "varnishadm -x workdir" + +# happy paths +shell { + # -x workdir + DEFAULT_WORKDIR=$(varnishadm -x workdir) + test -n "$DEFAULT_WORKDIR" + + # -x workdir foo: returns dirname(DEFAULT_WORKDIR)/foo + WORKDIR_FOO=$(varnishadm -x workdir foo) + EXPECTED_FOO=$(dirname "$DEFAULT_WORKDIR")/foo + test "$WORKDIR_FOO" = "$EXPECTED_FOO" + + # -x workdir /foo: returns /foo + WORKDIR_ABSOLUTE=$(varnishadm -x workdir /foo) + test "$WORKDIR_ABSOLUTE" = "/foo" +} + +# -x with other options is rejected +shell -exit 1 -match "-x must be the only option" { + varnishadm -n /tmp -x workdir +} + +# -x with invalid argument fails +shell -exit 1 -match "Invalid -x argument" { + varnishadm -x invalid +} + +# -x workdir with too many arguments is rejected +shell -exit 1 -match "-x must be the only option" { + varnishadm -x workdir arg1 arg2 +} \ No newline at end of file diff --git a/bin/varnishtest/vtest2 b/bin/varnishtest/vtest2 index b987871958a..11ee91098c7 160000 --- a/bin/varnishtest/vtest2 +++ b/bin/varnishtest/vtest2 @@ -1 +1 @@ -Subproject commit b987871958a2da46ec000e0b6c46a1407fd5010a +Subproject commit 11ee91098c7a1f0a1f8afab9a7356609bfd3cb2d diff --git a/doc/changes.rst b/doc/changes.rst index 7e89c785f53..e5758530390 100644 --- a/doc/changes.rst +++ b/doc/changes.rst @@ -49,6 +49,9 @@ Vinyl-Cache NEXT (2026-03-15) being retained as an alias. ``req.ttl`` is now deprecated, but no warning is emitted yet. It will be removed in a future version of Vinyl-Cache. (`4389`_) +* ``varnishadm -x workdir`` will print the default work directory and exit. This + is useful for tools that need to discover the VSM location in most setups. + ================================ Varnish-Cache 8.0.0 (2025-09-15) ================================