From 6456f85788a8c526d73957656aaf2a5a04091aa7 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 6 Apr 2016 16:31:36 -0700 Subject: [PATCH] Disallow running as root without --user-data-dir This commit adds the --user-data-dir argument which allows a custom data dir for Chromium's user data. This is useful when running as root on Linux since the root user does not have read or write permissions for the ~/.config/Code dir. Part of #3068 --- resources/linux/bin/code.sh | 20 +++++++++++++++++++- src/main.js | 8 ++++++++ src/vs/workbench/electron-main/cli.ts | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/resources/linux/bin/code.sh b/resources/linux/bin/code.sh index 0ebb4a496fcdc..ce8e57287591a 100755 --- a/resources/linux/bin/code.sh +++ b/resources/linux/bin/code.sh @@ -3,6 +3,23 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. +ARGS=$@ + +# If root, ensure that --user-data-dir is specified +if [ "$(id -u)" = "0" ]; then + while test $# -gt 0 + do + if [[ $1 == --user-data-dir=* ]]; then + DATA_DIR_SET=1 + fi + shift + done + if [ -z $DATA_DIR_SET ]; then + echo "It is recommended to start vscode as a normal user. To run as root, you must specify an alternate user data directory with the --user-data-dir argument." 1>&2 + exit 1 + fi +fi + if [ ! -L $0 ]; then # if path is not a symlink, find relatively VSCODE_PATH="$(dirname $0)/.." @@ -15,7 +32,8 @@ else VSCODE_PATH="/usr/share/@@NAME@@" fi fi + ELECTRON="$VSCODE_PATH/@@NAME@@" CLI="$VSCODE_PATH/resources/app/out/cli.js" -ATOM_SHELL_INTERNAL_RUN_AS_NODE=1 "$ELECTRON" "$CLI" "$@" +ATOM_SHELL_INTERNAL_RUN_AS_NODE=1 "$ELECTRON" "$CLI" $ARGS exit $? \ No newline at end of file diff --git a/src/main.js b/src/main.js index 2d69ec46136cc..839f5b226a96f 100644 --- a/src/main.js +++ b/src/main.js @@ -110,6 +110,14 @@ if (process.env.VSCODE_DEV) { app.setPath('userData', path.join(appData, 'Code-Development')); } +// Use custom user data dir if specified, required to run as root on Linux +var args = process.argv; +args.forEach(function (arg) { + if (arg.indexOf('--user-data-dir=') === 0) { + app.setPath('userData', arg.split('=')[1]); + } +}); + // Mac: when someone drops a file to the not-yet running VSCode, the open-file event fires even before // the app-ready event. We listen very early for open-file and remember this upon startup as path to open. global.macOpenFiles = []; diff --git a/src/vs/workbench/electron-main/cli.ts b/src/vs/workbench/electron-main/cli.ts index 21c489432d3c0..e675fdc3556ad 100644 --- a/src/vs/workbench/electron-main/cli.ts +++ b/src/vs/workbench/electron-main/cli.ts @@ -40,6 +40,8 @@ ${ indent }--locale=LOCALE The locale to use (e.g. en-US or zh-TW). ${ indent }-n, --new-window Force a new instance of Code. ${ indent }-r, --reuse-window Force opening a file or folder in the last active ${ indent } window. +${ indent }--user-data-dir=DIR Specifies the directory that user data is kept in, +${ indent } useful when running as root. ${ indent }-v, --version Print version. ${ indent }-w, --wait Wait for the window to be closed before returning.`; }