How to use parseOutput method of ipc Package

Best Syzkaller code snippet using ipc.parseOutput

ipc.go

Source:ipc.go Github

copy

Full Screen

...266 env.cmd.close()267 env.cmd = nil268 return269 }270 info, err0 = env.parseOutput(p)271 if info != nil && env.config.Flags&FlagSignal == 0 {272 addFallbackSignal(p, info)273 }274 if !env.config.UseForkServer {275 env.cmd.close()276 env.cmd = nil277 }278 return279}280func (env *Env) Exec(opts *ExecOpts, p *prog.Prog) (output []byte, info *ProgInfo, hanged bool, err0 error) {281 // Copy-in serialized program.282 progSize, err := p.SerializeForExec(env.in)283 if err != nil {284 err0 = fmt.Errorf("failed to serialize: %v", err)285 return286 }287 var progData []byte288 if !env.config.UseShmem {289 progData = env.in[:progSize]290 }291 // Zero out the first two words (ncmd and nsig), so that we don't have garbage there292 // if executor crashes before writing non-garbage there.293 for i := 0; i < 4; i++ {294 env.out[i] = 0295 } 296 atomic.AddUint64(&env.StatExecs, 1)297 if env.cmd == nil {298 if p.Target.OS != "test" && targets.Get(p.Target.OS, p.Target.Arch).HostFuzzer {299 // The executor is actually ssh,300 // starting them too frequently leads to timeouts.301 <-rateLimit.C302 }303 tmpDirPath := "./"304 atomic.AddUint64(&env.StatRestarts, 1)305 env.cmd, err0 = makeCommand(env.pid, env.bin, env.config, env.inFile, env.outFile, env.out, tmpDirPath)306 if err0 != nil {307 return308 }309 }310 // syscall.Setpriority(syscall.PRIO_PROCESS, env.cmd.cmd.Process.Pid, int(p.Prio))311 output, hanged, err0, _ = env.cmd.exec(opts, progData)312 if hanged { 313 atomic.AddUint64(&env.Hangs, 1)314 }315 if err0 != nil {316 env.cmd.close()317 env.cmd = nil318 return319 }320 info, err0 = env.parseOutput(p)321 if info != nil && env.config.Flags&FlagSignal == 0 {322 addFallbackSignal(p, info)323 }324 if !env.config.UseForkServer {325 env.cmd.close()326 env.cmd = nil327 }328 return329}330// addFallbackSignal computes simple fallback signal in cases we don't have real coverage signal.331// We use syscall number or-ed with returned errno value as signal.332// At least this gives us all combinations of syscall+errno.333func addFallbackSignal(p *prog.Prog, info *ProgInfo) {334 callInfos := make([]prog.CallInfo, len(info.Calls))335 for i, inf := range info.Calls {336 if inf.Flags&CallExecuted != 0 {337 callInfos[i].Flags |= prog.CallExecuted338 }339 if inf.Flags&CallFinished != 0 {340 callInfos[i].Flags |= prog.CallFinished341 }342 if inf.Flags&CallBlocked != 0 {343 callInfos[i].Flags |= prog.CallBlocked344 }345 callInfos[i].Errno = inf.Errno346 }347 p.FallbackSignal(callInfos)348 for i, inf := range callInfos {349 info.Calls[i].Signal = inf.Signal350 }351}352func (env *Env) parseOutput(p *prog.Prog) (*ProgInfo, error) {353 out := env.out354 ncmd, ok := readUint32(&out)355 if !ok {356 return nil, fmt.Errorf("failed to read number of calls")357 }358 info := &ProgInfo{Calls: make([]CallInfo, len(p.Calls))}359 extraParts := make([]CallInfo, 0)360 for i := uint32(0); i < ncmd; i++ {361 if len(out) < int(unsafe.Sizeof(callReply{})) {362 return nil, fmt.Errorf("failed to read call %v reply", i)363 }364 reply := *(*callReply)(unsafe.Pointer(&out[0]))365 out = out[unsafe.Sizeof(callReply{}):]366 var inf *CallInfo...

Full Screen

Full Screen

plugin_runner.go

Source:plugin_runner.go Github

copy

Full Screen

1package main2import (3 "context"4 "encoding/json"5 "flag"6 "io"7 "net"8 "os"9 "path/filepath"10 "runtime"11 "strings"12 "sync"13 "time"14 _ "embed"15 "github.com/apex/log"16 "github.com/getlantern/systray"17 "github.com/laher/lunchbox/lunch"18 "github.com/matryer/xbar/pkg/plugins"19 "golang.org/x/exp/utf8string"20)21func runPlugin(ctx context.Context, args []string) error {22 if err := os.Chdir(pluginsDir()); err != nil {23 return err24 }25 var pluginCmd = flag.NewFlagSet("plugin", flag.ExitOnError)26 var pluginPtr = pluginCmd.String("plugin", "", "name of plugin to run")27 err := pluginCmd.Parse(args)28 if err != nil {29 return err30 }31 // TODO should we do dotenv?32 // godotenv.Load(filepath.Base(*pluginPtr) + ".env")33 bin := *pluginPtr34 if !strings.Contains(*pluginPtr, "/") {35 bin = filepath.Join(pluginsDir(), *pluginPtr)36 }37 r, err := newPluginRunner(bin)38 if err != nil {39 return err40 }41 start := &IPCMessage{Type: msgPluginID, Data: filepath.Base(r.plugin.Command)}42 if err := start.Write(r.conn); err != nil {43 return err44 }45 go r.Listen()46 systray.Run(r.init(ctx), r.onExit)47 return nil48}49type pluginRunner struct {50 plugin *plugins.Plugin51 lock sync.Mutex // this lock is for refreshing and actions52 conn net.Conn53 mainItem *systray.MenuItem54 items []*itemWrap55 log *log.Entry56}57func (r *pluginRunner) refreshItems(ctx context.Context) error {58 if err := os.Chdir(pluginsDir()); err != nil {59 r.plugin.OnErr(err)60 return err61 }62 if strings.HasSuffix(r.plugin.Command, ".elvish") || strings.HasSuffix(r.plugin.Command, ".elv") {63 // run it and parse output64 out, err := lunch.ElvishRunScript(ctx, []string{r.plugin.Command})65 if err != nil {66 r.plugin.OnErr(err)67 return err68 }69 items, err := r.plugin.ParseOutput(ctx, r.plugin.Command, strings.NewReader(strings.Join(out, "\n")))70 if err != nil {71 r.plugin.OnErr(err)72 return err73 }74 r.plugin.Items = items75 } else {76 r.plugin.Refresh(ctx)77 }78 // TODO do we need this ever?79 // r.sendIPC(&IPCMessage{Type: msgPluginRestartme})80 return nil81}82func newPluginRunner(bin string) (*pluginRunner, error) {83 p := plugins.NewPlugin(bin)84 conn, err := net.Dial("unix", sockAddr())85 if err != nil {86 return nil, err87 }88 r := &pluginRunner{89 plugin: p,90 conn: conn,91 items: []*itemWrap{},92 log: log.WithField("plugin", filepath.Base(bin)).WithField("pid", os.Getpid()),93 }94 r.log.Infof("plugin runner initialised. full path: %s", bin)95 return r, nil96}97func (r *pluginRunner) Listen() {98 r.log.Infof("listen for messages")99 decoder := json.NewDecoder(r.conn)100 for decoder.More() {101 m := &IPCMessage{}102 if err := decoder.Decode(m); err != nil {103 if err == io.EOF {104 // TODO - restart?105 r.log.Infof("client closed connection")106 return107 }108 r.log.WithError(err).Errorf("error receiving IPC message")109 return110 }111 if err := m.Read(r.conn); err != nil {112 r.log.Errorf("IPC server error %s", err)113 break114 }115 r.log.WithField("messageType", m.Type).WithField("body", m.Data).Debug("plugin runner received message")116 switch m.Type {117 case msgSupervisorRefresh:118 r.lock.Lock()119 r.refresh(context.Background(), false)120 r.lock.Unlock()121 case msgSupervisorUnrecognised:122 // TODO die here?123 r.log.WithField("messageType", m.Type).WithField("body", m.Data).Warnf("supervisor did not recognise previous command")124 case msgSupervisorQuit:125 r.log.Info("Requesting systray quit")126 systray.Quit()127 r.log.Info("Finished quit request")128 default:129 r.log.WithField("messageType", m.Type).130 WithField("body", m.Data).Error("Plugin received a message type which it doesn't recognise")131 r.sendIPC(&IPCMessage{Type: msgPluginUnrecognised})132 }133 }134}135func (r *pluginRunner) init(ctx context.Context) func() {136 r.log.Debug("launching systray icon")137 return func() {138 r.lock.Lock()139 defer r.lock.Unlock()140 r.log.Debugf("initialise plugin with command %s", r.plugin.Command)141 r.refresh(ctx, true)142 go func() {143 time.Sleep(5 * time.Second)144 r.loop()145 }()146 r.log.Infof("initialisation complete for: %s", r.plugin.Command)147 }148}149func (r *pluginRunner) loop() {150 if r.plugin.RefreshInterval.Duration() > 0 {151 ctx := context.Background()152 r.log.Infof("refresh every %v", r.plugin.RefreshInterval.Duration().String())153 for {154 time.Sleep(r.plugin.RefreshInterval.Duration())155 r.lock.Lock()156 r.refresh(ctx, false)157 r.lock.Unlock()158 }159 } else {160 // nothing to do. this plugin is static161 r.log.Info("no refresh")162 }163}164func (r *pluginRunner) sendIPC(m *IPCMessage) {165 err := m.Write(r.conn)166 if err != nil {167 r.log.Warnf("could not write to server: %s", err)168 }169}170func (r *pluginRunner) refreshAll(_ context.Context) {171 r.sendIPC(&IPCMessage{Type: msgPluginRefreshAll})172}173// TODO allow icon configuration (via dotenv?)174func (r *pluginRunner) iconOnly() bool {175 return runtime.GOOS == osWindows || os.Getenv("LUNCHBAR_ICON_ONLY") == "1"176}177func (r *pluginRunner) refresh(ctx context.Context, initial bool) {178 r.log.Debug("refresh items")179 err := r.refreshItems(ctx)180 if err != nil {181 r.sendIPC(&IPCMessage{Type: msgPluginRefreshError})182 }183 r.log.Debug("items refreshed")184 title := r.plugin.Items.CycleItems[0].DisplayText()185 uTitle := utf8string.NewString(title)186 if uTitle.RuneCount() > 20 { // systray seems to hang with long title187 title = uTitle.Slice(0, 19)188 }189 firstChar := uTitle.Slice(0, 1)190 r.log.Infof("title: %s", title)191 systray.SetTooltip(title) // not all platforms192 lunchbarTitle := "Lunchbar menu"193 if r.iconOnly() {194 // necessary for windows - set icon ...195 // it's a bit ugly right now so just leave it for other platforms for now ...196 r.log.Infof("found %d names\n", len(getNames()))197 if b, err := GetIconForChar(strings.ToLower(firstChar)); err != nil {198 r.log.Warnf("couldnt find image for '%s': %v", strings.ToLower(firstChar), err)199 // oops200 } else {201 ic, err := getEmojicon(b)202 if err != nil {203 r.log.Warnf("couldnt load emojicon: %v", err)204 ic, err = getTextIcon(strings.ToLower(firstChar))205 if err != nil {206 r.log.Warnf("couldnt load text icon: %v", err)207 }208 }209 if ic != nil {210 if runtime.GOOS != osWindows || initial { // <- Windows seems to have problems with settings anything after icon211 systray.SetIcon(ic)212 }213 }214 }215 lunchbarTitle = title216 } else {217 systray.SetTitle(title) // doesn't do anything on windows.218 }219 if initial {220 r.LunchbarMenu(lunchbarTitle)221 } else {222 r.mainItem.SetTitle(lunchbarTitle)223 }224 // override the default logger225 r.plugin.Debugf = r.log.Infof226 r.plugin.AppleScriptTemplate = appleScriptDefaultTemplate227 r.log.Infof("found %d items", len(r.plugin.Items.ExpandedItems))228 for index, item := range r.plugin.Items.ExpandedItems {229 r.loadItem(index, item)230 }231 if len(r.plugin.Items.ExpandedItems) < len(r.items) {232 for i := len(r.plugin.Items.ExpandedItems); i < len(r.items); i++ {233 r.items[i].trayItem.Hide()234 }235 }236}237func (r *pluginRunner) loadItem(index int, item *plugins.Item) {238 var itemW *itemWrap239 var title string240 if item.Params.Separator {241 title = "----------"242 } else {243 title = item.DisplayText()244 }245 if len(r.items) < index+1 {246 itemW = &itemWrap{plugItem: item, subitems: []*itemWrap{}}247 itemW.trayItem = systray.AddMenuItem(title, item.DisplayText())248 r.items = append(r.items, itemW)249 r.handleAction(itemW)250 } else {251 itemW = r.items[index]252 itemW.trayItem.SetTitle(item.DisplayText())253 }254 itemW.plugItem = item255 itemW.isSeparator = item.Params.Separator256 itemW.trayItem.Show()257 if len(item.Items) > 0 {258 itemW.trayItem.Enable()259 for subindex, subitem := range item.Items {260 r.loadSubitem(itemW, subindex, subitem)261 }262 if len(item.Items) < len(itemW.subitems) {263 for i := len(item.Items); i < len(itemW.subitems); i++ {264 itemW.subitems[i].trayItem.Hide()265 }266 }267 } else {268 if !item.Params.Separator && itemW.plugItem.Action() != nil {269 itemW.trayItem.Enable()270 } else {271 itemW.trayItem.Disable()272 }273 }274}275func (r *pluginRunner) loadSubitem(itemW *itemWrap, subindex int, subitem *plugins.Item) {276 var subitemW *itemWrap277 if len(itemW.subitems) == subindex { // need to add it278 subitemW = &itemWrap{isSeparator: false, plugItem: subitem, subitems: []*itemWrap{}}279 subitemW.trayItem = itemW.trayItem.AddSubMenuItem(subitem.DisplayText(), "tooltip")280 itemW.subitems = append(itemW.subitems, subitemW)281 r.handleAction(subitemW)282 } else if len(itemW.subitems) > subindex {283 subitemW = itemW.subitems[subindex]284 subitemW.trayItem.SetTitle(subitem.DisplayText())285 subitemW.trayItem.Show()286 } else {287 // error288 r.log.WithFields(log.Fields{289 "subindex": subindex,290 "displaytext": subitem.DisplayText(),291 "len": len(itemW.subitems),292 }).Fatal("not enough subitems. Unexpected. Die")293 }294 if len(subitem.Items) > 0 {295 subitemW.trayItem.Enable()296 for subsubindex, subsubitem := range subitem.Items {297 var subsubitemW *itemWrap298 if len(subitemW.subitems) < subsubindex+1 {299 subsubitemW = &itemWrap{isSeparator: false, plugItem: subsubitem, subitems: []*itemWrap{}}300 subsubitemW.trayItem = subitemW.trayItem.AddSubMenuItem(subsubitem.DisplayText(), "tooltip")301 subitemW.subitems = append(subitemW.subitems, subsubitemW)302 r.handleAction(subsubitemW)303 } else {304 subsubitemW = subitemW.subitems[subsubindex]305 subsubitemW.trayItem.SetTitle(subsubitem.DisplayText())306 subsubitemW.trayItem.Show()307 }308 }309 if len(subitem.Items) < len(subitemW.subitems) {310 for i := len(subitem.Items); i < len(subitemW.subitems); i++ {311 itemW.subitems[i].trayItem.Hide()312 }313 }314 } else if subitemW.plugItem.Action() != nil {315 subitemW.trayItem.Enable()316 } else {317 subitemW.trayItem.Disable()318 }319}320func (r *pluginRunner) handleAction(item *itemWrap) {321 go func() {322 for range item.trayItem.ClickedCh {323 // only run one action at once. avoids stuck actions from accumulating324 r.log.Infof("Clicked item %+v", item)325 r.lock.Lock()326 ctx := context.Background()327 item.DoAction(ctx)328 r.log.Infof("click action complete")329 r.lock.Unlock()330 }331 }()332}333func (r *pluginRunner) onExit() {334}335const appleScriptDefaultTemplate = `336 set quotedScriptName to quoted form of "{{ .Command }}"337 {{ if .Params }}338 set commandLine to {{ .Vars }} & " " & quotedScriptName & " " & {{ .Params }}339 {{ else }}340 set commandLine to {{ .Vars }} & " " & quotedScriptName341 {{ end }}342 if application "Terminal" is running then343 tell application "Terminal"344 do script commandLine345 activate346 end tell347 else348 tell application "Terminal"349 do script commandLine in window 1350 activate351 end tell352 end if353 `...

Full Screen

Full Screen

parseOutput

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 path, err := exec.LookPath("1.exe")4 if err != nil {5 fmt.Println(err)6 }7 cmd := exec.Command(path, "1", "2", "3")8 output, err := cmd.Output()9 if err != nil {10 fmt.Println(err)11 }12 outputStrings := strings.Split(string(output), "13 fmt.Println(outputStrings)14}

Full Screen

Full Screen

parseOutput

Using AI Code Generation

copy

Full Screen

1import (2type ipc struct {3}4func (ipc) parseOutput(cmd *exec.Cmd) string {5 stdout, _ := cmd.StdoutPipe()6 cmd.Start()7 output, _ := ioutil.ReadAll(stdout)8 cmd.Wait()9 return string(output)10}11var (12func runbuttonClicked(b *ui.Button) {13 ip := ipbox.Text()14 port := portbox.Text()15 portint, _ := strconv.Atoi(port)16 if portint >= 1 && portint <= 65535 {17 cmd := exec.Command("ping", ip, "-n", "1", "-w", "1000")18 output := obj.parseOutput(cmd)19 outputarray := strings.Split(output, "20 output = outputarray[len(outputarray)-1]

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful