From 70b67d388eb675c5b541116ac86af9afa5aca18d Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Wed, 26 Jul 2023 15:56:35 -0300 Subject: [PATCH] feat: add support for custom user and workdir in init --- skel/sbin/init | 70 ++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/skel/sbin/init b/skel/sbin/init index 832131e0..992a73b3 100755 --- a/skel/sbin/init +++ b/skel/sbin/init @@ -9,44 +9,43 @@ busybox mount -t tmpfs tmpfs /dev/shm -o nosuid,nodev,mode=1777 # net [ -f /etc/hostname ] && busybox hostname -F /etc/hostname -busybox ifconfig lo up +[ -z "$noloopback" ] && busybox ifconfig lo up -# custom init -if [ -f /proc/device-tree/cartesi/init ]; then - . /proc/device-tree/cartesi/init -fi +# source environment +[ -f /etc/environment ] && . /etc/environment +export PATH="$PATH:/opt/cartesi/bin" + +# execute init from device tree +[ -f /proc/device-tree/cartesi/init ] && . /proc/device-tree/cartesi/init -# use entrypoint from device tree when available +# set entrypoint from device tree when available if [ -s /proc/device-tree/cartesi/entrypoint ]; then ENTRYPOINT=". /proc/device-tree/cartesi/entrypoint" -else +elif [ -n "$*" ]; then ENTRYPOINT="$*" fi # execute entrypoint if [ -n "$ENTRYPOINT" ]; then - [ -f /etc/environment ] && . /etc/environment - export PATH="/opt/cartesi/bin:$PATH" - - # can login as dapp user? - if [ ! "$single" == "yes" ] && busybox id dapp &> /dev/null; then - if [ -c /dev/rollup ]; then - busybox chown root:dapp /dev/rollup - busybox chmod 660 /dev/rollup - fi - if [ -c /dev/yield ]; then - busybox chown root:dapp /dev/yield - busybox chmod 660 /dev/yield - fi - export HOME=~dapp USER=dapp LOGNAME=dapp - else - export HOME=~root USER=root LOGNAME=root + # set user + if [ -z "$USER" ]; then + busybox id dapp &> /dev/null && USER=dapp || USER=root fi - cd $HOME && + # give user group access to rollup and yield devices + [ -c /dev/rollup ] && \ + busybox chown :$(busybox id -g $USER) /dev/rollup && \ + busybox chmod g+rw /dev/rollup + [ -c /dev/yield ] && + busybox chown :$(busybox id -g $USER) /dev/yield && \ + busybox chmod g+rw /dev/yield + + # finally execute + (export USER LOGNAME=$USER HOME="$(eval echo ~$USER)"; \ + cd ${WORKDIR:-"$HOME"} && \ busybox setsid \ busybox cttyhack \ - busybox su -p $USER -c "$ENTRYPOINT" + busybox su -p $USER -c "$ENTRYPOINT") else echo "Nothing to do." fi @@ -55,15 +54,18 @@ busybox mount -o ro,remount / busybox umount -af busybox poweroff -f -# Copyright 2023 Cartesi Pte. Ltd. +# Copyright Cartesi and individual authors (see AUTHORS) +# SPDX-License-Identifier: LGPL-3.0-or-later +# +# This program is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. # -# Licensed under the Apache License, Version 2.0 (the "License"); you may not -# use this file except in compliance with the License. You may obtain a copy of -# the License at http://www.apache.org/licenses/LICENSE-2.0 +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. # -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations under -# the License. +# You should have received a copy of the GNU General Public License along with +# this program (see COPYING). If not, see . #